-
Notifications
You must be signed in to change notification settings - Fork 411
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
base: kimbacksul
Are you sure you want to change the base?
Step2 - 문자열 계산기 #1151
Changes from all commits
d1619f3
f1c70dc
c264520
89b030b
4146987
deca0a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
class Calculation { | ||
|
||
fun calcutateByString(input: String?): Int { | ||
if(input.isNullOrBlank()) { | ||
throw IllegalArgumentException() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 명시적인 오류 메시지를 던져보는건 어떨까요? |
||
} | ||
Comment on lines
+4
to
+6
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
val splitedInput = input.split(" ") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
var result: Int = splitedInput.first().toInt() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return first / second | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
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" | ||
) |
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")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 여유가 있다면 |
||
.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) | ||
} | ||
} |
This file was deleted.
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 | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
이를 직접 콘솔에서 실행해 볼 수 있는
main
함수를 만들어보면 어떨까요? :)