-
Notifications
You must be signed in to change notification settings - Fork 1.4k
피드백에 대한 1차 수정 #1920
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
base: main
Are you sure you want to change the base?
피드백에 대한 1차 수정 #1920
Changes from all commits
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 |
|---|---|---|
| @@ -1 +1,5 @@ | ||
| # java-calculator-precourse | ||
| # java-calculator-precourse | ||
|
|
||
| 문자열을 받아서 덧셈 구현 | ||
| 숫자끼리 합을 구해 결과 도출 | ||
| 커스텀 구분자 지정 가능 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,23 @@ | ||
| package calculator; | ||
|
|
||
| import static camp.nextstep.edu.missionutils.Console.readLine; | ||
|
|
||
| public class Application { | ||
| public static void main(String[] args) { | ||
| // TODO: 프로그램 구현 | ||
| Parse parse = new Parse(); | ||
| Calculator calculator = new Calculator(parse); | ||
| while(true) { | ||
| System.out.println("덧셈할 문자열을 입력해 주세요. 종료를 원하시면 0을 입력해 주세요."); | ||
| String input = readLine(); | ||
| if (input.isEmpty()) { | ||
| continue; | ||
| } | ||
| if (input.charAt(0) == '0') { | ||
| System.out.println("종료합니다."); | ||
| break; | ||
| } | ||
| int output = calculator.getOutput(input); | ||
| System.out.println("결과 : " + output); | ||
| } | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package calculator; | ||
|
|
||
| public class Calculator { | ||
| private final Parse parse; | ||
|
|
||
| public Calculator(Parse parse) { | ||
| this.parse = parse; | ||
| } | ||
|
|
||
| public int getOutput(String input) { | ||
| int i = parse.parseCustom(input); | ||
| int result; | ||
| result = calculate(input,i); | ||
|
|
||
| return result; | ||
|
Comment on lines
+13
to
+15
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 calculate(input,i); |
||
| } | ||
|
|
||
| public int calculate(String input, int i) { | ||
| int output = 0; | ||
| for (; i<input.length(); i++) { | ||
| if (parse.getCustom().contains(input.charAt(i))) { | ||
| output += parse.parseInteger(); | ||
| } else if (input.charAt(i) >= '0' && input.charAt(i) <= '9') { | ||
| parse.addValue(input.charAt(i)); | ||
| } else { | ||
| System.out.println("숫자, 구분자 이외 문자 입력. 애플리케이션 종료."); | ||
| throw new IllegalArgumentException("숫자, 구분자 이외 문자 입력. 애플리케이션 종료."); | ||
| } | ||
| } | ||
| output += parse.parseInteger(); | ||
| return output; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package calculator; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class Parse { | ||
|
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. 클래스 이름으로는 동사보다는 명사가 적합합니다. |
||
| private final List<Character> custom; | ||
| private final StringBuilder value = new StringBuilder(); | ||
|
|
||
| public Parse() { | ||
| custom = new ArrayList<>(); | ||
| custom.add(','); | ||
| custom.add(':'); | ||
|
Comment on lines
+12
to
+13
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. 단순히 ',' 와 ':' 을 하드코딩하는것 보단 아래와 같이 상수로 분리하여 매직넘버를 제거해주세요. private static final char DEFAULT_DELIMITER1 = ',';
private static final char DEFAULT_DELIMITER2 = ':'; |
||
| } | ||
| public List<Character> getCustom() { | ||
| return custom; | ||
| } | ||
|
|
||
| public int parseCustom(String input) { | ||
|
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. 해당 메서드의 반환값은 계산을 시작할 시작 인덱스인데 이름이 |
||
| if (input.length() < 5) { | ||
| return 0; | ||
| } | ||
|
Comment on lines
+20
to
+22
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. 동일하게 5가 무엇을 의미하는지 알기 어렵습니다. |
||
| if (input.charAt(0) == '/') { | ||
| if (input.charAt(1) != '/') { | ||
| System.out.println("/만 나오므로 종료."); | ||
| throw new IllegalArgumentException("/만 나오므로 종료"); | ||
| } | ||
| if (input.charAt(3) != '\\') { | ||
| System.out.println("//이후 \\이 나오지 않아 종료."); | ||
| throw new IllegalArgumentException("//이후 \\이 나오지 않아 종료"); | ||
| } | ||
| if (input.charAt(4) != 'n') { | ||
| System.out.println("\\이후 n이 나오지 않아 종료."); | ||
| throw new IllegalArgumentException("\\이후 n이 나오지 않아 종료"); | ||
| } | ||
| if (!(custom.contains(input.charAt(2)))) { | ||
| custom.add(input.charAt(2)); | ||
| } | ||
| return 5; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| public int parseInteger() { | ||
| int result = 0; | ||
| if (!(value.isEmpty())) { | ||
|
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. StringBuilder 는 |
||
| result = Integer.parseInt(value.toString()); | ||
| } | ||
| value.setLength(0); | ||
| return result; | ||
| } | ||
|
|
||
| public void addValue(char c) { | ||
| value.append(c); | ||
| } | ||
| } | ||
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.
변수 이름을 조금 더 의미 있게 작성해 주시면 좋겠습니다. 현재 이름만으로는 역할을 이해하기 어렵습니다.