-
Notifications
You must be signed in to change notification settings - Fork 3
PR for code review #6
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: Hyojung-lee
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,20 @@ | ||
| import java.util.Scanner; | ||
|
|
||
| public class Input { | ||
|
|
||
| public static String[] splitInput(){ | ||
| System.out.println("계산식 입력 : "); | ||
| Scanner sc = new Scanner(System.in); | ||
| String input = sc.nextLine(); | ||
|
|
||
| String[] splitString = input.split(" "); | ||
|
|
||
| checkInput(splitString); | ||
|
|
||
| return splitString; | ||
| } | ||
|
|
||
| public static void checkInput(String[] splitInput){ | ||
| if(splitInput.length % 2 == 0) throw new IllegalArgumentException("잘못된 연산입력입니다."); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| public class Main { | ||
| public static void main(String[] args){ | ||
| calculate(); | ||
| } | ||
| public static void calculate(){ | ||
| StringCalculator stringCalculator = new StringCalculator(); | ||
| Output.printOutput(stringCalculator.calculateStr(Input.splitInput())); | ||
| } | ||
|
Comment on lines
+2
to
+8
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. 굳이 분리하지 않아도 괜찮지 않을까요? |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| public class Output { | ||
| public static void printOutput(int num){ | ||
| System.out.println(num); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| import java.util.*; | ||
|
|
||
| public class StringCalculator { | ||
|
|
||
| public int calculateStr(String[] inputSplit){ | ||
|
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. 메소드 네임에는 타입이 들어가는 것을 추천드리지 않아요. 간단하게 |
||
| int number = Integer.parseInt(inputSplit[0]); | ||
| for(int i = 0; i < inputSplit.length - 2; i += 2){ | ||
| number = calculate(number, inputSplit[i+1].charAt(0), Integer.parseInt(inputSplit[i+2])); | ||
| } | ||
| return number; | ||
| } | ||
|
|
||
| public int calculate(int firstFactor, char operator, int secondFactor) { | ||
| if (operator == '+') | ||
| return add(firstFactor,secondFactor); | ||
| if (operator == '-') | ||
| return subtract(firstFactor,secondFactor); | ||
| if (operator == '*') | ||
| return multiply(firstFactor,secondFactor); | ||
| if (operator == '/') | ||
| return divide(firstFactor,secondFactor); | ||
|
|
||
| throw new RuntimeException("올바르지 않은 연산자"); | ||
| } | ||
|
|
||
|
|
||
| public int add(int num1, int num2){ | ||
| return num1 + num2; | ||
| } | ||
| public int subtract(int num1, int num2){ | ||
| return num1 - num2; | ||
| } | ||
| public int multiply(int num1, int num2){ | ||
| return num1 * num2; | ||
| } | ||
| public int divide(int num1, int num2){ | ||
|
Comment on lines
+13
to
+36
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. 접근제어자 변경해주세요 |
||
| try { | ||
| return num1 / num2; | ||
| } catch(ArithmeticException e){ | ||
| System.out.println("0 으로 나눌수 없음 : " + num1 + "/" + num2); | ||
| } | ||
|
Comment on lines
+39
to
+41
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. 👍 |
||
| throw new RuntimeException(); | ||
|
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. 어떤 케이스에 대한 예외인가요? |
||
| } | ||
|
|
||
|
|
||
|
|
||
| } | ||
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으로 변경해주세요.또한 연산 입력의 오류의 케이스가 더 있을 것 같은데 한번 생각해보면 좋을 것 같아요.