-
Notifications
You must be signed in to change notification settings - Fork 132
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
[문자열 덧셈 계산기] 이홍진 미션 제출합니다. #126
base: main
Are you sure you want to change the base?
Conversation
1. Functional Requirements 2. Functional Lists & Checklist
1. 기능 요구 사항에 실행 결과 예시 추가
1. 입력 메세지 "덧셈할 문자열을 입력해 주세요" 추가 2. 쉼표 또는 콜론을 구분자로 갖는 문자열에 대한 덧셈 연산기능 추가 3. 구분자를 기준으로 각 숫자의 합을 출력 todo 1. 커스텀 구분자 확인 2. 오류처리 3. 테스트 4. model, view, controller로 구분
1. 입력 메세지, 구분자 확인, 쉼표, 콜론 2. 출력 결과 todo 1. 커스텀 구분자 확인 2. 오류처리 3. 테스트 4. model, view, controller로 구분
1. 커스텀 구분자를 subStringAfter, subStringBefore로 구분, 이는 정규식을 사용하는 것보다 직관적이라고 생각하여 정함 todo 1. 오류처리 2. 테스트 3. model, view, controller로 구분
1. 입력된 문자열에 숫자가 양수가 아닌경우 "양수로 구성된 문자열을 입력해 주세요."라는 오류메세지와 함께 어플을 종료시킨다 todo 1. 오류처리 2. 테스트 3. model, view, controller로 구분
1. case1과 case2에 경우 둘다 정규식을 이용해 숫자와 구분자가 아닌 경우를 모두 찾아낸 다음 IllegalArgumentException를 발생시켜 어플을 종료시킴 2. 문자열이 입력되지 않은 경우는 없다. readeLine은 String! 타입이기 때문, 빈값이면 0을 반환한다. todo 1. 테스트 2. model, view, controller로 구분
1. 커스텀 구분자 사용시 콤마와 콜론이 같이 혼용될 수 있는지 확인하는 테스트 추가 2. 예외 테스트 음수포함에 에러메세지 확인 3. 예외 테스트 입력오류로 구분자가 아닌 값이 들어왔을때 해당 문자열을 반환하는 에러메세지 확인 4. 커스텀 구분자 사용시 예외 테스트 입력오류로 구분자가 아닌 값이 들어왔을때 해당 문자열을 반환하는 에러메세지 확인 5. BUILD SUCCESSFUL in 0s 확인 todo 1. model, view, controller로 구분
1. 오류처리, 테스트 진행완료 todo 1. model, view, controller로 구분
1. 커스텀 구분자와 콤마, 쉼표를 혼용할때 커스텀 구분자가 가장 뒤, 콤마가 가장 앞에 사용될 경우 앱이 종료되던 버그 수정, 문자열을 분할한 후 또다시 확인하는 방식으로 변경 2. 구분자가 아닌 문자열 찾기 오류 수정, 구분자를 오류를 확인할때 음수를 나타내는 "-"도 정규식에 포함 todo 1. model, view, controller로 구분
1. Application main 함수에서 실행했던 모든 함수를 MVC 패턴의 아키텍처로 변경 2. Model에서는 구분자로 구분된 숫자리스트를 만드는 함수와 그를 계산하는 함수, 오류메세지를 띄우는 함수로 구분 3. View에서는 입력 메세지와 출력 메세지를 보여줌 4. Controller에서는 View의 input을 받아 Model에서 result값을 View로 보내줘 UI를 갱신함
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.
MVC 패턴을 적용한 코드 잘 봤습니다 고생하셨습니다~
import calculator.controller.CalculatorController | ||
import calculator.model.CalculatorModel | ||
import calculator.view.CalculatorView | ||
|
||
fun main() { |
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.
MVC 패턴이 어떤 식으로 구현되는지 궁금했는데 각각 잘 나누어 구분해주신 덕에 이해하기 쉬웠습니다!
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.
감사합니다!
private fun customSeparator(input: String, separator: String): List<String> { | ||
val numList = mutableListOf<String>() | ||
|
||
for (n in input.split(separator)) { |
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.
이 for문 안에 중첩 for문과 if문이 너무 많은데 조금 더 간단하게 할 수 있지 않을까 아쉬움이 남네요
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.
네...그냥 정규식을 이용해서 간단히 구분하는게 훨씬 간단하고 편한거 같네요
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.
단순 코드 라인 수를 줄이기 위해서라면 numList
와for loop
대신 map
을 사용해보는 것도 좋은 방법일 것 같아요!
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.
for문 안에 input.split같은 연산 처리보다 상위에서 처리하여 할당한 변수를 적는 것이 가독성이 좋을 것 같아요.
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.
아키텍처 패턴에 대해 전혀 몰랐는데, 이번 기회에 MVC 패턴을 알 수 있어서 좋았습니다.
|
||
class CalculatorView { | ||
fun getInput(): String { | ||
println("덧셈할 문자열을 입력해 주세요.") |
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.
출력문을 상수화하면 더 보기 좋을거같아요!
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.
1주 차 너무 고생많으셨습니다!!
이번 주차도 같이 파이팅해욥 ㅎㅎ🙌
} | ||
|
||
fun sumWithCustomSeparator(input: String): Int { | ||
val newSeparator = input.substringAfter("//").substringBefore("\\n") |
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.
오 이렇게도 나눌 수가 있군요!! 배워갑니다 👍
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.
subStringAfter
와 substsringBefore
가 있었군요!
새로운 지식 배워갑니다 😃
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.
호오 저도 배워갑니다!🙇🏻♂️
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.
오! 저런 메서드가 있군요! 저도 배워갑니다!
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.
저도 배워갑니다!!
) { | ||
fun runCalculator() { | ||
val input = view.getInput() | ||
val result = if (input.contains("//")) model.sumWithCustomSeparator(input) else model.sumWithDefaultSeparator(input) |
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.
여기서 //
로 분기를 나누신 의도가 궁금합니다!! 👀
커스텀 구분자와 기본 구분자를 나누기 위함이라면 \n
은 포함시키지 않으신 이유가 어떤 것일까요!!?
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.
커스텀 구분자에만 '//'이 사용되기 때문에 넣었습니다!
return numList | ||
} | ||
|
||
private fun commaSeparator(s: String, numList: MutableList<String>) { |
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.
매개변수 네이밍을 s가 아닌 다른 이름으로 짓는 것이 어떨까요!? 어떤 매개변수인지 알기 어려울 것 같습니다!
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.
넵 참고하겠습니다.
단축키로 함수를 빼다보니 매개변수에 대해 수정을 안했나보네요ㅠ
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.
1주차 수고하셨습니다!!!! 2주차도 같이 파이팅해요!!
errorNotPositiveNumber(s) | ||
numList.add(s) | ||
} | ||
} |
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.
colon 이랑 comma separator를 따로 구분하신 이유가 있나요?
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.
반복되어서 많이 사용될까봐 함수로 만들어 봤었던것 같습니다.
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.
1주차 미션 고생하셨습니다!!
모델의 함수명이 명사형으로 되어있는 것 같습니다.
동사 + 명사 형태로 수정하는게 가독성 면에서 더 좋을 것 같아요!
} | ||
|
||
fun sumWithCustomSeparator(input: String): Int { | ||
val newSeparator = input.substringAfter("//").substringBefore("\\n") |
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.
subStringAfter
와 substsringBefore
가 있었군요!
새로운 지식 배워갑니다 😃
private fun customSeparator(input: String, separator: String): List<String> { | ||
val numList = mutableListOf<String>() | ||
|
||
for (n in input.split(separator)) { |
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.
단순 코드 라인 수를 줄이기 위해서라면 numList
와for loop
대신 map
을 사용해보는 것도 좋은 방법일 것 같아요!
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.
1주차 진행하시느라 고생하셨습니다!!
리뷰하면서 MVC패턴 적용을 느껴볼 수 있어 도움이 많이 되었습니다.
다른 분들 리뷰처럼 정규식과 매개변수 네이밍이 조금 아쉽네요.
이번주도 화이팅 하세요!!!!
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.
MVC 패턴을 적용시켜서 일까요? 설계를 잘하셨다는 느낌이 드네요.
return numList.sumOf { it.toInt() } | ||
} | ||
|
||
private fun defaultSeparator(input: String): List<String> { |
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.
함수명이 직관적이지 않아보입니다. 기능에 맞게 splitByDefaultSeparator 등으로 네이밍하면 어떨까요?
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.
네이밍에 신경더 쓰겠습니다. 감사합니다!
fun sumWithDefaultSeparator(input: String): Int { | ||
errorNotPositiveNumber(input) | ||
val numList = defaultSeparator(input) | ||
return numList.sumOf { it.toInt() } | ||
} |
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.
제가 놓쳤을 수도 있지만, Defalut케이스에서는 반환하기 전에 입력이 숫자가 아닌지 체크하는 부분이 없는 듯 합니다.
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.
그렇네요. 정확한 지적 감사합니다!!
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.
1주차 고생 많으셨습니다! 코드 리뷰 감사합니다. 화이팅!😋
throw IllegalArgumentException("${invalidChar}는 구분자가 아닙니다.") | ||
} | ||
} | ||
} |
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.
Separator 마다 따로 메소드를 정의해놓았는데, 중복을 줄일 수 있지 않을까요?
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.
넵 저도 아쉬운점이라 생각하고 있습니다. 복기하면서 최대한 줄여보겠습니다! 감사합니다
fun `예외 테스트 입력오류2`() { | ||
assertSimpleTest { | ||
assertThrows<IllegalArgumentException> { runException("//;\\nb1,2:a3;4c") } | ||
assertThat(output().contains("bac는 구분자가 아닙니다.")) | ||
} | ||
} | ||
|
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.
JUnit 단위 테스트 하는 방법을 배워갈 수 있었습니다😊
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.
1주차 고생하셨습니다~~!
남은 주차도 같이 힘내요!!!😤
} | ||
|
||
fun sumWithCustomSeparator(input: String): Int { | ||
val newSeparator = input.substringAfter("//").substringBefore("\\n") |
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.
호오 저도 배워갑니다!🙇🏻♂️
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.
제 개인적인 생각이긴 한데 지금 구현하신 CalculatorModel에서 모든 도메인 로직을 수행하고 있다는 생각이 듭니다.
좀더 명확성을 위해 계산기는 계산에 대한 책임만 수행하고, 구분자와 피연산자에 대해서는 분리해서 각각의 책임을 가지는 객체를 선언함으로써 책임을 분산시키는 방법도 가능하지 않을까? 하는 생각을 남겨봅니다!
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.
오오 좋은 의견 감사합니다!
모델에서도 각각 책임을 나눠 객체를 만드는 방법 배워갑니다!
) { | ||
fun runCalculator() { | ||
val input = view.getInput() | ||
val result = if (input.contains("//")) model.sumWithCustomSeparator(input) else model.sumWithDefaultSeparator(input) |
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.
"input.contains("//")"라는 조건문을 사용하셨는데
컨트롤러 영역에서 이 조건문의 의미가 커스텀 구분자가 존재할 수 있는지 확인하는 조건문이라는 것을 계산기의 요구사항에 대한 배경지식이 없다면 처음 코드를 읽는 자가 이해하기가 어려울 수 있다고 생각합니다.
- if (input.contains("//")) model.sumWithCustomSeparator(input) else model.sumWithDefaultSeparator(input) 이 부분에 대한 기능을 하나의 함수로 만들거나,
- val containCustomPrefix = input.contains("//") 이런식으로 조건문을 변수로 만들어 조건문에 활용하면
가독성이 더 좋아질 것 같습니다.
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.
좋은 지적 감사합니다!
이해하기 쉽게 가독성을 높이는 법도 중요하단 것 배워갑니다.
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.
1 주차 고생하셨습니다~~
리뷰가 이미 많아서 리뷰 없는 부분들 위주로 봤어요 !ㅎㅎ
private fun customSeparator(input: String, separator: String): List<String> { | ||
val numList = mutableListOf<String>() | ||
|
||
for (n in input.split(separator)) { |
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.
for문 안에 input.split같은 연산 처리보다 상위에서 처리하여 할당한 변수를 적는 것이 가독성이 좋을 것 같아요.
for (n in input.split(separator)) { | ||
if (n.contains(":") || n.contains(",")) { | ||
for (s in n.split(":")) { | ||
if (s.contains(",")) { |
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.
split안에 구분자들을 여러개 넣어 한 번에 처리하면 효율적이고 코드가 개선될 것 같아요.
또는 구분자들을 관리하는 배열이나 리스트를 활용하면 좋을 것 같아요.
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.
맞아요 split 구분자를 여러개 사용할 수 있더라고요.. 다음에 활용해 보겠습니다!
|
||
private fun errorNotPositiveNumber(num: String) { | ||
if (num.toInt() < 1) { | ||
throw IllegalArgumentException("양수로 구성된 문자열을 입력해 주세요.") |
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.
예외 처리 기능과 테스트 코드 등을 읽어보았으나 0 값을 처리하는 부분을 따로 확인을 못한 것 같아서 질문합니다.
입력 값이 ""가 0으로 처리되는 부분이 어디인가요?
"0" 입력 시 예외가 발생하는것인지 궁금합니다.
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.
그렇네요
코드 구조를 변경하다보니 함수 리펙토링을 진행했고, 그 과정에서 빈값이 들어왔을 때 0값 처리를 해주던 연산을 고려하지 않았네요
테스트 케이스에도 넣지 않아서 알아차리기 어려웠습니다!
Model에 sumWithDefaultSeparator() 함수 첫줄에
if (input.isEmpty()) return 0
를 추가해 해결했습니다.
감사합니다
1주차동안 고생 많으셨습니다. |
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.
아이고 제가 리뷰를 남겨놓고 펜딩 상태로 뒀네요 죄송합니다 지금서 알아차려서 늦게나마 올려둡니다!
3주차도 화이팅입니다!
if (input.isNotEmpty()) { | ||
for (n in input.split(",")) { | ||
if (n.contains(":")) { | ||
for (s in n.split(":")) { |
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.
임시 변수로 보이긴 하지만 n이나 s보다는 좀 더 명확한 변수명을 사용해보시는 건 어떨까 싶습니다!
if (invalidChar.isNotEmpty()) { | ||
throw IllegalArgumentException("${invalidChar}는 구분자가 아닙니다.") | ||
} | ||
controller.runCalculator() |
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.
app에서 Model이랑 View 불러와서 Controller로 주는 건 생각 못했는데 배워갑니다!
|
||
fun sumWithCustomSeparator(input: String): Int { | ||
val newSeparator = input.substringAfter("//").substringBefore("\\n") | ||
val newInput = input.substringAfter("\\n") |
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.
전 냅다 인덱스로 구별했는데 이런 방법이 있었네요!! bb
} | ||
|
||
fun sumWithCustomSeparator(input: String): Int { | ||
val newSeparator = input.substringAfter("//").substringBefore("\\n") |
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.
제가 못찾은 걸 수도 있지만 이렇게 앞뒤를 확인해서 잘랐을 때 커스텀 구분자가 여러 개일 경우에도 체크하는 게 있을까요?
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.
해당 경우는 생각하지 못했네요!
subStringAfter
와 subStringBefore
는 문자열의 앞에서부터 작동하기 때문에, //;\n//?\n
과 같이 커스텀 구분자가 두 개가 있다면 ;
값만 체크하고 뒤에 값은 입력오류가 나겠네요.
while
과 같은 중복문으로 커스텀 구분자를 여러번 체크하는 방법도 있겠네요!
기능 목록
입력
출력
오류처리
테스트