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

Step2 - 문자열 계산기 #1151

Open
wants to merge 6 commits into
base: kimbacksul
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
40 changes: 40 additions & 0 deletions src/main/kotlin/Calculation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Calculation {

fun calcutateByString(input: String?): Int {
Copy link

Choose a reason for hiding this comment

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

이를 직접 콘솔에서 실행해 볼 수 있는 main함수를 만들어보면 어떨까요? :)

if(input.isNullOrBlank()) {
throw IllegalArgumentException()
Copy link

Choose a reason for hiding this comment

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

명시적인 오류 메시지를 던져보는건 어떨까요?

}
Comment on lines +4 to +6
Copy link

Choose a reason for hiding this comment

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

kotlinruquire를 사용해 볼 수 있어요 :)

val splitedInput = input.split(" ")
Copy link

Choose a reason for hiding this comment

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

" " 는 상수화 해 볼 수 있을 것 같아요 :)

var result: Int = splitedInput.first().toInt()
Copy link

Choose a reason for hiding this comment

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

숫자가 아닌 문자가 들어오면 어떻게 될까요?

splitedInput.drop(1).windowed(2, 2).forEach {
result = calculate(result, it[1].toInt(), it[0])
}
return result
}

private fun calculate(first: Int, second: Int, operator: String): Int {
return when(operator) {
"+" -> add(first, second)
"-" -> subtract(first, second)
"*" -> multiply(first, second)
"/" -> divide(first, second)
else -> throw IllegalArgumentException("Using unsupported operators")
}
}

private fun add(first: Int, second: Int): Int {
return first + second
}

private fun subtract(first: Int, second: Int): Int {
return first - second
}

private fun multiply(first: Int, second: Int): Int {
return first * second
}

private fun divide(first: Int, second: Int): Int {
Copy link

Choose a reason for hiding this comment

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

5/2 일 경우에는 어떤 결과값이 나올까요? 이 경우도 테스트로 작성해보면 좋을 것 같아요:)

return first / second
}
}
Copy link

Choose a reason for hiding this comment

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

intellji에서 new line 설정이 가능하니 설정해두시면 좋습니다 :)

5 changes: 5 additions & 0 deletions src/main/kotlin/Person.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data class Person(
val name: String,
val age: Int,
var nickname: String? = "Guest"
)
44 changes: 44 additions & 0 deletions src/test/kotlin/CalcutationTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package study

import Calculation
import org.assertj.core.api.Assertions
import org.assertj.core.api.Assertions.assertThat
import org.assertj.core.api.Assertions.assertThatThrownBy
import org.junit.jupiter.api.Test

class CalcutationTest {

@Test
fun `예제 확인`() {
assertThat(Calculation().calcutateByString("2 + 3 * 4 / 2"))
Copy link

Choose a reason for hiding this comment

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

여유가 있다면 @ParameterizedTest 를 이용해서 다양한 케이스에 대해 테스트를 해보면 좋을 것 같아요 :)

.isEqualTo(10)
}

@Test
fun `입력값이 Null`() {
assertThatThrownBy {
Calculation().calcutateByString(null)
}.isInstanceOf(IllegalArgumentException::class.java)
}

@Test
fun `연산자 다를때`() {
assertThatThrownBy {
Calculation().calcutateByString("2 + 3 * 4 % 2")
}.isInstanceOf(IllegalArgumentException::class.java)
}

@Test
fun `문자열 처음 슷자가 아닐 때`() {
assertThatThrownBy {
Calculation().calcutateByString("# + 3 * 4 / 2")
}.isInstanceOf(IllegalArgumentException::class.java)
}

@Test
fun `문자열 마지막이 숫자가 아닐 때`() {
assertThatThrownBy {
Calculation().calcutateByString("1 + 3 * 4 / #")
}.isInstanceOf(IllegalArgumentException::class.java)
}
}
29 changes: 0 additions & 29 deletions src/test/kotlin/Person.kt

This file was deleted.

1 change: 1 addition & 0 deletions src/test/kotlin/PersonTest.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package study

import Person
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

Expand Down