Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
64 changes: 64 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import java.util.ArrayList;
import java.util.List;

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

Choose a reason for hiding this comment

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

Suggested change
static private final String ADD_OPERATION = "+";
static private final String SUBTRACT_OPERATION = "-";
static private final String MULTIPLY_OPERATION = "*";
static private final String DIVIDE_OPERATION = "/";
static private final int NUMBER_INDEX = 0;
static private final int OPERATION_INDEX = 1;
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<>();
List<String> operationList = new ArrayList<>();
int result = 0;

Choose a reason for hiding this comment

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

Calculator의 필드변수들입니다. 접근제어자를 작성해주지 않으면 default라는 접근제어자가 기본값으로 적용됩니다. default는 같은 패키지 내의 다른 클래스가 사용할 수 있습니다. 다른 클래스에서 접근하지 못하게 private으로 변경해주세요.


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;
}

public void validation(String line[]) {

Choose a reason for hiding this comment

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

Suggested change
public void validation(String line[]) {
public void validation(String[] line) {

Array의 선언은 일반적으로 타입뒤에 대괄호를 붙입니다. 아래도 동일하게 변경해주세요.

if (line.length%2 == 0) throw new RuntimeException("올바른 식을 완성해주세요.");

Choose a reason for hiding this comment

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

조건문은 어떤 것을 의미할까요? 이 메소드만 보았을 때에 입력받은 line의 길이가 짝수이면 올바른 식이 아니라는 말로 받아들일 수 있는데, line은 어떤 것을 의미하는지 모를 수 있겠네요.

}

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

public void addOperationList(String line[]) {
for (int i = OPERATION_INDEX; i < line.length; i += 2) {
operationList.add(line[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 {
public static final String BLANK = " ";

Choose a reason for hiding this comment

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

Suggested change
public static final String BLANK = " ";
private static final String BLANK = " ";

접근제어자를 변경해주세요.


public static String getInput() {

Choose a reason for hiding this comment

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

이처럼 'get'으로 시작하는 메소드는 클래스의 필드값을 반환해주는 로직을 수행하는 것을 권장합니다. 메소드명을 변경해주세요. 어떤 것을 입력받는지에 대해 알려주면 좋을 것 같습니다.

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 = getStream();
try {
Output.printOutput(calculator.execute(lineForCalculate));
}
catch (Exception e){
Output.printError(e);
runCalculate();
}
}

public static String[] getStream() {

Choose a reason for hiding this comment

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

메소드명의 의미 전달이 불확실합니다. 메소드명을 변경해주세요.

String line = Input.getInput();
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 printError(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.