-
Notifications
You must be signed in to change notification settings - Fork 8
(강상문) 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
base: sangmoon/main
Are you sure you want to change the base?
Changes from all commits
d2ae7b4
33e2d0b
261ce04
c44c0c4
30ec2e9
dc2d1a5
684c9b9
7e09213
af42101
9d312d6
16f9805
352bab3
56aac5c
b32d31d
7a44f52
926d269
f8fe96c
2104618
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,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(); | ||
} | ||
} |
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); | ||
int result = operator.calculate(values); | ||
outputView.resultOutput(result); | ||
} | ||
} |
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) { | ||
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. 어떤 매개변수를 받아오는 건가요. 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. 현재 푸쉬한 코드에 존재하지 않는 코드입니다. 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. 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
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. static 메소드를 사용해야할 이유가 있을까요? |
||
} | ||
|
||
return result; | ||
} | ||
} |
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<>(); | ||
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. 이걸 필드로 선언한 이유가 있을까요? |
||
|
||
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); | ||
} | ||
} |
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(" "); | ||
} | ||
} |
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(); | ||
} | ||
} |
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); | ||
} | ||
} |
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.
values는 어떤 용도로 사용되는 변수인가요