Skip to content

Commit 53f25bb

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

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

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

+21-15
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.wonu606.calculator.strategy.Strategy;
99
import com.wonu606.io.Input;
1010
import com.wonu606.io.Print;
11-
import com.wonu606.util.Message;
1211
import java.io.IOException;
1312
import java.util.ArrayList;
1413
import java.util.List;
@@ -18,8 +17,6 @@ public class CalculatorApp implements App {
1817

1918
private final List<Strategy> strategies = new ArrayList<>();
2019
private final Persistence store = new ResultStore();
21-
Input input;
22-
Print printer;
2320

2421
public CalculatorApp() {
2522
initStrategies();
@@ -30,20 +27,29 @@ private void initStrategies() {
3027
}
3128

3229
public void execute(Input input, Print printer) throws IOException {
33-
this.input = input;
34-
this.printer = printer;
35-
3630
Converter<String, Integer> stringIntegerConverter = new StringToIntegerConverter();
3731
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);
4735
}
4836
}
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+
}
4955
}

0 commit comments

Comments
 (0)