-
Notifications
You must be signed in to change notification settings - Fork 3
🎨 calculator 코드 구현 not w/ testcode #5
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: hansol
Are you sure you want to change the base?
Changes from 1 commit
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 | ||||
|---|---|---|---|---|---|---|
| @@ -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; | ||||||
|
|
||||||
| List<String> numberList = new ArrayList<>(); | ||||||
| List<String> operationList = new ArrayList<>(); | ||||||
| int result = 0; | ||||||
|
||||||
|
|
||||||
| 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
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.
Author
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. Calculator 내에 main()을 만들라는 말인가여? 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. main 메소드의 길이를 신경쓰지 않으셔도 된다는 말이에요. 제 개인적인 의견이지만 main에서 이 프로그램이 어떻게 흘러가는지를 알 수 있게끔 구현해도 괜찮다고 생각합니다. |
||||||
| 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[]) { | ||||||
|
||||||
| public void validation(String line[]) { | |
| public void validation(String[] line) { |
Array의 선언은 일반적으로 타입뒤에 대괄호를 붙입니다. 아래도 동일하게 변경해주세요.
Outdated
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.
조건문은 어떤 것을 의미할까요? 이 메소드만 보았을 때에 입력받은 line의 길이가 짝수이면 올바른 식이 아니라는 말로 받아들일 수 있는데, line은 어떤 것을 의미하는지 모를 수 있겠네요.
| 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 = " "; | ||||||
|
||||||
| public static final String BLANK = " "; | |
| private static final String BLANK = " "; |
접근제어자를 변경해주세요.
Outdated
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.
이처럼 'get'으로 시작하는 메소드는 클래스의 필드값을 반환해주는 로직을 수행하는 것을 권장합니다. 메소드명을 변경해주세요. 어떤 것을 입력받는지에 대해 알려주면 좋을 것 같습니다.
| 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() { | ||
|
||
| String line = Input.getInput(); | ||
| String[] splittedLine = Input.split(line); | ||
| return splittedLine; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| 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("출력 에러"); | ||
|
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. 메소드명으로 에러를 출력함을 알 수 있는데, 어떤 에러인지 알 수 없습니다. 메소드명을 변경해주세요. |
||
| e.printStackTrace(); | ||
| } | ||
|
|
||
| } | ||
This file was deleted.
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.
private는 접근제어자입니다. 모든 선언에서 접근제어자는 맨 처음으로 나와주어야 합니다.