|
| 1 | +package controller; |
| 2 | + |
| 3 | +import static view.InputView.INPUT_EXCEPTION_MESSAGE; |
| 4 | + |
| 5 | +import java.util.Map; |
| 6 | +import java.util.stream.Collectors; |
| 7 | +import model.goal.Goal; |
| 8 | +import model.goal.Goals; |
| 9 | +import model.ladder.Ladder; |
| 10 | +import model.LadderGame; |
| 11 | +import model.ladder.LadderHeight; |
| 12 | +import java.util.ArrayList; |
| 13 | +import java.util.List; |
| 14 | +import model.player.Player; |
| 15 | +import model.player.PlayerName; |
| 16 | +import model.player.Players; |
| 17 | +import model.player.Position; |
| 18 | +import util.LadderGenerator; |
| 19 | +import view.InputView; |
| 20 | +import view.OutputView; |
| 21 | + |
| 22 | +public class LadderController { |
| 23 | + |
| 24 | + private final InputView inputView; |
| 25 | + private final OutputView outputView; |
| 26 | + private final LadderGenerator generator; |
| 27 | + |
| 28 | + public LadderController(InputView inputView, OutputView outputView, LadderGenerator generator) { |
| 29 | + this.inputView = inputView; |
| 30 | + this.outputView = outputView; |
| 31 | + this.generator = generator; |
| 32 | + } |
| 33 | + |
| 34 | + public void run() { |
| 35 | + Players players = createPlayers(); |
| 36 | + Goals goals = createGoals(players); |
| 37 | + LadderHeight height = createLadderHeight(players.size()); |
| 38 | + Ladder ladder = Ladder.of(players, height, generator); |
| 39 | + LadderGame game = new LadderGame(ladder); |
| 40 | + Map<Player, Goal> results = game.play(players, goals); |
| 41 | + outputView.printLadder(ladder, players, goals); |
| 42 | + showResults(players, results); |
| 43 | + } |
| 44 | + |
| 45 | + private Players createPlayers() { |
| 46 | + try { |
| 47 | + List<String> rawNames = inputView.inputPlayers(); |
| 48 | + return Players.from(rawNames); |
| 49 | + } catch (IllegalArgumentException e) { |
| 50 | + outputView.printErrorMessage(e.getMessage()); |
| 51 | + return createPlayers(); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private Goals createGoals(Players players) { |
| 56 | + try { |
| 57 | + List<String> rawGoals = inputView.inputGoals(); |
| 58 | + return Goals.from(rawGoals, players.size()); |
| 59 | + } catch (IllegalArgumentException e) { |
| 60 | + outputView.printErrorMessage(e.getMessage()); |
| 61 | + return createGoals(players); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private LadderHeight createLadderHeight(int playerCount) { |
| 66 | + try { |
| 67 | + int height = inputView.inputLadderHeight(); |
| 68 | + return new LadderHeight(height, playerCount); |
| 69 | + } catch (IllegalArgumentException e) { |
| 70 | + outputView.printErrorMessage(e.getMessage()); |
| 71 | + return createLadderHeight(playerCount); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private void showResults(Players players, Map<Player, Goal> results) { |
| 76 | + String input = inputView.inputPlayerForResult(); |
| 77 | + while (!input.isBlank()) { |
| 78 | + input = processResultInput(input, players, results); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + private String processResultInput(String input, Players players, Map<Player, Goal> results) { |
| 83 | + try { |
| 84 | + selectResult(input, players, results); |
| 85 | + return inputView.inputPlayerForResult(); |
| 86 | + } catch (IllegalArgumentException e) { |
| 87 | + outputView.printErrorMessage(e.getMessage()); |
| 88 | + return inputView.inputPlayerForResult(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + private void selectResult(String input, Players players, Map<Player, Goal> results) { |
| 93 | + if (input.equals("all")) { |
| 94 | + outputView.printAllResults(results); |
| 95 | + return; |
| 96 | + } |
| 97 | + players.validateContainsPlayer(input); |
| 98 | + outputView.printSingleResult(input, players, results); |
| 99 | + } |
| 100 | +} |
0 commit comments