-
Notifications
You must be signed in to change notification settings - Fork 59
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
제세형 1주차 과제입니다. #53
제세형 1주차 과제입니다. #53
Conversation
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.
고생하셨습니다 👍
전반적으로 잘해주신 것 같아요 !
몇 가지 코멘트 남겼으니 확인 부탁드립니다 ~
if(right == 0){ //우변이 0인 경우 -> 0으로 나누는 문제 | ||
throw new RuntimeException("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.
👍
src/main/java/StringCalculator.java
Outdated
if(input == null || input.isEmpty()) return 0; | ||
|
||
//기본 구분자 정규표현식 (, 또는 :) | ||
String splitter = "[,|:]"; |
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.
정규 표현식 변수는 보통 regex
를 많이 사용하는 거 같아요 !
int formatEndIndex = input.indexOf("\n"); | ||
if (formatEndIndex == -1){ // "\n" 을 찾을 수 없는 경우 | ||
throw new RuntimeException("올바르지 않은 커스텀 구분자 지정 구문입니다."); | ||
} |
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.
해당 경우에 커스텀 구분자 지정 문제임에도 '숫자 이외의 값이 입력됨' 에서 예외가 발생하는 것을 확인하여 해당 경우에 제대로 커스텀 구분자 지정 예외가 발생하도록 수정하였습니다!
더불어 AssertJ 단위 테스트 또한 올바른 예외 구문이 출력되는지 비교할 수 있도록 hasMessage() 함수를 적용하여 수정했습니다.
src/main/java/StringCalculator.java
Outdated
throw new RuntimeException("올바르지 않은 커스텀 구분자 지정 구문입니다."); | ||
} | ||
|
||
splitter = "[" + Pattern.quote(input.substring(2, formatEndIndex)) + "]"; //Pattern.quote를 이용하여 특수문자를 안전하게 처리 |
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.
quote
메소드는 저도 처음 보네요 👀 하나 배워 갑니다 !
splitter에 있는 기본 구분자가 커스텀 구분자로 덮어지는 거 같아요. ,과 :
을 기본 구분자로 가지면서 커스텀 구분자도 가져가는 방법에는 어떤 방법이 있을까요 ??
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.
기본 구문자 2종 (, 또는 :) 과 커스텀으로 지정한 구문자를 혼용 하여 사용할 수 있음을 놓치고 있었던 것 같습니다..!
커스텀 구분자 지정시 기본 구문자 2종을 함께 가져갈 수 있도록 수정하였고 단위 테스트에도 여러 구문자를 혼용 사용 하는 케이스를 추가했습니다
src/main/java/StringCalculator.java
Outdated
} | ||
|
||
private int calculateSum(String numbers, String splitter) { | ||
String[] tokens = numbers.split(splitter); |
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.
💯
src/main/java/StringCalculator.java
Outdated
int result = 0; | ||
for(int number : parsedNumbers){ | ||
result += number; | ||
} | ||
return result; |
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.
극단적인 입력값이 들어와서 int 자료형의 범위를 넘어가게 된다면, 오버 플로우가 발생하게 됩니다.
이를 해결하기 위해서 자바에서는 어떻게 처리하는지 추가 학습하면 더 좋을 거 같아요 !
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.
자바에서는 정수형 타입의 오버플로우 발생을 해결하기 위해 BigInteger 클래스 (문자열 타입을 이용한 정수 계산, 메모리만 충분 하다면 아주 큰 숫자도 계산 가능) 를 이용하거나 오버플로우 발생시 예외를 발생시키기 위해 Math 클래스의 관련 정적 메소드를 활용할 수 있음을 추가 학습했습니다.
Math.addExact() 정적 메서드를 이용해 결과값을 계산하고 계산 중 발생한 ArithmeticException을 캐치하여 "계산 결과값이 너무 큽니다" 라는 런타임예외를 발생할 수 있도록 수정하였습니다. 이에 따라 단위테스트에도 오버플로우가 발생할 수 있는 입력을 추가했습니다.
//0으로 나누는 예외 | ||
assertThrows(RuntimeException.class, () -> calculator.div(6,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.
💯
@DisplayName("문자열 계산기 단위 테스트") | ||
public class TestStringCalculator { | ||
|
||
StringCalculator sc = new StringCalculator(); |
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.
풀네임으로 적는 방향이 좋은 거 같아요 !
sc
라는 변수명이 다른 사람이 봤을 때, 무슨 뜻인지 이해하기 힘들 것 같아요 !
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주차 미션 수행하여 리뷰 요청드립니다.
Git 사용법을 익히는 것 부터 이번에 사용한 InteliJ IDE가 생소하여 다소 제출이 늦어진 점 죄송합니다..!
다음 주차 과제부터는 일정 차질 없게 제출할 수 있도록 하겠습니다.