Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
d2ae7b4
feat(Calculator) : 계산기 실행 코드
kangsangmoon Aug 27, 2024
33e2d0b
ADD(Calculator) : 계산기 계산 로직 제어 코드
kangsangmoon Aug 30, 2024
261ce04
ADD(Calculator) : 계산기 수식 입력 처리 코드
kangsangmoon Aug 30, 2024
c44c0c4
refactor(Calculator) : 변수명 수정
kangsangmoon Aug 30, 2024
30ec2e9
ADD(Calculator) : 계산기 enum 코드
kangsangmoon Aug 30, 2024
dc2d1a5
ADD(Calculator) : 계산기 결과 출력 코드
kangsangmoon Aug 30, 2024
684c9b9
refactor(Calculator) : OperatorType Enum 추가 및 연산 처리 수정
kangsangmoon Aug 30, 2024
7e09213
ADD(Calculator) : 계산기 실행 코드
kangsangmoon Aug 30, 2024
af42101
DELETE(Calculator) : 코드 수정을 통해 삭제
kangsangmoon Aug 30, 2024
9d312d6
refactor(Calculator) : CalculatorController에 FormulaParser 추가
kangsangmoon Aug 30, 2024
16f9805
refactor(Calculator) : FormulaParser 인스턴스 추가
kangsangmoon Aug 30, 2024
352bab3
feat(Calculator) : 수식 분할을 위한 코드 추가
kangsangmoon Aug 30, 2024
56aac5c
refactor(Calculator) : 문자열을 반환하도록 수정
kangsangmoon Aug 30, 2024
b32d31d
refactor(Calculator) : 중복된 기능을 제거
kangsangmoon Aug 30, 2024
7a44f52
refactor(Calculator) : Operations 의존성 제거
kangsangmoon Aug 30, 2024
926d269
refactor(Calculator) : OperatorType enum 로직 수정
kangsangmoon Aug 30, 2024
f8fe96c
refactor(Calculator) : 메소드명 변경
kangsangmoon Aug 30, 2024
2104618
refactor(Calculator) : 메소드호출 코드 변경
kangsangmoon Aug 30, 2024
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
11 changes: 11 additions & 0 deletions src/main/java/Calculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import java.util.Scanner;


public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String[] values = Input.readInput(scanner);
int result = Operator.calculate(values);
Output.printResult(result);
}
}
8 changes: 8 additions & 0 deletions src/main/java/Input.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import java.util.Scanner;

public class Input {
public static String[] readInput(Scanner scanner) {
System.out.print("수식을 입력하세요: ");
return scanner.nextLine().split(" ");
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. Input은 어떤 역할을 하는 클래스인가요?
  2. readInput 메소드가 static인 이유가 뭔가요
  3. Scanner 객체를 매개변수로 받는 이유가 뭔가요

Copy link
Author

Choose a reason for hiding this comment

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

  1. 사용자의 입력을 받아오는 역할을 하는 클래스입니다.
  2. 객체를 생성하지 않고 클래스 이름으로 직접 호출 할 수 있게 하려고 사용했었습니다.
  3. Scanner 객체를 사용하는 다른 클래스가 있을 경우 Scanner 객체를 사용할 수 있게 하려고 사용했었습니다.

20 changes: 20 additions & 0 deletions src/main/java/Operations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class Operations {
public int add(int a, int b) {
return a + b;
}

public int subtract(int a, int b) {
return a - b;
}

public int multiply(int a, int b) {
return a * b;
}

public int divide(int a, int b) {
if (b == 0) {
throw new ArithmeticException("0으로 나눌 수 없습니다.");
}
return a / b;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Enum을 사용해보세요

Copy link
Author

Choose a reason for hiding this comment

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

Enum 추가하여 수정했습니다.

30 changes: 30 additions & 0 deletions src/main/java/Operator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
public class Operator {
public static int calculate(String[] values) {
Copy link
Contributor

Choose a reason for hiding this comment

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

매개변수의 values는 어떤 값을 받아오는 건가요

Copy link
Author

Choose a reason for hiding this comment

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

사용자가 입력한 수식을 공백을 기준으로 분할 하여 입력한 수식을 받아옵니다.

Operations operations = new Operations();
int result = Integer.parseInt(values[0]);

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

switch (operator) {
case "+":
result = operations.add(result, nextNumber);
break;
case "-":
result = operations.subtract(result, nextNumber);
break;
case "*":
result = operations.multiply(result, nextNumber);
break;
case "/":
result = operations.divide(result, nextNumber);
break;
default:
System.out.println("잘못된 연산자입니다.");
return 0;
}
}
return result;
}
}
6 changes: 6 additions & 0 deletions src/main/java/Output.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
public class Output {

public static void printResult(int result) {
Copy link
Contributor

Choose a reason for hiding this comment

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

print는 예약어입니다. 예약어를 사용하지 않는 메소드명을 사용해주세요.

Copy link
Author

Choose a reason for hiding this comment

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

resultOutput으로 메소드명을 변경했습니다.

System.out.println("결과: " + result);
}
}