|
| 1 | +package org.com.level.manchester; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Locale; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.Scanner; |
| 7 | +import java.util.function.Supplier; |
| 8 | + |
| 9 | +import static org.com.level.manchester.Printer.printComparative; |
| 10 | +import static org.com.level.manchester.Printer.printMenu; |
| 11 | +import static org.com.level.manchester.Printer.print; |
| 12 | +import static org.com.level.manchester.Printer.printTopPlayerByStatistic; |
| 13 | + |
| 14 | +public class Manchester { |
| 15 | + private static final Scanner scanner = new Scanner(System.in); |
| 16 | + static final Map<Integer, Player> manchester = Map.of( |
| 17 | + 8, new Player("Bruno Fernandes", 5, 6, 9, 10, 3, 8), |
| 18 | + 11, new Player("Rasmus Hojlund", 12, 8, 2, 6, 2, 11), |
| 19 | + 5, new Player("Harry Maguire", 1, 5, 1, 7, 9, 5), |
| 20 | + 1, new Player("Alejandro Garnacho", 8, 7, 8, 6, 0, 17), |
| 21 | + 7, new Player("Mason Mount", 2, 6, 14, 8, 1, 7) |
| 22 | + ); |
| 23 | + |
| 24 | + public static void main(String[] args) { |
| 25 | + repeat(Manchester::statisticsProcess); |
| 26 | + } |
| 27 | + |
| 28 | + private static boolean statisticsProcess() { |
| 29 | + printMenu(); |
| 30 | + switch (scanner.nextInt()) { |
| 31 | + case 1 -> { |
| 32 | + print("give the jersey number"); |
| 33 | + print(getPlayer()); |
| 34 | + } |
| 35 | + case 2 -> { |
| 36 | + var results = compareTwoPlayers(); |
| 37 | + printComparative(results); |
| 38 | + } |
| 39 | + case 3 -> printTopPlayerByStatistic("speed"); |
| 40 | + case 4 -> printTopPlayerByStatistic("goals"); |
| 41 | + case 5 -> printTopPlayerByStatistic("assists"); |
| 42 | + case 6 -> printTopPlayerByStatistic("passing"); |
| 43 | + case 7 -> printTopPlayerByStatistic("defensivePlayer"); |
| 44 | + case 9 -> {return false;} |
| 45 | + } |
| 46 | + return true; |
| 47 | + } |
| 48 | + |
| 49 | + private static Map<String, Player> compareTwoPlayers() { |
| 50 | + var selected = getRequireData(); |
| 51 | + return Player |
| 52 | + .getComparativeFor(selected.statistics(), |
| 53 | + selected.player1(), |
| 54 | + selected.player2()); |
| 55 | + } |
| 56 | + |
| 57 | + private static Player getPlayer() { |
| 58 | + return manchester.get(scanner.nextInt()); |
| 59 | + } |
| 60 | + |
| 61 | + private static SubmitedData getRequireData() { |
| 62 | + var statistics = new ArrayList<String>(); |
| 63 | + print("give the jersey number of player one"); |
| 64 | + var player1 = getPlayer(); |
| 65 | + print("give the jersey number of player two"); |
| 66 | + var player2 = getPlayer(); |
| 67 | + repeat( |
| 68 | + () -> { |
| 69 | + print("Select player's statistic to compare. 'Q' to stop"); |
| 70 | + String next = scanner.next().toLowerCase(Locale.ROOT); |
| 71 | + return (! next.equals("q")) && statistics.add(next); |
| 72 | + } |
| 73 | + ); |
| 74 | + return new SubmitedData(statistics, player1, player2); |
| 75 | + } |
| 76 | + |
| 77 | + private static void repeat(Supplier<Boolean> fun) { |
| 78 | + var shouldContinue = true; |
| 79 | + while (shouldContinue) { |
| 80 | + shouldContinue = fun.get(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private record SubmitedData( |
| 85 | + ArrayList<String> statistics, Player player1, Player player2 |
| 86 | + ) {} |
| 87 | +} |
0 commit comments