-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathInputController.java
More file actions
76 lines (63 loc) · 2.35 KB
/
Copy pathInputController.java
File metadata and controls
76 lines (63 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package pairmatching.controller;
import pairmatching.controller.command.MainCommand;
import pairmatching.controller.command.ReMatchingCommand;
import pairmatching.domain.choice.Choice;
import pairmatching.domain.choice.ChoiceMaker;
import pairmatching.view.InputView;
import pairmatching.view.OutputView;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class InputController {
private final InputView inputView;
private final OutputView outputView;
public InputController(InputView inputView, OutputView outputView) {
this.inputView = inputView;
this.outputView = outputView;
}
public MainCommand readValidCommand() {
return repeatUntilGettingValidValue(this::readCommand);
}
public Choice readValidChoice() {
return repeatUntilGettingValidValue(this::readChoice);
}
public ReMatchingCommand readValidReMatchingCommand() {
return repeatUntilGettingValidValue(this::readReMatchingCommand);
}
private MainCommand readCommand() {
outputView.printCommandGuide();
String inputCommand = inputView.readCommand();
return MainCommand.valueOfCommand(inputCommand);
}
private Choice readChoice() {
outputView.printChoiceGuide();
List<String> choices = inputView.readChoices();
ChoiceMaker choiceMaker = new ChoiceMaker();
return choiceMaker.createChoice(choices);
}
private ReMatchingCommand readReMatchingCommand() {
outputView.printReMatchingGuide();
String input = inputView.readReMatchingCommand();
return ReMatchingCommand.valueOfReMatchingCommand(input);
}
private <T> T repeatUntilGettingValidValue(Supplier<T> getSomething) {
while (true) {
try {
return getSomething.get();
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
}
}
}
public <T> void repeatUntilGettingValidValue(Consumer<T> getSomething, T input) {
boolean isContinuing = true;
while (isContinuing) {
try {
getSomething.accept(input);
isContinuing = false;
} catch (IllegalArgumentException e) {
outputView.printErrorMessage(e.getMessage());
}
}
}
}