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
62 changes: 62 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import java.util.ArrayList;
import java.util.List;

public class Calculator {
private static final String ADD_OPERATION = "+";
private static final String SUBTRACT_OPERATION = "-";
private static final String MULTIPLY_OPERATION = "*";
private static final String DIVIDE_OPERATION = "/";
private static final int NUMBER_INDEX = 0;
private static final int OPERATION_INDEX = 1;

private List<String> numberList = new ArrayList<>();
private List<String> operationList = new ArrayList<>();
private int result = 0;
Comment on lines +12 to +14

Choose a reason for hiding this comment

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

이렇게 인스턴스 변수를 초기화하는 것을 명시적 초기화라고 합니다. 생성자를 통해 인스턴스 변수를 초기화 할 수 있습니다. 인스턴스 변수 초기화의 방법은 4가지가 있고, 각 초기화가 실행되는 시점이 다릅니다. 이를 알고 있으면 좋을 것 같아요.


public int operation(int firstNumber, int secondNumber, String operation) {
switch (operation) {
case ADD_OPERATION:
result = firstNumber + secondNumber;
break;
case SUBTRACT_OPERATION:
result = firstNumber - secondNumber;
break;
case MULTIPLY_OPERATION:
result = firstNumber * secondNumber;
break;
case DIVIDE_OPERATION:
result = firstNumber / secondNumber;
break;
default:
throw new RuntimeException("부적합한 연산자가 입력되었습니다.");
}
return result;
}

public int execute(String[] line) {
validation(line);
addNumberList(line);
addOperationList(line);
Comment on lines +37 to +39

Choose a reason for hiding this comment

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

Calculator클래스의 메소드 중 다른 클래스에서 사용하는 메소드는 execute() 단 하나입니다.
그래서 이 메소드들의 접근제어자를 private으로 변경하는게 좋을 수 있습니다.
단, private 메소드들은 테스트를 하기 어려워집니다. 테스트 클래스에서 바로 호출할 수 없기 때문이죠.
execute()와 같은 메소드가 존재할 경우에 발생하는 문제는 이렇습니다. 이 메소드가 수행하는 로직을 main()에서 진행하는 것을 권장합니다.

Copy link
Author

Choose a reason for hiding this comment

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

Calculator 내에 main()을 만들라는 말인가여?

Choose a reason for hiding this comment

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

main 메소드의 길이를 신경쓰지 않으셔도 된다는 말이에요. 제 개인적인 의견이지만 main에서 이 프로그램이 어떻게 흘러가는지를 알 수 있게끔 구현해도 괜찮다고 생각합니다.
추가적으로 공식에 대한 예외처리를 과연 Calculator가 해야하는 지에 대한 의문도 드네요.

result = Integer.parseInt(numberList.get(0));
for (int i = 0; i < operationList.size(); i++) {
result = operation(result, Integer.parseInt(numberList.get(i + 1)), operationList.get(i));
}
return result;
}

private void validation(String[] formattedInput) {
if (formattedInput.length%2 == 0) throw new RuntimeException("올바른 식을 완성해주세요.");
}
Comment on lines +47 to +49

Choose a reason for hiding this comment

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

개발자가 편하면 사용자가 불편하다.라고 생각해요. 올바른 식은 어떤 것일까요? 숫자 두 개를 연속으로 입력한 식은 올바른 식일까요? 연산자 두 개를 연속으로 입력한 식은 올바른 식일까요? 예외에 대해 조금 더 깊게 생각해보는 것도 중요하다고 생각합니다.


private void addNumberList(String[] formattedInput) {
for (int i = NUMBER_INDEX; i < formattedInput.length; i += 2) {
numberList.add(formattedInput[i]);
}
}

private void addOperationList(String[] formattedInput) {
for (int i = OPERATION_INDEX; i < formattedInput.length; i += 2) {
operationList.add(formattedInput[i]);
}
}
}
13 changes: 13 additions & 0 deletions src/main/java/Input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import java.util.Scanner;

public class Input {
private static final String BLANK = " ";

public static String inputExpression() {
Scanner scanner = new Scanner(System.in);
return scanner.nextLine();
}
public static String[] split(String line) {
return line.split(BLANK);
}
}
28 changes: 28 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@


public class Main {

public static void main(String[] args) {
runCalculate();
}

public static void runCalculate() {
Calculator calculator = new Calculator();
String[] lineForCalculate = getFormattedInput();
try {
Output.printOutput(calculator.execute(lineForCalculate));
}
catch (Exception e){
Output.printOutputError(e);
runCalculate();
}
}

public static String[] getFormattedInput() {

Choose a reason for hiding this comment

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

이 메소드 또한 이전의 코멘트처럼 get으로 시작하는 메소드이네요. input으로 시작하는 이름으로 지으면 헷갈리는 일이 덜 발생할 것이라고 생각해요.

String line = Input.inputExpression();
String[] splittedLine = Input.split(line);
return splittedLine;
}

}

10 changes: 10 additions & 0 deletions src/main/java/Output.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class Output {
public static void printOutput(int result) {
System.out.println(result);
}
public static void printOutputError(Exception e) {
System.out.println("출력 에러");

Choose a reason for hiding this comment

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

메소드명으로 에러를 출력함을 알 수 있는데, 어떤 에러인지 알 수 없습니다. 메소드명을 변경해주세요.

e.printStackTrace();
}

}
51 changes: 0 additions & 51 deletions src/main/java/main.java

This file was deleted.