Skip to content

Commit b794b35

Browse files
committed
feat(first): first problem
1 parent 77f93fa commit b794b35

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

level1/pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>level1</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<properties>
12+
<maven.compiler.source>21</maven.compiler.source>
13+
<maven.compiler.target>21</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
</project>
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
package org.com.level;
2+
3+
import java.util.ArrayList;
4+
import java.util.Comparator;
5+
import java.util.Locale;
6+
import java.util.Map;
7+
import java.util.Scanner;
8+
import java.util.function.Function;
9+
import java.util.function.Predicate;
10+
import java.util.stream.Collector;
11+
import java.util.stream.Collectors;
12+
import java.util.stream.Stream;
13+
14+
public class Manchester {
15+
public static final Map<String, Comparator<Player>> COMPARATOR = Map.of(
16+
"goals", Comparator.comparingInt(Player :: goals),
17+
"speed", Comparator.comparingInt(Player :: speed),
18+
"assists", Comparator.comparingInt(Player :: assists),
19+
"passing", Comparator.comparingInt(Player :: passingAccuracy),
20+
"defensivePlayer",
21+
Comparator.comparingInt(Player :: passingAccuracy)
22+
);
23+
private static Scanner scanner = new Scanner(System.in);
24+
private static Map<Integer, Player> manchester = Map.of(
25+
8, new Player("Bruno Fernandes", 5, 6, 9, 10, 3, 8),
26+
11, new Player("Rasmus Hojlund", 12, 8, 2, 6, 2, 11),
27+
5, new Player("Harry Maguire", 1, 5, 1, 7, 9, 5),
28+
1, new Player("Alejandro Garnacho", 8, 7, 8, 6, 0, 17),
29+
7, new Player("Mason Mount", 2, 6, 14, 8, 1, 7)
30+
);
31+
32+
public static void main(String[] args) {
33+
var shouldContinue = true;
34+
while (shouldContinue) {
35+
printMenu();
36+
switch (scanner.nextInt()) {
37+
case 1 -> {
38+
System.out.println("give the jersey number");
39+
System.out.println(manchester.get(scanner.nextInt()));
40+
}
41+
case 2 -> {
42+
requireData result = getRequireData();
43+
var playerMap = getComparationMap(
44+
result.statistics(),
45+
result.player1(),
46+
result.player2(),
47+
COMPARATOR);
48+
printComparation(playerMap);
49+
}
50+
case 3 -> printTopPlayerByStatistic("speed");
51+
case 4 -> printTopPlayerByStatistic("goals");
52+
case 5 -> printTopPlayerByStatistic("assists");
53+
case 6 -> printTopPlayerByStatistic("passing");
54+
case 7 -> printTopPlayerByStatistic("defensivePlayer");
55+
case 9 -> shouldContinue = false;
56+
}
57+
}
58+
}
59+
60+
private static void printTopPlayerByStatistic(String st) {
61+
System.out.println(manchester.values().stream().max(COMPARATOR.get(st)).get());
62+
}
63+
64+
private static requireData getRequireData() {
65+
var statistics = new ArrayList<String>();
66+
System.out.println("give the jersey number of player one");
67+
var player1 = manchester.get(scanner.nextInt());
68+
System.out.println("give the jersey number of player two");
69+
var player2 = manchester.get(scanner.nextInt());
70+
71+
var shouldAksStatistic = true;
72+
while (shouldAksStatistic) {
73+
System.out.println("Select player's statistic to compare. 'Q' to stop");
74+
String next = scanner.next().toLowerCase(Locale.ROOT);
75+
shouldAksStatistic = (! next.equals("q")) && statistics.add(next);
76+
}
77+
return new requireData(statistics, player1, player2);
78+
}
79+
80+
private record requireData(
81+
ArrayList<String> statistics, Player player1, Player player2
82+
) {}
83+
84+
private static Map<String, Player> getComparationMap(ArrayList<String> statistics,
85+
Player player1, Player player2
86+
, Map<String, Comparator<Player>> comparator) {
87+
return statistics
88+
.stream()
89+
.map(comparatorFromStatisticToStaAndPlayer(player1, player2, comparator))
90+
.collect(makeAMap());
91+
}
92+
93+
private static void printComparation(Map<String, Player> list) {
94+
list.forEach((key, player) -> System.out.println("Statistic: " + key + " Player" +
95+
":" + player.name()));
96+
}
97+
98+
private static Collector<Map.Entry<String, Player>, ?, Map<String, Player>> makeAMap() {
99+
return Collectors.toMap(Map.Entry :: getKey, Map.Entry :: getValue);
100+
}
101+
102+
private static Function<String, Map.Entry<String, Player>> comparatorFromStatisticToStaAndPlayer(Player player1, Player player2, Map<String, Comparator<Player>> comparator) {
103+
return st -> Map.entry(st,
104+
Stream.of(player1, player2).max(comparator.get(st)).get());
105+
}
106+
107+
private static void printMenu() {
108+
System.out.println("1) Show player characteristics by Jersey Number");
109+
System.out.println("2) Compare two players");
110+
System.out.println("3) Show the fastest player");
111+
System.out.println("4) Show the top goal scorer");
112+
System.out.println("5) Show the player with most assist");
113+
System.out.println("6) Show the highest passing accuracy");
114+
System.out.println("7) Show the better defense player");
115+
System.out.println("8) to exit...!");
116+
}
117+
118+
public static Player searchBy(String characteristic, int comparator) {
119+
Map<String, Predicate<Player>> playerMap = Map.of(
120+
"goals", player -> player.goals().equals(comparator),
121+
"speed", player -> player.speed().equals(comparator),
122+
"assists", player -> player.assists().equals(comparator),
123+
"accurate", player -> player.passingAccuracy().equals(comparator),
124+
"defensivePlayer", player -> player.defensive().equals(comparator)
125+
);
126+
return manchester
127+
.values()
128+
.stream()
129+
.filter(playerMap.get(characteristic))
130+
.findFirst()
131+
.orElseGet(() -> new Player("", 0, 0, 0, 0, 0, 0));
132+
}
133+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package org.com.level;
2+
3+
public record Player (String name,
4+
Integer goals,
5+
Integer speed,
6+
Integer assists,
7+
Integer passingAccuracy,
8+
Integer defensive,
9+
Integer jerseyNumber)
10+
{
11+
}

0 commit comments

Comments
 (0)