Football Comeptition
Football Comeptition
Hand_On_Projects;
import java.util.*;
/*
Tally the results of a small football competition. Based on an input file
containing which team played against which and what the outcome was, create a file
with a table like this:
Team | MP | W | D | L | P
Devastating Tigers | 3 | 2 | 1 | 0 | 7
Allegoric Alaskans | 3 | 2 | 0 | 1 | 6
Blithering Badgers | 3 | 1 | 0 | 2 | 3
Courageous Californians | 3 | 0 | 1 | 2 | 1
What do those abbreviations mean?
MP: Matches Played
W: Matches Won
D: Matches Drawn (Tied)
L: Matches Lost
P: Points
A win earns a team 3 points. A draw earns 1. A loss earns 0. The outcome should be
ordered by points, descending. In case of a tie, teams are ordered alphabetically.
*/
class Teams{
public int MP,W,D,L,P;
public Map<String, Teams> map = new HashMap<>(4,0.75f);
Teams(int mp,int w,int d,int l,int p){
this.MP=mp;
this.W=w;
this.D=d;
this.L=l;
this.P=p;
}
if (!map.isEmpty()) {
System.out.println();
ArrayList<Map.Entry<String,Teams>> list = new
ArrayList<>(map.entrySet());
list.sort((entry1,entry2) -> {
int point =
Integer.compare(entry2.getValue().P,entry1.getValue().P);
if(point == 0){
return entry1.getKey().compareToIgnoreCase(entry2.getKey());
}
return point;
});
System.out.println("Team | MP | W | D | L | P ");
for (Map.Entry<String, Teams> obj : list) {
System.out.println(obj.getKey() + " " + obj.getValue());
}
}
System.out.println();
}
public void MatchToPlay (String team1, String team2, String winOrLoose){
Teams obj1 = map.get(team1);
Teams obj2 = map.get(team2);
if(map.containsKey(team1) && map.containsKey(team2)){
if (winOrLoose.equals("win")) {
obj1.MP++;
obj1.W++;
obj1.P+=3;
obj2.MP++;
obj2.L++;
} else if (winOrLoose.equals("loose")) {
obj2.MP++;
obj2.W++;
obj2.P+=3;
obj1.MP++;
obj1.L++;
} else {
obj1.MP++;
obj1.D++;
obj1.P++;
obj2.MP++;
obj2.D++;
obj2.P++;
}
} else {
System.out.println("The given teams aren't playing..!!");
}
System.out.println();
}
}
@Override
public String toString(){
return this.MP + " | " + this.W + " | " + this.D + " | " + this.L + " | "
+ this.P;
}
}
public class Football_Competition {
public static void main(String[] args) {
Teams team = new Teams(0, 0, 0, 0, 0);
Scanner sc = new Scanner(System.in);
team.addTeams("Devastating_Tigers");
team.addTeams("Allegoric_Alaskans");
team.addTeams("Blithering_Badgers");
team.addTeams("Courageous_Californians");
String team1, team2, winOrLoose;
while (true) {
System.out.println("**FootBall Competition**\n1.Show Details\
n2.MatchToPlay\n3.OrderTable\n4.Exit\nEnter your Choice");
int n = sc.nextInt();
switch (n) {
case 1 -> team.showDetails();
case 2 -> {
System.out.println("Enter two teams and win/loose:");
System.out.println("Team1: ");
team1 = sc.next();
System.out.println("Team2: ");
team2 = sc.next();
System.out.println("win/Loose: ");
winOrLoose = sc.next();
team.MatchToPlay(team1, team2, winOrLoose);
}
case 3 -> team.OrderTable();
case 4 -> System.exit(0);
}
}
}
}