From 5b600c9a805201ea9ec96c349d43983ecd8205db Mon Sep 17 00:00:00 2001 From: tonic523 Date: Fri, 15 Sep 2023 01:06:10 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EA=B3=84=EC=82=B0=EA=B8=B0=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 기능 구현에 집중한 나머지 기능 별 커밋을 나누지 못했네요.. 리팩토링할 때 기능 별로 수정하여 커밋올리겠습니다. --- .idea/.gitignore | 8 ++ .idea/misc.xml | 6 ++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 ++ wooseok/.gitignore | 30 +++++++ wooseok/README.md | 78 +++++++++++++++++++ wooseok/src/main/kotlin/Main.kt | 38 +++++++++ .../controller/CalculationController.kt | 21 +++++ .../src/main/kotlin/controller/Controller.kt | 6 ++ .../main/kotlin/controller/EndController.kt | 8 ++ .../kotlin/controller/InquiryController.kt | 14 ++++ .../main/kotlin/domain/CalculateExpression.kt | 19 +++++ .../main/kotlin/domain/CalculationResult.kt | 3 + .../src/main/kotlin/domain/EmptyExpression.kt | 14 ++++ wooseok/src/main/kotlin/domain/Expression.kt | 10 +++ wooseok/src/main/kotlin/domain/Operator.kt | 39 ++++++++++ .../repository/CalculationRepository.kt | 16 ++++ wooseok/wooseok.iml | 15 ++++ 18 files changed, 339 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 wooseok/.gitignore create mode 100644 wooseok/README.md create mode 100644 wooseok/src/main/kotlin/Main.kt create mode 100644 wooseok/src/main/kotlin/controller/CalculationController.kt create mode 100644 wooseok/src/main/kotlin/controller/Controller.kt create mode 100644 wooseok/src/main/kotlin/controller/EndController.kt create mode 100644 wooseok/src/main/kotlin/controller/InquiryController.kt create mode 100644 wooseok/src/main/kotlin/domain/CalculateExpression.kt create mode 100644 wooseok/src/main/kotlin/domain/CalculationResult.kt create mode 100644 wooseok/src/main/kotlin/domain/EmptyExpression.kt create mode 100644 wooseok/src/main/kotlin/domain/Expression.kt create mode 100644 wooseok/src/main/kotlin/domain/Operator.kt create mode 100644 wooseok/src/main/kotlin/repository/CalculationRepository.kt create mode 100644 wooseok/wooseok.iml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..639900d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..67457af --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/wooseok/.gitignore b/wooseok/.gitignore new file mode 100644 index 0000000..872f2f8 --- /dev/null +++ b/wooseok/.gitignore @@ -0,0 +1,30 @@ +### IntelliJ IDEA ### +.idea +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/wooseok/README.md b/wooseok/README.md new file mode 100644 index 0000000..fe1a348 --- /dev/null +++ b/wooseok/README.md @@ -0,0 +1,78 @@ +# kotlin_calculator +코틀린 계산기 구현 미션 Repository입니다.
+❗각자 폴더만들고 그 안에 프로젝트를 만들어주세요! (0depth에 프로젝트가 올라가지않게 주의) + +### 과제를 통해 기대하는 역량 + +- 기본적인 테스트 코드 작성 및 활용하는 능력해보자 +- 스스로 OOP를 생각하고 코드로 옮길 수 있는 능력해보자 + +### 요구사항 +- 콘솔로 구현입니다.(스윙으로 구현하시는 분들 계실까봐) +- 객체지향적인 코드로 계산기 구현하기 + - [ ] 더하기 + - [ ] 빼기 + - [ ] 곱하기 + - [ ] 나누기 + - [ ] 우선순위(사칙연산) +- [ ] 테스트 코드 구현하기 +- [ ] 계산 이력을 맵으로 데이터 저장기능 만들기 + - 애플리케이션이 동작하는 동안 데이터베이스 외에 데이터를 저장할 수 있는 방법을 고안해보세요. +- (선택) 정규식 사용 + +### 실행결과(콘솔) +``` +1. 조회 +2. 계산 + +선택 : 2 + +1 + 2 +3 + +1. 조회 +2. 계산 + +선택 : 2 + +1 + 2 * 3 +7 + +1. 조회 +2. 계산 + +선택 : 1 + +1 + 2 = 3 +1 + 2 * 3 = 7 + +선택 : 2 + +3 - 2 * 2 +-1 +``` + +# 요구사항 분석 +## 비즈니스 로직 +### 연산 +1. 왼쪽부터 오른쪽으로 사칙연산 중 곱하기(*), 나누기(/) 부터 연산한다. +2. 왼쪽부터 오른쪽으로 사칙연산 중 더하기(+), 빼기(-) 를 연산한다. + +### 조회 +1. 식을 저장한다. +2. 연산 완료된 값을 저장한다. + +## 도메인 별 역할 +### Expression +- 숫자를 받으면 연산한 결과값을 반환할 수 있다. +- 연산자와 숫자를 가지고 있다. +- EmptyExpression은 연산할 경우 전달받은 숫자를 그대로 반환한다. + +### Operator +- 연산자에 맞게 연산할 수 있다. +- 연산자끼리의 연산 우선순위를 비교할 수 있다. +- 사칙연산이 구현되어 있다. + +## 그 외 객체의 역할 +- controller: 프로그램의 프로세스를 관리할 수 있다. +- repository: 연산 결과를 저장하고 조회할 수 있다. diff --git a/wooseok/src/main/kotlin/Main.kt b/wooseok/src/main/kotlin/Main.kt new file mode 100644 index 0000000..f0061d3 --- /dev/null +++ b/wooseok/src/main/kotlin/Main.kt @@ -0,0 +1,38 @@ +import controller.CalculationController +import controller.EndController +import controller.InquiryController + +fun main(args: Array) { + + val inquiryController = InquiryController() + val calculationController = CalculationController() + val endController = EndController() + + running@ while (true) { + when (choiceController()) { + 1 -> inquiryController.run() + 2 -> calculationController.run() + 3 -> { + endController.run() + break@running + } + else -> { + println("해당 번호에 맞는 기능이 없습니다. 다시 입력해주세요.") + inquiryController.run() + } + } + } +} + +fun choiceController(): Int { + print( + """ + 1. 조회 + 2. 계산 + 3. 종료 + 선택 : + """.trimIndent() + ) + + return readln().toInt() +} diff --git a/wooseok/src/main/kotlin/controller/CalculationController.kt b/wooseok/src/main/kotlin/controller/CalculationController.kt new file mode 100644 index 0000000..07b1050 --- /dev/null +++ b/wooseok/src/main/kotlin/controller/CalculationController.kt @@ -0,0 +1,21 @@ +package controller + +import domain.* +import repository.CalculationRepository + +class CalculationController: Controller { + + override fun run() { + val readLine = readLine() + val strings: List = readLine?.split(" ")!! + + val startNumber: Int = strings[0].toInt() + var expression: Expression = EmptyExpression(); + for (index in strings.size-1 downTo 2 step 2) { + expression = CalculateExpression(strings[index].toInt(), Operator.of(strings[index-1]), expression) + } + val result = expression.calculate(startNumber) + CalculationRepository.save(CalculationResult(strings.joinToString(" "), result.toString())) + println(result) + } +} diff --git a/wooseok/src/main/kotlin/controller/Controller.kt b/wooseok/src/main/kotlin/controller/Controller.kt new file mode 100644 index 0000000..6ff2850 --- /dev/null +++ b/wooseok/src/main/kotlin/controller/Controller.kt @@ -0,0 +1,6 @@ +package controller + +interface Controller { + + fun run() +} \ No newline at end of file diff --git a/wooseok/src/main/kotlin/controller/EndController.kt b/wooseok/src/main/kotlin/controller/EndController.kt new file mode 100644 index 0000000..5869e5b --- /dev/null +++ b/wooseok/src/main/kotlin/controller/EndController.kt @@ -0,0 +1,8 @@ +package controller + +class EndController: Controller { + + override fun run() { + println("계산기를 종료합니다.") + } +} \ No newline at end of file diff --git a/wooseok/src/main/kotlin/controller/InquiryController.kt b/wooseok/src/main/kotlin/controller/InquiryController.kt new file mode 100644 index 0000000..71afd01 --- /dev/null +++ b/wooseok/src/main/kotlin/controller/InquiryController.kt @@ -0,0 +1,14 @@ +package controller + +import domain.* +import repository.CalculationRepository + +class InquiryController(): Controller { + + override fun run() { + val calculationResults: List = CalculationRepository.findAll() + for (calculationResult in calculationResults) { + println(calculationResult.expression + " = " + calculationResult.result) + } + } +} \ No newline at end of file diff --git a/wooseok/src/main/kotlin/domain/CalculateExpression.kt b/wooseok/src/main/kotlin/domain/CalculateExpression.kt new file mode 100644 index 0000000..965c16c --- /dev/null +++ b/wooseok/src/main/kotlin/domain/CalculateExpression.kt @@ -0,0 +1,19 @@ +package domain + +open class CalculateExpression( + override val number: Int, + override val operator: Operator, + val nextExpression: Expression = EmptyExpression(), + override val isEnd: Boolean = false +) : Expression { + + override fun calculate(number: Int): Int { + if (nextExpression.isEnd) { + return operator.operate(number, this.number) + } + if (operator.isPrecede(nextExpression.operator)) { + return nextExpression.calculate(operator.operate(number, this.number)) + } + return operator.operate(number, nextExpression.calculate(this.number)) + } +} diff --git a/wooseok/src/main/kotlin/domain/CalculationResult.kt b/wooseok/src/main/kotlin/domain/CalculationResult.kt new file mode 100644 index 0000000..8b1dbd9 --- /dev/null +++ b/wooseok/src/main/kotlin/domain/CalculationResult.kt @@ -0,0 +1,3 @@ +package domain + +data class CalculationResult(val expression: String, val result: String) \ No newline at end of file diff --git a/wooseok/src/main/kotlin/domain/EmptyExpression.kt b/wooseok/src/main/kotlin/domain/EmptyExpression.kt new file mode 100644 index 0000000..4036888 --- /dev/null +++ b/wooseok/src/main/kotlin/domain/EmptyExpression.kt @@ -0,0 +1,14 @@ +package domain + +class EmptyExpression : Expression { + override val number: Int + get() = 0 + override val operator: Operator + get() = Operator.ADDITION + + override val isEnd: Boolean = true + + override fun calculate(number: Int): Int { + return number + } +} diff --git a/wooseok/src/main/kotlin/domain/Expression.kt b/wooseok/src/main/kotlin/domain/Expression.kt new file mode 100644 index 0000000..151fa4b --- /dev/null +++ b/wooseok/src/main/kotlin/domain/Expression.kt @@ -0,0 +1,10 @@ +package domain + +interface Expression { + + val number: Int + val operator: Operator + val isEnd: Boolean + + fun calculate(number: Int): Int +} \ No newline at end of file diff --git a/wooseok/src/main/kotlin/domain/Operator.kt b/wooseok/src/main/kotlin/domain/Operator.kt new file mode 100644 index 0000000..32c99c8 --- /dev/null +++ b/wooseok/src/main/kotlin/domain/Operator.kt @@ -0,0 +1,39 @@ +package domain + +import java.util.* + +enum class Operator(val orderNumber: Int, val symbol: String) { + + ADDITION(0, "+") { + override fun operate(number1: Int, number2: Int): Int { + return number1 + number2 + } + }, + SUBSTRACTION(0, "-") { + override fun operate(number1: Int, number2: Int): Int { + return number1 - number2 + } + }, + MULTIPLICATION(1, "*") { + override fun operate(number1: Int, number2: Int): Int { + return number1 * number2 + } + }, + DIVISION(1, "/") { + override fun operate(number1: Int, number2: Int): Int { + return number1 / number2 + } + } + ; + + abstract fun operate(number1: Int, number2: Int): Int + fun isPrecede(other: Operator): Boolean { + return orderNumber > other.orderNumber + } + + companion object { + fun of(symbol: String): Operator { + return entries.first { it.symbol == symbol } + } + } +} \ No newline at end of file diff --git a/wooseok/src/main/kotlin/repository/CalculationRepository.kt b/wooseok/src/main/kotlin/repository/CalculationRepository.kt new file mode 100644 index 0000000..b42deb3 --- /dev/null +++ b/wooseok/src/main/kotlin/repository/CalculationRepository.kt @@ -0,0 +1,16 @@ +package repository + +import domain.CalculationResult + +object CalculationRepository { + + val store: MutableList = mutableListOf() + + fun save(calculationResult: CalculationResult) { + store.add(calculationResult) + } + + fun findAll(): List { + return store.toList() + } +} \ No newline at end of file diff --git a/wooseok/wooseok.iml b/wooseok/wooseok.iml new file mode 100644 index 0000000..4eba30b --- /dev/null +++ b/wooseok/wooseok.iml @@ -0,0 +1,15 @@ + + + + + + + + + + + + + + + \ No newline at end of file