Skip to content

Commit ef9dcaa

Browse files
committed
refactor: CalculatorApp 객체에서 Input, Print 필드 제거 및 함수화
1 parent f57592e commit ef9dcaa

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

calculator/src/main/java/com/wonu606/calculator/CalculatorApp.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)