99import  com .wonu606 .util .Message ;
1010import  java .io .IOException ;
1111import  java .util .ArrayList ;
12+ import  java .util .HashMap ;
1213import  java .util .List ;
14+ import  java .util .Map ;
1315import  java .util .Optional ;
1416
1517public  class  CalculatorApp  implements  App  {
1618
17-     private  final  List < CalculatorStrategy > strategies  = new  ArrayList <> ();
19+     private  final  Map < String ,  CalculatorStrategy > strategies  = new  HashMap ();
1820    private  final  Persistence  store  = new  ResultStore ();
19-     Input  input ;
20-     Print  printer ;
2121
2222    public  CalculatorApp () {
2323        initStrategies ();
@@ -27,20 +27,31 @@ private void initStrategies() {
2727        // TODO 조회와 계산 기능을 생성하여 추가해야 함 
2828    }
2929
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 ) {
3449        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+             }
4455        }
4556    }
4657}
0 commit comments