Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[문자열 덧셈 계산기] 김지원 미션 제출합니다. #140

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,55 @@
# kotlin-calculator-precourse
## 요구사항 분석
### ✏️ calculator - 초기화
* 변수 선언
사용자 입력을 받을 `input: String?`과 `result: Int`를 선언한다.
* 반환
`result`를 반환한다.
main()에서 반환값을 받아 `결과 : ${result}` 형식으로 출력한다.

### ✏️calculator - 입력 기능
calculator()에서 `camp.nextstep.edu.missionutils.Console`의 `readLine()`을 이용하여 입력받는다.

### ✏️calculator - 설명 기능
calculator()에서 입력 받기 전, `덧셈할 문자열을 입력해 주세요.` 문구를 출력한다.

### ✏️calculator - 빈 값 처리 1
입력 값이 비어있다면 0을 반환한다.
비어있지 않다면 isRightInput()을 호출한다.

### ✏️calculator - 오류 체크 1
입력값에 `customSeparates`에 저장된 변수가 모두 있는거나 모두 없는지 확인한다.
//만 있거나 \n만 있다면 `IllegalArgumentException`를 발생시킨다.
올바른 입력값이라면 \n의 인덱스+2 또는 0을 반환한다.

### ✏️calculator - 빈 값 처리 2
calculator()에서 반환받은 값을 `index`로 선언하고, input.size 이하라면 0을 반환한다.

### ✏️calculator - separator 호출 및 반환값 합하기
separator(index)를 호출한다.
반환된 값을 합한다.

### ✏️separator - 변수 선언
구분자 종류는 다음과 같다.
- 쉼표
- 콜론
- `//`와 `\n`사이에 위치하는 문자

따라서 변수는 다음과 같다.
- 구분자를 담은 변수 `separates: MutableList`를 선언한다. 초기값은 `,`, `:`이다.
- 커스텀 구분자를 처리하는 변수를 담은 변수 `customSeparates: List`를 선언한다. 초기값은 '//'와 '\n'이다.

### ✏️separator - 커스텀 구분자 체크
`customSeparator(index-3)`를 호출한다.
반환 받은 값을 `separates`에 추가한다.

### ✏️customSeparator - 커스텀 구분자 구하기
인덱스 `2 ~ index`을 만나기 전까지의 문자는 커스텀 구분자이다.
두 인덱스 사이에 있는 문자들을 리스트로 반환한다.

### ✏️separator - 구분하고 반환하기
`index`부터 문자열의 끝까지를 기준으로 split()을 통해 구분한다.
오류 체크를 하고 반환한다.

### ✏️separator - 오류 체크 1
구분된 리스트가 하나라도 숫자가 아니라면 `IllegalArgumentException`을 발생시킨다.
3 changes: 2 additions & 1 deletion src/main/kotlin/calculator/Application.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package calculator

fun main() {
// TODO: 프로그램 구현
val calculator = Calculator()
println("결과 : ${calculator.getSum()}")
}
19 changes: 19 additions & 0 deletions src/main/kotlin/calculator/Calculator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package calculator
import camp.nextstep.edu.missionutils.Console

class Calculator {
private var input: String = ""
private val separator = Separator()

// calculator 초기화하는 함수
private fun setInput() {
println("덧셈할 문자열을 입력해주세요.")
input = Console.readLine()
}

// 사용자 입력을 통해 합을 구하는 함수
fun getSum(): Int {
setInput()
return separator.run(input).sum()
}
}
39 changes: 39 additions & 0 deletions src/main/kotlin/calculator/Separator.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package calculator

class Separator {
private val separates: MutableList<Char> = mutableListOf(',', ':')
private val customSeparates = Pair("//", "\\n")
Comment on lines +4 to +5

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

',', ':'"//", "\\n"

요 아이들은 상수로 관리하는게 가독성에 더 좋다고 생각해요 !

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

개인적으로 요 아이들은 "커스텀 구분자를 입력하기 위한 접두어/접미어" 로 생각을해서
customSeparates 즉, "커스텀 구분자들" 이란 이름과는 조금 맞지 않다는 생각을 했어요!

private var startIndex = 0
private var endIndex = 0
private val result: MutableList<Int> = mutableListOf(0)

// 커스텀 구분자가 있는 경우 separates 변수에 추가하는 함수
private fun getCustomSeparate(input: String) {
if (!input.startsWith(customSeparates.first)) return

startIndex = customSeparates.first.length
endIndex = input.indexOf(customSeparates.second)
require(endIndex != -1) {"커스텀 구분자를 이용하려면 //, \\n 사이에 작성하세요."}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

require를 사용하는건 생각 못했네요 ! 하나 배웠습니다 :)

separates.addAll(input.substring(startIndex, endIndex).toList())

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 잘못 이해한걸 수 도 있어서 조심스럽게 말씀드리자면
저는 기본 구분자로 ',', ':' 요 아이들을 사용해서 숫자를 분리하는 케이스 하나와
커스텀 구분자로 숫자를 분리하는 케이스 두 케이스로 분리해서 코드를 작성했는데

제가 작성하신 코드를 이해한바로는 기본 구분자 + 커스텀 구분자로 구분한다고 이해했는데 요게 맞을까용 ,,?

}

// 구분자를 통해 구분하는 함수
fun run(input: String): List<Int> {
if (input.isBlank()) return listOf(0)

if (input.startsWith(customSeparates.first)) {
getCustomSeparate(input)
if (endIndex+customSeparates.second.length == input.length) return listOf(0)
}

input.substring(endIndex + customSeparates.second.length).split(*separates.toCharArray()).forEach { item ->
item.toIntOrNull()?.let {
result.add(it)
} ?: run {
throw IllegalArgumentException("구분된 ${item}은 숫자가 아닙니다.")
}
}
Comment on lines +30 to +35

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

대박 toIntOrNull을 요런식으로도 활용할 수 도 있군요 ! 좋은 활용법 배워갑니다 !

Comment on lines +20 to +35

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 함수의 이름을 run에서 extractNumbersparseNumbers로 변경하면, 함수의 목적을 더 명확하게 나타낼 수 있을 것 같아요!


return result
}
}