-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
77 lines (65 loc) · 2.23 KB
/
Main.java
File metadata and controls
77 lines (65 loc) · 2.23 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
77
import java.util.*;
class Player implements Comparable<Player> {
private String playerName;
private int noOfGoals;
private double average;
public Player(String playerName, int noOfGoals, double average) {
this.playerName = playerName;
this.noOfGoals = noOfGoals;
this.average = average;
}
public String getPlayerName() {
return playerName;
}
public void setPlayerName(String playerName) {
this.playerName = playerName;
}
public int getNoOfGoals() {
return noOfGoals;
}
public void setNoOfGoals(int noOfGoals) {
this.noOfGoals = noOfGoals;
}
public double getAverage() {
return average;
}
public void setAverage(double average) {
this.average = average;
}
@Override
public String toString() {
return playerName + " " + noOfGoals + " " + average;
}
@Override
public int compareTo(Player other) {
if (this.noOfGoals != other.noOfGoals) {
return other.noOfGoals - this.noOfGoals;
} else if (this.average != other.average) {
return Double.compare(other.average, this.average);
} else {
return this.playerName.compareTo(other.playerName);
}
}
}
public class Main {
public static TreeMap<Player, Integer> getMapOfSelectedPlayers(Map<Integer, Player> playerMap) {
TreeMap<Player, Integer> treeMapOfPlayers = new TreeMap<>();
for (Map.Entry<Integer, Player> entry : playerMap.entrySet()) {
treeMapOfPlayers.put(entry.getValue(), entry.getValue().getNoOfGoals());
}
return treeMapOfPlayers;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int numPlayers = scanner.nextInt();
Map<Integer, Player> playerMap = new HashMap<>();
for (int i = 1; i <= numPlayers; i++) {
String playerName = scanner.next();
int noOfGoals = scanner.nextInt();
double average = scanner.nextDouble();
playerMap.put(i, new Player(playerName, noOfGoals, average));
}
TreeMap<Player, Integer> result = getMapOfSelectedPlayers(playerMap);
System.out.println(result);
}
}