Advertisement
Martina312

[НП] - Фудбалска табела

Aug 21st, 2020
1,147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.*;
  5. import java.util.stream.Collectors;
  6. import java.util.stream.IntStream;
  7.  
  8. class Team implements Comparable<Team>{
  9.     private String name;
  10.     private int numWins;
  11.     private int numLoses;
  12.     private int numDraws;
  13.     private int givenGoals;
  14.     private int takenGoals;
  15.  
  16.     public Team(String name) {
  17.         this.name = name;
  18.         numWins = numDraws = numLoses = givenGoals = takenGoals = 0;
  19.     }
  20.  
  21.     public void takeGoals(int goals){
  22.         takenGoals+=goals;
  23.     }
  24.  
  25.     public void giveGoals(int goals){
  26.         givenGoals+=goals;
  27.     }
  28.  
  29.     public void win(){
  30.         numWins++;
  31.     }
  32.  
  33.     public void lose(){
  34.         numLoses++;
  35.     }
  36.  
  37.     public void draw(){
  38.         numDraws++;
  39.     }
  40.  
  41.     public int getTotalPoints(){
  42.       return numDraws+numWins*3;
  43.     }
  44.  
  45.     public int goalsDiff(){
  46.         return givenGoals - takenGoals;
  47.     }
  48.  
  49.     @Override
  50.     public String toString() {
  51.         return String.format("%-15s%5d%5d%5d%5d%5d", name, numDraws+numLoses+numWins, numWins, numDraws, numLoses, numDraws+numWins*3);
  52.     }
  53.  
  54.     @Override
  55.     public int compareTo(Team o) {
  56.         if (getTotalPoints() == o.getTotalPoints()){
  57.             if (goalsDiff() == o.goalsDiff())
  58.                 return name.compareTo(o.name);
  59.             else return -Integer.compare(goalsDiff(), o.goalsDiff());
  60.         }
  61.         return -Integer.compare(getTotalPoints(), o.getTotalPoints());
  62.     }
  63. }
  64.  
  65. class FootballTable{
  66.     Map<String, Team> map;
  67.  
  68.     public FootballTable() {
  69.         this.map = new HashMap<>();
  70.     }
  71.  
  72.     public void addGame(String homeTeam, String awayTeam, int homeGoals, int awayGoals){
  73.         map.computeIfAbsent(homeTeam, (k) -> map.put(homeTeam, new Team(homeTeam)));
  74.         map.computeIfAbsent(awayTeam, (k) -> map.put(awayTeam, new Team(awayTeam)));
  75.  
  76.         Team home = map.get(homeTeam);
  77.         Team away = map.get(awayTeam);
  78.  
  79.         home.giveGoals(homeGoals);
  80.         home.takeGoals(awayGoals);
  81.         away.giveGoals(awayGoals);
  82.         away.takeGoals(homeGoals);
  83.  
  84.         if(homeGoals > awayGoals){
  85.             home.win();
  86.             away.lose();
  87.         }else if (homeGoals < awayGoals){
  88.             home.lose();
  89.             away.win();
  90.         }else{
  91.             home.draw();
  92.             away.draw();
  93.         }
  94.     }
  95.  
  96.     public void printTable(){
  97.         int k = 1;
  98.         List<Team> teamList = map.values().stream().sorted(Team::compareTo).collect(Collectors.toList());
  99.         for (Team t:teamList){
  100.             System.out.println(String.format("%2d. %s", k, t));
  101.             k++;
  102.         }
  103.     }
  104. }
  105. public class FootballTableTest {
  106.     public static void main(String[] args) throws IOException {
  107.         FootballTable table = new FootballTable();
  108.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  109.         reader.lines()
  110.                 .map(line -> line.split(";"))
  111.                 .forEach(parts -> table.addGame(parts[0], parts[1],
  112.                         Integer.parseInt(parts[2]),
  113.                         Integer.parseInt(parts[3])));
  114.         reader.close();
  115.         System.out.println("=== TABLE ===");
  116.         System.out.printf("%-19s%5s%5s%5s%5s%5s\n", "Team", "P", "W", "D", "L", "PTS");
  117.         table.printTable();
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement