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
20 changes: 20 additions & 0 deletions src/main/java/Input.java
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){
Copy link
Author

Choose a reason for hiding this comment

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

한 클래스 내에서만 사용하는 메소드는 접근제어자를 private으로 변경해주세요.
또한 연산 입력의 오류의 케이스가 더 있을 것 같은데 한번 생각해보면 좋을 것 같아요.

if(splitInput.length % 2 == 0) throw new IllegalArgumentException("잘못된 연산입력입니다.");
}
}
9 changes: 9 additions & 0 deletions src/main/java/Main.java
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
Copy link
Author

Choose a reason for hiding this comment

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

굳이 분리하지 않아도 괜찮지 않을까요?

}
5 changes: 5 additions & 0 deletions src/main/java/Output.java
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);
}
}
47 changes: 47 additions & 0 deletions src/main/java/StringCalculator.java
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){
Copy link
Author

Choose a reason for hiding this comment

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

메소드 네임에는 타입이 들어가는 것을 추천드리지 않아요. 간단하게 calculate로 바꿔도 괜찮을 것 같습니다.

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
Copy link
Author

Choose a reason for hiding this comment

The 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
Copy link
Author

Choose a reason for hiding this comment

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

👍

throw new RuntimeException();
Copy link
Author

Choose a reason for hiding this comment

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

어떤 케이스에 대한 예외인가요?

}



}
Binary file added src/main/java/out/production/main/Input.class
Binary file not shown.
Binary file added src/main/java/out/production/main/Main.class
Binary file not shown.
Binary file added src/main/java/out/production/main/Output.class
Binary file not shown.
Binary file not shown.
Empty file.