๐Ÿ”ฅTIL๐Ÿ”ฅ์ŠคํŒŒ๋ฅดํƒ€ | ๊ณผ์ œ : ๊ณ„์‚ฐ๊ธฐ ์•ฑ

hyihyiยท2023๋…„ 12์›” 1์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
10/70
post-thumbnail

๊ณ„์‚ฐ๊ธฐ

์‚ฌ์น™ ์—ฐ์‚ฐ์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ณ„์‚ฐ๊ธฐ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ–ˆ๋‹ค.

์ฒ˜์Œ์— ๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ ๊ตฌ์„ฑ

Calculator.kt
Main.kt

Calculator.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
    }
}

Main.kt

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

๐Ÿ–‹๏ธAbstractOperation.kt

์—ฐ์‚ฐ์„ ํ•˜๋Š” ํด๋ž˜์Šค๋ผ๊ณ  ์•Œ๋ ค์ฃผ๊ธฐ๋งŒ ํ•˜๋Š” ๊ฒƒ

operate : ์ถ”์ƒ ๋ฉ”์„œ๋“œ, ์—ฐ์‚ฐ์„ ํ•˜๋Š” ๊ธฐ๋Šฅ์ด ์ •์˜๋˜์–ด ์žˆ์Œ

abstract class AbstractOperation {
    abstract fun operate(a: Int, b: Int): Double
}

๐Ÿ–‹๏ธAddOperation.kt (Divide/Mod/Multiply/Substract/Operation๋„ ๊ฐ™์Œ)

AbstractOperation์˜ ๋ฉ”์„œ๋“œ๋ฅผ overridingํ•ด operate์˜ ๋™์ž‘์„ ์žฌ์ •์˜ํ•จ

class AddOperation : AbstractOperation() {
    override fun operate(num1: Int, num2: Int): Double = (num1 + num2).toDouble()
}

๐Ÿ–‹๏ธCalculator.kt

class Calculator(private val operator: AbstractOperation){
    fun operator(num1: Int, num2: Int): Double{
        return operator.operate(num1, num2)
    }
}

์–ด๋–ค ์—ฐ์‚ฐ์„ ํ•  ๊ฒƒ์ธ์ง€๋Š” ์•„๋ž˜์ฒ˜๋Ÿผ ์“ธ ์ˆ˜ ์žˆ๋‹ค

Calculator(AddOperation())

๐Ÿ–‹๏ธkotlinApplication.kt

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("์˜ฌ๋ฐ”๋ฅธ ์—ฐ์‚ฐ์ž๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.")
        }
    }
}

๐Ÿ˜ฎ ๋ฐฐ์šด ๊ฒƒ

๋‹จ์ผ ์ฑ…์ž„ ์›์น™(SRP) : ๊ฐ์ฒด๋Š” ๋‹จ ํ•˜๋‚˜์˜ ์ฑ…์ž„๋งŒ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค๋Š” ์›์น™

ํ•œ ํด๋ž˜์Šค๊ฐ€ ์—ฌ๋Ÿฌ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•˜๋ฉด ๊ด€๋ฆฌํ•  ๋ถ€๋ถ„์ด ๋Š˜์–ด๋‚œ๋‹ค.

AbstractOperation์€ operate๋ฅผ ํ•ด์ฃผ๋Š” ํด๋ž˜์Šค๋ผ๊ณ  ๋ณด์žฅ๋งŒ ํ•ด์ฃผ๋ฉด ๋˜๊ณ  ์ž์‹๋“ค์ด ์‚ฌ์น™์—ฐ์‚ฐ์„ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

ํŠœํ„ฐ๋‹˜์ด ํ•ด์ฃผ์‹  ๊ณผ์ œ ํ”ผ๋“œ๋ฐฑ : operator๋Š” kotlin์—์„œ ์˜ˆ์•ฝ์–ด๋กœ ์“ฐ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ณ€์ˆ˜๋ช…์œผ๋กœ ์ ํ•ฉํ•˜์ง€ ์•Š๋‹ค!

profile
๋‚ด๊ฐ€ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์“ฐ๋Š” ๋ธ”๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€