-
Notifications
You must be signed in to change notification settings - Fork 8
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
base: jooyeon/main
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,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으로 나눌 수 없습니다."); | ||
} | ||
currentResult /= number; | ||
break; | ||
default: | ||
System.out.println("지원하지 않는 연산자입니다: " + operator); | ||
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 클래스의 책임은 무엇인가요. |
||
return 0; | ||
} | ||
} | ||
return currentResult; | ||
} | ||
} |
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 클래스에 너무 많은 책임이 있습니다.
|
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("잘못된 수식 형식입니다. 다시 입력해주세요."); | ||
} | ||
} | ||
} |
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
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 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("/"); | ||
} | ||
} |
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.
Enum을 사용해서 switch문을 사용하지 않는 방향으로 코드를 작성해봅시다