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 all 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
20 changes: 20 additions & 0 deletions src/main/java/calculator/CalculatorMain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package calculator;

import calculator.controller.CalculatorController;
import calculator.model.Operator;
import calculator.util.FormulaParser;
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();
FormulaParser formulaParser = new FormulaParser();

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

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

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

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

public void calculatorRun() {
String formula = inputView.formulaInput();
String[] values = formulaParser.parse(formula);

Choose a reason for hiding this comment

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

values는 어떤 용도로 사용되는 변수인가요

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

public class Operator {

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) {
String symbol = values[i];
int nextNumber = Integer.parseInt(values[i + 1]);
OperatorType operatorType = OperatorType.fromString(symbol);
result = operatorType.apply(result, nextNumber);
Comment on lines +11 to +12

Choose a reason for hiding this comment

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

static 메소드를 사용해야할 이유가 있을까요?

}

return result;
}
}
45 changes: 45 additions & 0 deletions src/main/java/calculator/model/OperatorType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package calculator.model;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;

public enum OperatorType {
ADDITION("+", (a, b) -> a + b),
SUBTRACTION("-", (a, b) -> a - b),
MULTIPLICATION("*", (a, b) -> a * b),
DIVISION("/", (a, b) -> {
if (b == 0) {
throw new ArithmeticException("0으로 나눌 수 없습니다.");
}
return a / b;
});

private static final Map<String, OperatorType> SYMBOL_MAP = new HashMap<>();

Choose a reason for hiding this comment

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

이걸 필드로 선언한 이유가 있을까요?
데이터 주도 설계가 아닌 도메인 주도 설계를 공부해보세


static {
for (OperatorType type : values()) {
SYMBOL_MAP.put(type.symbol, type);
}
}

private final String symbol;
private final BiFunction<Integer, Integer, Integer> operation;

OperatorType(String symbol, BiFunction<Integer, Integer, Integer> operation) {
this.symbol = symbol;
this.operation = operation;
}

public static OperatorType fromString(String symbol) {
OperatorType type = SYMBOL_MAP.get(symbol);
if (type == null) {
throw new IllegalArgumentException("잘못된 연산자입니다.");
}
return type;
}

public int apply(int a, int b) {
return operation.apply(a, b);
}
}
7 changes: 7 additions & 0 deletions src/main/java/calculator/util/FormulaParser.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package calculator.util;

public class FormulaParser {
public String[] parse(String input) {
return input.split(" ");
}
}
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();
}
}
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);
}
}