9
9
import com .wonu606 .util .Message ;
10
10
import java .io .IOException ;
11
11
import java .util .ArrayList ;
12
+ import java .util .HashMap ;
12
13
import java .util .List ;
14
+ import java .util .Map ;
13
15
import java .util .Optional ;
14
16
15
17
public class CalculatorApp implements App {
16
18
17
- private final List < CalculatorStrategy > strategies = new ArrayList <> ();
19
+ private final Map < String , CalculatorStrategy > strategies = new HashMap ();
18
20
private final Persistence store = new ResultStore ();
19
- Input input ;
20
- Print printer ;
21
21
22
22
public CalculatorApp () {
23
23
initStrategies ();
@@ -27,20 +27,31 @@ private void initStrategies() {
27
27
// TODO 조회와 계산 기능을 생성하여 추가해야 함
28
28
}
29
29
30
- public void execute (Input input , Print printer ) throws IOException {
31
- this .input = input ;
32
- this .printer = printer ;
33
-
30
+ public void execute (Input input , Print printer ) {
31
+
32
+ while (true ) {
33
+ String selection = inputMenuSelection (input );
34
+ CalculatorStrategy selectedStrategy = getStrategyOrThrow (selection );
35
+ performStrategy (input , printer , selectedStrategy );
36
+ }
37
+ }
38
+
39
+ private void performStrategy (Input input , Print printer , CalculatorStrategy selectedStrategy ) {
40
+ selectedStrategy .execute (input , printer , store );
41
+ }
42
+
43
+ private CalculatorStrategy getStrategyOrThrow (String selection ) {
44
+ return Optional .ofNullable (strategies .get (selection ))
45
+ .orElseThrow (() -> new IllegalArgumentException (Message .INVALID_INPUT ));
46
+ }
47
+
48
+ private String inputMenuSelection (Input input ) {
34
49
while (true ) {
35
- int selection = Integer .parseInt (input .getInput ());
36
-
37
- Optional <CalculatorStrategy > selectedStrategy =
38
- Optional .ofNullable (strategies .get (selection - 1 ));
39
- selectedStrategy .ifPresentOrElse (
40
- strategy -> strategy .execute (input , printer , store ),
41
- () -> {
42
- throw new IllegalArgumentException (Message .INVALID_INPUT );
43
- });
50
+ try {
51
+ return input .getInput ();
52
+ } catch (IOException e ) {
53
+ e .printStackTrace ();
54
+ }
44
55
}
45
56
}
46
57
}
0 commit comments