Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.stream.Collectors;
- import java.util.stream.IntStream;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.concurrent.atomic.AtomicInteger;
- class FootballTable {
- Map<String, Team> teams;
- public FootballTable() {
- this.teams = new HashMap<>();
- }
- public void addGame(String hTeam, String aTeam, int hGoals, int aGoals) {
- teams.putIfAbsent(hTeam, new Team(hTeam, 0, 0, 0, 0));
- teams.putIfAbsent(aTeam, new Team(aTeam, 0, 0, 0, 0));
- teams.computeIfPresent(hTeam,
- (k, v) -> {
- v.setWins(v.getWins() + (hGoals > aGoals ? 1 : 0));
- v.setDraws(v.getDraws() + (hGoals == aGoals ? 1 : 0));
- v.setLosses(v.getLosses() + (hGoals < aGoals ? 1 : 0));
- v.setGoalsDifference(v.getGoalsDifference() + hGoals - aGoals);
- return v;
- });
- teams.computeIfPresent(aTeam,
- (k, v) -> {
- v.setWins(v.getWins() + (aGoals > hGoals ? 1 : 0));
- v.setDraws(v.getDraws() + (aGoals == hGoals ? 1 : 0));
- v.setLosses(v.getLosses() + (aGoals < hGoals ? 1 : 0));
- v.setGoalsDifference(v.getGoalsDifference() + aGoals - hGoals);
- return v;
- });
- }
- public void printTable() {
- AtomicInteger atomicInteger = new AtomicInteger(1);
- teams.values().stream().sorted().forEach(i-> System.out.printf("%2d. %s",atomicInteger.getAndIncrement(),i));
- }
- }
- class Team implements Comparable<Team>{
- String name;
- int wins;
- int draws;
- int losses;
- int goalsDifference;
- public Team(String name, int wins, int draws, int losses, int goalsDifference) {
- this.name= name;
- this.wins = wins;
- this.draws = draws;
- this.losses = losses;
- this.goalsDifference= goalsDifference;
- }
- public void setGoalsDifference(int goalsDifference) {
- this.goalsDifference = goalsDifference;
- }
- public int getGoalsDifference() {
- return goalsDifference;
- }
- public void setWins(int wins) {
- this.wins = wins;
- }
- public void setDraws(int draws) {
- this.draws = draws;
- }
- public void setLosses(int losses) {
- this.losses = losses;
- }
- public int getWins() {
- return wins;
- }
- public int getDraws() {
- return draws;
- }
- public int getLosses() {
- return losses;
- }
- public int getPoints(){
- return 3*wins+1*draws;
- }
- public String getName() {
- return name;
- }
- public int getNumberOfMatches(){
- return wins+draws+losses;
- }
- @Override
- public String toString() {
- return String.format("%-15s%5d%5d%5d%5d%5d\n",getName(),getNumberOfMatches(),getWins(),getDraws(),getLosses(),getPoints());
- }
- @Override
- public int compareTo(Team o) {
- return Comparator.comparing(Team::getPoints)
- .thenComparing(Team::getGoalsDifference).reversed()
- .thenComparing(Team::getName)
- .compare(this,o);
- }
- }
- /**
- * Partial exam II 2016/2017
- */
- public class FootballTableTest {
- public static void main(String[] args) throws IOException {
- FootballTable table = new FootballTable();
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- reader.lines()
- .map(line -> line.split(";"))
- .forEach(parts -> table.addGame(parts[0], parts[1],
- Integer.parseInt(parts[2]),
- Integer.parseInt(parts[3])));
- reader.close();
- System.out.println("=== TABLE ===");
- System.out.printf("%-19s%5s%5s%5s%5s%5s\n", "Team", "P", "W", "D", "L", "PTS");
- table.printTable();
- }
- }
- // Your code here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement