-
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 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 |
|---|---|---|
| @@ -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; | ||
|
|
||
| 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; | ||
| } | ||
|
|
||
| private void validation(String[] formattedInput) { | ||
| if (formattedInput.length%2 == 0) throw new RuntimeException("올바른 식을 완성해주세요."); | ||
| } | ||
|
Comment on lines
+47
to
+49
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 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]); | ||
| } | ||
| } | ||
| } | ||
| 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); | ||
| } | ||
| } |
| 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() { | ||
|
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. 이 메소드 또한 이전의 코멘트처럼 get으로 시작하는 메소드이네요. input으로 시작하는 이름으로 지으면 헷갈리는 일이 덜 발생할 것이라고 생각해요. |
||
| String line = Input.inputExpression(); | ||
| 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 printOutputError(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.
이렇게 인스턴스 변수를 초기화하는 것을 명시적 초기화라고 합니다. 생성자를 통해 인스턴스 변수를 초기화 할 수 있습니다. 인스턴스 변수 초기화의 방법은 4가지가 있고, 각 초기화가 실행되는 시점이 다릅니다. 이를 알고 있으면 좋을 것 같아요.