Skip to content

(강상문) StringCalculator 리뷰 요청드립니다. #27

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

Open
wants to merge 18 commits into
base: sangmoon/main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
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
18 changes: 18 additions & 0 deletions src/main/java/calculator/CalculatorMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package calculator;

import calculator.controller.CalculatorController;
import calculator.model.Operator;
import calculator.view.InputView;
import calculator.view.OutputView;


public class CalculatorMain {
public static void main(String[] args) {
Operator operator = new Operator();
InputView inputView = new InputView();
OutputView outputView = new OutputView();

CalculatorController controller = new CalculatorController(operator, inputView, outputView);
controller.run();
}
}
23 changes: 23 additions & 0 deletions src/main/java/calculator/controller/CalculatorController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package calculator.controller;

import calculator.model.Operator;
import calculator.view.InputView;
import calculator.view.OutputView;

public class CalculatorController {
private final Operator operator;
private final InputView inputView;
private final OutputView outputView;

public CalculatorController(Operator operator, InputView inputView, OutputView outputView) {
this.operator = operator;
this.inputView = inputView;
this.outputView = outputView;
}

public void run() {
String[] values = inputView.formulaInput();

Choose a reason for hiding this comment

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

이 변수가 어떤 역할을 하는 변수인지 변수명만 보고 알 수 있게 만들어주세요

Copy link
Author

Choose a reason for hiding this comment

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

calculatorRun으로 수정했습니다.

int result = operator.calculate(values);
outputView.resultOutput(result);
}
}
22 changes: 22 additions & 0 deletions src/main/java/calculator/model/Operations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package calculator.model;

public class Operations {
public int addition(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;
}
}
34 changes: 34 additions & 0 deletions src/main/java/calculator/model/Operator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package calculator.model;

public class Operator {
private final Operations operations;

public Operator() {
this.operations = new Operations();
}

public int calculate(String[] values) {

Choose a reason for hiding this comment

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

어떤 매개변수를 받아오는 건가요.
매개변수가 메소드 내에서 어떤 역할을 하는지 알 수 있도록 지어주세요

Copy link
Author

Choose a reason for hiding this comment

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

현재 푸쉬한 코드에 존재하지 않는 코드입니다.

Choose a reason for hiding this comment

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

values 말한 거에요

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

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

switch (operatorType) {
case ADDITION:
result = operations.addition(result, nextNumber);
break;
case SUBTRACT:
result = operations.subtract(result, nextNumber);
break;
case MULTIPLY:
result = operations.multiply(result, nextNumber);
break;
case DIVIDE:
result = operations.divide(result, nextNumber);
break;
}
}
return result;
}
}
20 changes: 20 additions & 0 deletions src/main/java/calculator/model/OperatorType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package calculator.model;

public enum OperatorType {
ADDITION, SUBTRACT, MULTIPLY, DIVIDE;

public static OperatorType fromString(String symbol) {
switch (symbol) {
case "+":

Choose a reason for hiding this comment

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

이 메소드가 static 메소드여야만 하는 이유가 있나요?

Copy link
Author

Choose a reason for hiding this comment

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

객체를 변경하지 않아서 인스턴스를 생성하지 않아도 된다고 생각했고
static메소드로 선언하면 인스턴스 생성없이 호출 할 수 있어서 사용했습니다.

return ADDITION;
case "-":
return SUBTRACT;
case "*":
return MULTIPLY;
case "/":
return DIVIDE;
default:
throw new IllegalArgumentException("잘못된 연산자입니다.");
}

Choose a reason for hiding this comment

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

이렇게 문자열을 리턴해줄 필요가 있을까요?

Copy link
Author

Choose a reason for hiding this comment

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

지정된 연산자를 제외한 다른 기호를 입력했을 경우에 출력하도록 했지만
계산기의 경우 따로 없어도 될 것 같습니다.

}
}
11 changes: 11 additions & 0 deletions src/main/java/calculator/view/InputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package calculator.view;

import java.util.Scanner;

public class InputView {
public String[] formulaInput() {
Scanner scanner = new Scanner(System.in);
System.out.print("수식을 입력하세요 : ");
return scanner.nextLine().split(" ");
}
}

Choose a reason for hiding this comment

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

클래스에 역할이 두 가지 존재합니다

  1. 문자열을 받는다
  2. 문자열을 나눈다

클래스의 책임과 역할에 대해서 생각해봅시다

Copy link
Author

Choose a reason for hiding this comment

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

FormulaParser 클래스에 문자열을 나누는 역할을 분할했습니다.

8 changes: 8 additions & 0 deletions src/main/java/calculator/view/OutputView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package calculator.view;

public class OutputView {

public void resultOutput(int result) {
System.out.print("결과 : " + result);
}
}