-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathCalculatorApp.java
More file actions
62 lines (54 loc) · 1.62 KB
/
CalculatorApp.java
File metadata and controls
62 lines (54 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package co.programmers.domain;
import co.programmers.exception.ExceptionMessage;
import co.programmers.repository.Repository;
import co.programmers.view.CalculatorOutputView;
import co.programmers.view.InputView;
import co.programmers.view.OutputView;
public class CalculatorApp {
private final InputView inputView;
private final OutputView outputView;
private final Repository repository;
public CalculatorApp(InputView inputView, OutputView outputView, Repository repository) {
this.inputView = inputView;
this.outputView = outputView;
this.repository = repository;
}
public void run() {
UserMenu userMenu;
do {
CalculatorOutputView.printMenuChoiceGuide();
userMenu = UserMenu.get(inputView.inputUserMenu());
executeSelectedMenu(userMenu);
} while (userMenu != UserMenu.TERMINATE);
}
private void executeSelectedMenu(UserMenu userMenu) {
switch (userMenu) {
case INQUIRY:
inquiry();
break;
case CALCULATE:
calculate();
break;
case TERMINATE:
break;
default:
outputView.printMessage(ExceptionMessage.INVALID_INPUT);
break;
}
}
public void inquiry() {
outputView.printCalculationHistory(repository.read());
}
public void calculate() {
try {
CalculatorOutputView.printCalculationGuide();
Expression expression = inputView.inputExpression();
Calculation calculator = new Calculation(expression);
Double output = calculator.calculate();
outputView.printCalculationResult(output);
repository.save(expression.getExpression(), output);
} catch (ArithmeticException arithmeticException) {
System.out.println(arithmeticException.getMessage());
}
}
}