Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
8 changes: 8 additions & 0 deletions src/stringCalculator/FormulaSplitter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package stringCalculator;

public class FormulaSplitter {

public String[] split(String formula) {
return formula.split(" ");
}
Comment on lines +5 to +10
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

formula가 null이나 빈값이 들어오면 어떻게 되나요?

Copy link
Copy Markdown
Owner Author

@kdongd kdongd Jan 14, 2026

Choose a reason for hiding this comment

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

null값이 들어오면 NullPointerException이 발생
빈값이 들어오면 NumberFormatException이 발생합니다.
코드 수정 하겠습니다!

}
12 changes: 12 additions & 0 deletions src/stringCalculator/InputProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package stringCalculator;

import java.util.Scanner;

public class InputProcessor {
private final Scanner scanner = new Scanner(System.in);

public String input() {
System.out.print("계산식을 입력하세요(예:2 + 3 * 4 / 5): ");
return scanner.nextLine();
}
}
48 changes: 48 additions & 0 deletions src/stringCalculator/Operator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package stringCalculator;

public enum Operator {
PLUS("+") {
@Override
int operation(int a, int b) {
return a + b;
}
},
MINUS("-"){
@Override
int operation(int a, int b) {
return a - b;
}
},
MULTIPLY("*"){
@Override
int operation(int a, int b) {
return a * b;
}
},
DIVIDE("/"){
@Override
int operation(int a, int b) {
if (b == 0) {
throw new IllegalArgumentException("0으로 나눌 수 없습니다.");
}
return a / b;
}
Comment thread
kdongd marked this conversation as resolved.
};

private final String message;

Operator(String message) {
this.message = message;
}
Comment thread
kdongd marked this conversation as resolved.

abstract int operation(int a, int b);

public static Operator conversion(String message) { //입력받은 연산자로 enum 찾기
for (Operator op : Operator.values()) {
if (op.message.equals(message)) {
return op;
}
}
throw new IllegalArgumentException("지원하지 않는 연산자: " + message);
}
}
8 changes: 8 additions & 0 deletions src/stringCalculator/OutputProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package stringCalculator;

public class OutputProcessor {

public void output(int result) {
System.out.println("결과: " + result);
}
}
29 changes: 29 additions & 0 deletions src/stringCalculator/StringCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package stringCalculator;



public class StringCalculator {

public int calculate(String[] values) {
try {

int result;
int number = Integer.parseInt(values[0]);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

변수를 2개로 나눈 이유가 있을까요?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

number는 for문에서 중간 계산값을 저장, result는 number를 좀 더 쉽게 보여주기 위해 사용했습니다.
다시 생각해보니 result는 불필요한 변수인 것 같아서 바로 number를 return 하겠습니다.


for (int i = 1; i < values.length; i += 2) {
Operator operator = Operator.conversion(values[i]);
int nextNumber = Integer.parseInt(values[i + 1]);

number = operator.operation(number, nextNumber);
}
result = number;
return result;
} catch (NumberFormatException e) {
throw new IllegalArgumentException("숫자가 아닌 값이 포함되어 있습니다." , e);
} catch (ArrayIndexOutOfBoundsException e) {
throw new IllegalArgumentException("잘못된 계산식입니다. 연산자와 숫자를 올바르게 입력하시오." , e);
}
}
}


16 changes: 16 additions & 0 deletions src/stringCalculator/StringCalculatorMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package stringCalculator;

public class StringCalculatorMain {
public static void main(String[] args) {
StringCalculator stringCalculator = new StringCalculator();
InputProcessor inputProcessor = new InputProcessor();
OutputProcessor outputProcessor = new OutputProcessor();
FormulaSplitter formulaSplitter = new FormulaSplitter();

String value = inputProcessor.input();
String[] values = formulaSplitter.split(value);

int result = stringCalculator.calculate(values);
outputProcessor.output(result);
}
}