Skip to content
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

(이주연) StringCalculator 리뷰 요청드립니다. #29

Open
wants to merge 1 commit into
base: jooyeon/main
Choose a base branch
from
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
33 changes: 33 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
public class Calculator {
public double calculate(String expression) {
String[] tokens = expression.split(" ");
double currentResult = Double.parseDouble(tokens[0]);

for (int i = 1; i < tokens.length; i += 2) {
String operator = tokens[i];
double number = Double.parseDouble(tokens[i + 1]);

switch (operator) {
case "+":
currentResult += number;
break;
case "-":
currentResult -= number;
break;
case "*":
currentResult *= number;
break;
case "/":
if (number == 0) {
throw new ArithmeticException("0으로 나눌 수 없습니다.");
}
Comment on lines +10 to +23
Copy link
Author

Choose a reason for hiding this comment

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

Enum을 사용해서 switch문을 사용하지 않는 방향으로 코드를 작성해봅시다

currentResult /= number;
break;
default:
System.out.println("지원하지 않는 연산자입니다: " + operator);
Copy link
Author

Choose a reason for hiding this comment

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

Calculator 클래스의 책임은 무엇인가요.
한 클래스에 여러 개의 책임이 있는 것 같습니다.
SCP에 대해서 공부해보고 코드를 작성해주세요

return 0;
}
}
return currentResult;
}
}
19 changes: 18 additions & 1 deletion src/main/java/Main.java
Copy link
Author

Choose a reason for hiding this comment

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

Main 클래스에 너무 많은 책임이 있습니다.
SCP에 대해서 공부 해보고 코드를 작성해주세요

  • 콘솔에 프린트해주는 것도 하나의 책임으로 봅니다

Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
System.out.println("1 + 1 = 2");
Scanner scanner = new Scanner(System.in);
System.out.println("계산할 수식을 입력하세요 (예: 30 + 20 / 2 * 4):");
String expression = scanner.nextLine();

Validator validator = new Validator();
if (validator.isValid(expression)) {
Calculator calculator = new Calculator();
try {
double result = calculator.calculate(expression);
System.out.println("결과: " + result);
} catch (ArithmeticException e) {
System.out.println("계산 중 오류가 발생했습니다: " + e.getMessage());
}
} else {
System.out.println("잘못된 수식 형식입니다. 다시 입력해주세요.");
}
}
}
34 changes: 34 additions & 0 deletions src/main/java/Validator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
public class Validator {
public boolean isValid(String expression) {
String[] tokens = expression.split(" ");

if (tokens.length % 2 == 0) {
return false;
}

if (!isNumeric(tokens[0])) {
return false;
}

for (int i = 1; i < tokens.length; i += 2) {
if (!isOperator(tokens[i]) || !isNumeric(tokens[i + 1])) {
return false;
}
}

return true;
}
Comment on lines +2 to +20
Copy link
Author

Choose a reason for hiding this comment

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

한 메소드가 많은 역할을 해주는 것 같습니다.
각각의 if문을 여러 메소드로 나누어보세요.


private boolean isNumeric(String str) {
try {
Double.parseDouble(str);
return true;
} catch (NumberFormatException e) {
return false;
}
}

private boolean isOperator(String str) {
return str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/");
}
}