์ฌ์น ์ฐ์ฐ์ ๊ณ์ฐํ๋ ๊ณ์ฐ๊ธฐ ์ฝ๋๋ฅผ ์์ฑํ๋ค.
Calculator.kt
Main.kt
interface Calculator {
fun calculate(a: Int, b: Int): Int
}
class AddOperation : Calculator {
override fun calculate(a: Int, b: Int): Int {
return a + b
}
}
class SubtractOperation : Calculator {
override fun calculate(a: Int, b: Int): Int {
return a - b
}
}
class MultiplyOperation : Calculator {
override fun calculate(a: Int, b: Int): Int {
return a * b
}
}
class DivideOperation : Calculator {
override fun calculate(a: Int, b: Int): Int {
return a / b
}
}
class ModOperation : Calculator {
override fun calculate(a: Int, b: Int): Int {
return a % b
}
}
fun main() {
println("์ฐ์ฐ์์ ์
๋ ฅํด์ฃผ์ธ์")
val operators = listOf("+", "-", "*", "/", "%")
while (true) {
val inputOperation = readLine()?.replace(" ", "")
var operator: String? = null
for (op in operators) {
if (inputOperation?.contains(op) == true) {
operator = op
break
}
}
println("operator: $operator")
if (operator != null) {
val operationList = inputOperation!!.split(operator)
try {
val firstOperand = operationList[0].toInt()
val secondOperand = operationList[1].toInt()
val calc: Calculator = when (operator) {
"+" -> AddOperation()
"-" -> SubtractOperation()
"*" -> MultiplyOperation()
"/" -> DivideOperation()
"%" -> ModOperation()
else -> throw IllegalArgumentException("Unsupported operator")
}
println("calc: $calc")
val result = calc.calculate(firstOperand, secondOperand)
println("$firstOperand $operator $secondOperand ๊ฒฐ๊ณผ๋ : $result ์
๋๋ค")
} catch (e: NumberFormatException) {
println("์ซ์ ํ์์ด ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค.")
}
} else {
println("์ฌ๋ฐ๋ฅธ ์ฐ์ฐ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.")
}
}
}
์์ง ์ธํฐํ์ด์คํ๊ณ ์ถ์ ํด๋์ค์ ์ฐจ์ด๋ฅผ ์ ๋ชจ๋ฅด๊ฒ ๋ค.
Calculator ํด๋์ค ์์ ๋ง์
, ๋บ์
, ๊ณฑ์
, ๋๋์
ํจ์๋ฅผ ์ ์ํ ์๋ ์๋ค. ํ์ง๋ง ์ด๋ ๊ฒ ํ๋ฉด Calculator ํด๋์ค๊ฐ ํ๋ ์ผ์ด ๋๋ฌด ๋ง์์ง๋ค.
๊ทธ๋์ ์ด ์ฌ์น์ฐ์ฐ๋ค์ ํ๋๋ก ๋ฌถ์ผ๋ ค๊ณ ํ๋ค.
๋ง์
, ๋บ์
, ๊ณฑ์
, ๋๋์
์ด ์๋ <์ฐ์ฐ>์ผ๋ก ๋ฌถ๋๋ค.
operateํจ์๋ฅผ ๋ง๋ค๊ณ ์ธ์๋ก ์ฐ์ฐ์, ํผ์ฐ์ฐ์๋ฅผ ๋ฐ์์ ์ฐ์ฐํ๋ ์์ ์ ํด์ค๋ ๋์ง๋ง ์ด๊ฒ๋ ๊ด๋ฆฌํ ํฌ์ธํธ๋ค์ด ๋์ด๋๋ค.
Calculator ์์ฑ์์๋ค ๊ฐ์ฒด๋ฅผ ๋๊ฒจ์ค๋ค
- kotlinApplication.kt
๐abs
ใด AbstractOperation
๐calc
ใด AddOperation
ใด DivideOperation
ใด ModOperation
ใด MultiplyOperation
ใด SubstractOperation
์ฐ์ฐ์ ํ๋ ํด๋์ค๋ผ๊ณ ์๋ ค์ฃผ๊ธฐ๋ง ํ๋ ๊ฒ
operate : ์ถ์ ๋ฉ์๋, ์ฐ์ฐ์ ํ๋ ๊ธฐ๋ฅ์ด ์ ์๋์ด ์์
abstract class AbstractOperation {
abstract fun operate(a: Int, b: Int): Double
}
AbstractOperation์ ๋ฉ์๋๋ฅผ overridingํด operate์ ๋์์ ์ฌ์ ์ํจ
class AddOperation : AbstractOperation() {
override fun operate(num1: Int, num2: Int): Double = (num1 + num2).toDouble()
}
class Calculator(private val operator: AbstractOperation){
fun operator(num1: Int, num2: Int): Double{
return operator.operate(num1, num2)
}
}
์ด๋ค ์ฐ์ฐ์ ํ ๊ฒ์ธ์ง๋ ์๋์ฒ๋ผ ์ธ ์ ์๋ค
Calculator(AddOperation())
fun main(){
println("์ฐ์ฐ์์ ์
๋ ฅํด์ฃผ์ธ์")
val operators = listOf("+", "-", "*", "/", "%")
while (true) {
val inputOperation = readLine()?.replace(" ", "")
println(inputOperation)
var operator: String? = null
for (op in operators) {
if (inputOperation?.contains(op) == true) {
operator = op
break
}
}
if (operator != null) {
val operationList = inputOperation!!.split(operator)
try {
val firstOperand = operationList[0].toInt()
val secondOperand = operationList[1].toInt()
val calc: Calculator = when (operator) {
"+" -> Calculator(AddOperation())
"-" -> Calculator(SubstractOperation())
"*" -> Calculator(MultiplyOperation())
"/" -> Calculator(DivideOperation())
"%" -> Calculator(ModOperation())
else -> throw IllegalArgumentException("Unsupported operator")
}
val result = calc.operator(firstOperand, secondOperand)
println("$firstOperand $operator $secondOperand ๊ฒฐ๊ณผ๋ : $result ์
๋๋ค")
} catch (e: NumberFormatException) {
println("์ซ์ ํ์์ด ์ฌ๋ฐ๋ฅด์ง ์์ต๋๋ค.")
}
} else {
println("์ฌ๋ฐ๋ฅธ ์ฐ์ฐ์๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.")
}
}
}
ํ ํด๋์ค๊ฐ ์ฌ๋ฌ ๋์์ ์ํํ๋ฉด ๊ด๋ฆฌํ ๋ถ๋ถ์ด ๋์ด๋๋ค.
AbstractOperation์ operate๋ฅผ ํด์ฃผ๋ ํด๋์ค๋ผ๊ณ ๋ณด์ฅ๋ง ํด์ฃผ๋ฉด ๋๊ณ ์์๋ค์ด ์ฌ์น์ฐ์ฐ์ ํด์ฃผ๋ฉด ๋๋ค.
ํํฐ๋์ด ํด์ฃผ์ ๊ณผ์ ํผ๋๋ฐฑ : operator๋ kotlin์์ ์์ฝ์ด๋ก ์ฐ์ด๊ธฐ ๋๋ฌธ์ ๋ณ์๋ช ์ผ๋ก ์ ํฉํ์ง ์๋ค!