8
8
import com .wonu606 .calculator .strategy .Strategy ;
9
9
import com .wonu606 .io .Input ;
10
10
import com .wonu606 .io .Print ;
11
- import com .wonu606 .util .Message ;
12
11
import java .io .IOException ;
13
12
import java .util .ArrayList ;
14
13
import java .util .List ;
@@ -18,8 +17,6 @@ public class CalculatorApp implements App {
18
17
19
18
private final List <Strategy > strategies = new ArrayList <>();
20
19
private final Persistence store = new ResultStore ();
21
- Input input ;
22
- Print printer ;
23
20
24
21
public CalculatorApp () {
25
22
initStrategies ();
@@ -30,20 +27,29 @@ private void initStrategies() {
30
27
}
31
28
32
29
public void execute (Input input , Print printer ) throws IOException {
33
- this .input = input ;
34
- this .printer = printer ;
35
-
36
30
Converter <String , Integer > stringIntegerConverter = new StringToIntegerConverter ();
37
31
while (true ) {
38
- int selection = stringIntegerConverter .convert (input .getInput ());
39
-
40
- Optional <Strategy > selectedStrategy =
41
- Optional .ofNullable (strategies .get (selection - 1 ));
42
- selectedStrategy .ifPresentOrElse (
43
- strategy -> strategy .execute (input , printer , store ),
44
- () -> {
45
- throw new IllegalArgumentException ("잘못된 입력입니다." );
46
- });
32
+ int selection = inputMenuSelection (input , stringIntegerConverter );
33
+ Optional <Strategy > selectedStrategy = getStrategy (selection );
34
+ performStrategy (input , printer , selectedStrategy );
47
35
}
48
36
}
37
+
38
+ private void performStrategy (Input input , Print printer , Optional <Strategy > selectedStrategy ) {
39
+ selectedStrategy .ifPresentOrElse (
40
+ strategy -> strategy .execute (input , printer , store ),
41
+ () -> {
42
+ throw new IllegalArgumentException ("잘못된 입력입니다." );
43
+ });
44
+ }
45
+
46
+ private Optional <Strategy > getStrategy (int selection ) {
47
+ return Optional .ofNullable (strategies .get (selection - 1 ));
48
+ }
49
+
50
+ private static int inputMenuSelection (Input input ,
51
+ Converter <String , Integer > stringIntegerConverter )
52
+ throws IOException {
53
+ return stringIntegerConverter .convert (input .getInput ());
54
+ }
49
55
}
0 commit comments