-
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 9 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,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(); | ||
} | ||
} |
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(); | ||
int result = operator.calculate(values); | ||
outputView.resultOutput(result); | ||
} | ||
} |
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; | ||
} | ||
} |
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) { | ||
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) { | ||
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; | ||
} | ||
} |
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 "+": | ||
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 메소드여야만 하는 이유가 있나요? 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. 객체를 변경하지 않아서 인스턴스를 생성하지 않아도 된다고 생각했고 |
||
return ADDITION; | ||
case "-": | ||
return SUBTRACT; | ||
case "*": | ||
return MULTIPLY; | ||
case "/": | ||
return DIVIDE; | ||
default: | ||
throw new IllegalArgumentException("잘못된 연산자입니다."); | ||
} | ||
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. 지정된 연산자를 제외한 다른 기호를 입력했을 경우에 출력하도록 했지만 |
||
} | ||
} |
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(" "); | ||
} | ||
} | ||
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. FormulaParser 클래스에 문자열을 나누는 역할을 분할했습니다. |
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.
이 변수가 어떤 역할을 하는 변수인지 변수명만 보고 알 수 있게 만들어주세요
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.
calculatorRun으로 수정했습니다.