Skip to content

Commit eff56f5

Browse files
committed
feat(first): refactor classes
1 parent b794b35 commit eff56f5

File tree

5 files changed

+154
-144
lines changed

5 files changed

+154
-144
lines changed

level1/src/main/java/org/com/level/Manchester.java

Lines changed: 0 additions & 133 deletions
This file was deleted.

level1/src/main/java/org/com/level/Player.java

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.com.level.manchester;
2+
3+
import java.util.ArrayList;
4+
import java.util.Comparator;
5+
import java.util.Map;
6+
import java.util.function.Function;
7+
import java.util.stream.Collectors;
8+
import java.util.stream.Stream;
9+
10+
public record Player (String name,
11+
Integer goals,
12+
Integer speed,
13+
Integer assists,
14+
Integer passingAccuracy,
15+
Integer defensive,
16+
Integer jerseyNumber)
17+
{
18+
public static final Map<String, Comparator<Player>> COMPARATORS = Map.of(
19+
"goals", Comparator.comparingInt(Player :: speed),
20+
"speed", Comparator.comparingInt(Player :: speed),
21+
"assists", Comparator.comparingInt(Player :: assists),
22+
"passing", Comparator.comparingInt(Player :: passingAccuracy),
23+
"defensivePlayer", Comparator.comparingInt(Player :: passingAccuracy));
24+
25+
static Map<String, Player> getComparativeFor(ArrayList<String> statistics,
26+
Player player1, Player player2) {
27+
return statistics
28+
.stream()
29+
.map(comparePlayers(player1, player2))
30+
.collect(Collectors.toMap(Map.Entry :: getKey, Map.Entry :: getValue));
31+
}
32+
33+
private static Function<String, Map.Entry<String, Player>> comparePlayers(Player player1, Player player2) {
34+
return st -> Map.entry(st, Stream.of(player1, player2).max(COMPARATORS.get(st)).get());
35+
}
36+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.com.level.manchester;
2+
3+
import java.util.Map;
4+
5+
public class Printer {
6+
7+
static void printMenu() {
8+
System.out.println("1) Show player characteristics by Jersey Number");
9+
System.out.println("2) Compare two players");
10+
System.out.println("3) Show the fastest player");
11+
System.out.println("4) Show the top goal scorer");
12+
System.out.println("5) Show the player with most assist");
13+
System.out.println("6) Show the highest passing accuracy");
14+
System.out.println("7) Show the better defense player");
15+
System.out.println("8) to exit...!");
16+
}
17+
18+
static void printComparative(Map<String, Player> list) {
19+
list.forEach((key, player) -> System.out.println("Statistic: " + key + " Player" +
20+
":" + player.name()));
21+
}
22+
23+
static void printTopPlayerByStatistic(String st) {
24+
Player player = Manchester.manchester.values().stream().max(Player.COMPARATORS.get(st)).get();
25+
System.out.println(player);
26+
}
27+
28+
static <T> void print(T x) {
29+
System.out.println(x);
30+
}
31+
}

0 commit comments

Comments
 (0)