@@ -18,8 +18,6 @@ public class CalculatorApp implements App {
1818
1919 private final List <Strategy > strategies = new ArrayList <>();
2020 private final Persistence store = new ResultStore ();
21- Input input ;
22- Print printer ;
2321
2422 public CalculatorApp () {
2523 initStrategies ();
@@ -30,20 +28,29 @@ private void initStrategies() {
3028 }
3129
3230 public void execute (Input input , Print printer ) throws IOException {
33- this .input = input ;
34- this .printer = printer ;
35-
3631 Converter <String , Integer > stringIntegerConverter = new StringToIntegerConverter ();
3732 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- });
33+ int selection = inputMenuSelection (input , stringIntegerConverter );
34+ Strategy selectedStrategy = getStrategy (selection );
35+ performStrategy (input , printer , selectedStrategy );
36+ }
37+ }
38+
39+ private void performStrategy (Input input , Print printer , Strategy selectedStrategy ) {
40+ selectedStrategy .execute (input , printer , store );
41+ }
42+
43+ private Strategy getStrategy (int selection ) {
44+ try {
45+ return strategies .get (selection - 1 );
46+ } catch (IndexOutOfBoundsException exception ) {
47+ throw new IllegalArgumentException (Message .INVALID_INPUT );
4748 }
4849 }
50+
51+ private static int inputMenuSelection (Input input ,
52+ Converter <String , Integer > stringIntegerConverter )
53+ throws IOException {
54+ return stringIntegerConverter .convert (input .getInput ());
55+ }
4956}
0 commit comments