-
Notifications
You must be signed in to change notification settings - Fork 0
/
qb262.java
76 lines (55 loc) · 2.04 KB
/
qb262.java
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
/* QB-262:
Sports tournament organizer:
Write a program that simulates a sports tournament using a PriorityQueue. The program
should allow the user to input team names and their win-loss records. The program should
then prioritize teams based on their win-loss records and add them to the PriorityQueue.
When it's time for the next match, the program should remove the two highest priority
teams from the PriorityQueue and display their names.
*/
import java.util.*;
class Team {
String name;
int win;
int loss;
public Team(String name, int win, int loss) {
this.name = name;
this.win = win;
this.loss = loss;
}
public String toString() {
return name;
}
public int getWinLossRatio() {
return win - loss;
}
}
class SportsTournament {
public static void main(String[] args) {
PriorityQueue<Team> TeamList = new PriorityQueue<>(Comparator.comparing(Team::getWinLossRatio).reversed());
Scanner sc = new Scanner(System.in);
System.out.println("Enter No.of Teams: ");
int team = sc.nextInt();
sc.nextLine();
// To input team names and their win-loss records
for (int i = 0; i < team; i++) {
System.out.println("Enter Team name for " + (i + 1) + " th: ");
String teamname = sc.nextLine();
System.out.println("Enter number of wins: of " + teamname + "'s Team: ");
int win = sc.nextInt();
System.out.println("Enter number of loss: of " + teamname + "'s Team: ");
int loss = sc.nextInt();
sc.nextLine();
Team t = new Team(teamname, win, loss);
TeamList.add(t);
}
// Next-Match Maker
System.out.println("~~~NEXT-MATCH~~~");
if (!TeamList.isEmpty()) {
Team t1 = TeamList.poll();
Team t2 = TeamList.poll();
System.out.println(t1 + " VS " + t2);
} else {
System.out.println("Not Enough Teams to Start the match (Min:2 Req.)");
}
}
}