Skip to content
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# java-calculator-precourse
# java-calculator-precourse

문자열을 받아서 덧셈 구현
숫자끼리 합을 구해 결과 도출
커스텀 구분자 지정 가능
20 changes: 18 additions & 2 deletions src/main/java/calculator/Application.java
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);
}
}
}
}
33 changes: 33 additions & 0 deletions src/main/java/calculator/Calculator.java
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);

Choose a reason for hiding this comment

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

변수 이름을 조금 더 의미 있게 작성해 주시면 좋겠습니다. 현재 이름만으로는 역할을 이해하기 어렵습니다.

int result;
result = calculate(input,i);

return result;
Comment on lines +13 to +15

Choose a reason for hiding this comment

The 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;
}
}
56 changes: 56 additions & 0 deletions src/main/java/calculator/Parse.java
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 {

Choose a reason for hiding this comment

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

클래스 이름으로는 동사보다는 명사가 적합합니다. Parser 와 같이 변경하면 어떨까요

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

Choose a reason for hiding this comment

The 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) {

Choose a reason for hiding this comment

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

해당 메서드의 반환값은 계산을 시작할 시작 인덱스인데 이름이 parseCustom 이 맞을까요?

if (input.length() < 5) {
return 0;
}
Comment on lines +20 to +22

Choose a reason for hiding this comment

The 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())) {

Choose a reason for hiding this comment

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

StringBuilderisEmpty() 메서드가 없습니다. string 으로 변환하여 사용하거나 value.length() > 0와 같이 변경해주세요.

result = Integer.parseInt(value.toString());
}
value.setLength(0);
return result;
}

public void addValue(char c) {
value.append(c);
}
}