Advertisement
RRPetrova

Untitled

Mar 16th, 2022
1,264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.89 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3. import java.util.stream.StreamSupport;
  4.  
  5. public class OlympicsImpl implements Olympics {
  6.  
  7.  
  8.     private HashSet<Competitor> competitorList;
  9.     private List<Competition> competitionList;
  10.  
  11.     public OlympicsImpl() {
  12.         this.competitorList = new HashSet<>();
  13.         this.competitionList = new ArrayList<>();
  14.     }
  15.  
  16.  
  17.     @Override
  18.     public void addCompetitor(int id, String name) {
  19.         Competitor current = searchCompetitorById(id);
  20.         if (current != null) {
  21.             throw new IllegalArgumentException("Already exists");
  22.         }
  23.         Competitor competitor = new Competitor(id, name);
  24.         this.competitorList.add(competitor);
  25.     }
  26.  
  27.     @Override
  28.     public void addCompetition(int id, String name, int score) {
  29.         Competition current = searchCompetitionById(id);
  30.         if (current != null) {
  31.             throw new IllegalArgumentException("Already exists");
  32.         }
  33.         Competition competition = new Competition(name, id, score);
  34.         this.competitionList.add(competition);
  35.     }
  36.  
  37.     @Override
  38.     public void compete(int competitorId, int competitionId) {
  39.         Competition currCompetition = searchCompetitionById(competitionId);
  40.         Competitor currCompetitor = searchCompetitorById(competitorId);
  41.         if (currCompetitor == null || currCompetition == null) {
  42.             throw new IllegalArgumentException("Not found comp");
  43.         }
  44.         //TODO: if already exists case ?
  45.         Collection<Competitor> currCompetitionCompetitors = currCompetition.getCompetitors();
  46.         currCompetitor.setTotalScore(currCompetition.getScore() + currCompetitor.getTotalScore());
  47.         currCompetitionCompetitors.add(currCompetitor);
  48.         currCompetition.setCompetitors(currCompetitionCompetitors);
  49.         this.competitionList.add(currCompetition);
  50.     }
  51.  
  52.     @Override
  53.     public void disqualify(int competitionId, int competitorId) {
  54.         Competition currCompetition = searchCompetitionById(competitionId);
  55.         Competitor currCompetitor = searchCompetitorById(competitorId);
  56.         if (currCompetitor == null || currCompetition == null) {
  57.             throw new IllegalArgumentException("Not found");
  58.         }
  59.  
  60.         Competitor competitorInThisCompetition = null;
  61. //                = currCompetition
  62. //                .getCompetitors()
  63. //                .stream()
  64. //                .filter(c -> c.getId() == currCompetitor.getId())
  65. //                .findFirst()
  66. //                .orElse(null);
  67.  
  68.  
  69.         for (Competitor competitor : currCompetition.getCompetitors()) {
  70.             if (competitor.getId() == currCompetitor.getId()) {
  71.                 competitorInThisCompetition = competitor;
  72.                 currCompetition.getCompetitors().remove(competitorInThisCompetition);
  73.                 //  long score= currCompetitor.getTotalScore();
  74.                 //  int score1 = currCompetition.getScore();
  75.                 currCompetitor.setTotalScore(currCompetitor.getTotalScore() - currCompetition.getScore());
  76.                 break;
  77.             }
  78.         }
  79.  
  80.         if (competitorInThisCompetition == null) {
  81.             throw new IllegalArgumentException("Not found");
  82.         }
  83.     }
  84.  
  85.     @Override
  86.     public Iterable<Competitor> findCompetitorsInRange(long min, long max) {
  87. //        Set<Competitor> collect = this.competitorList
  88. //                .stream()
  89. //                .filter(c -> c.getTotalScore() > min && c.getTotalScore() <= max)
  90. //                .collect(Collectors.toSet());
  91.     List<Competitor> res = new ArrayList<>();
  92.         for (Competitor competitor : this.competitorList) {
  93.             if (competitor.getTotalScore() > min && competitor.getTotalScore() <= max) {
  94.                 res.add(competitor);
  95.             }
  96.         }
  97.         res.sort(Comparator.comparingInt(Competitor::getId));
  98.  
  99.         return res;
  100.     }
  101.  
  102.     @Override
  103.     public Iterable<Competitor> getByName(String name) {
  104.  
  105.         List<Competitor> collect = this.competitorList
  106.                 .stream()
  107.                 .filter(c -> c.getName().equals(name))
  108.                 .sorted(Comparator.comparingInt(Competitor::getId))
  109.                 .collect(Collectors.toList());
  110.         if (collect.isEmpty()) {
  111.             throw new IllegalArgumentException("Not found");
  112.         }
  113.         return collect;
  114.     }
  115.  
  116.     @Override
  117.     public Iterable<Competitor> searchWithNameLength(int minLength, int maxLength) {
  118.         List<Competitor> res = new ArrayList<>();
  119.         for (Competitor currCompetitor : this.competitorList) {
  120.             if (currCompetitor.getName().length() >= minLength && currCompetitor.getName().length() <= maxLength) {
  121.                 res.add(currCompetitor);
  122.             }
  123.         }
  124.  
  125.         res.sort(Comparator.comparingInt(Competitor::getId));
  126.         return res;
  127. //        return this.competitorList
  128. //                .stream()
  129. //                .filter(c -> c.getName().length() >= minLength && c.getName().length() <= maxLength)
  130. //                .sorted(Comparator.comparingInt(Competitor::getId))
  131. //                .collect(Collectors.toList());
  132.     }
  133.  
  134.     @Override
  135.     public Boolean contains(int competitionId, Competitor comp) {
  136.         Competition compn = searchCompetitionById(competitionId);
  137.  
  138.         if (compn == null) {
  139.             throw new IllegalArgumentException("Not found with cmpn id: " + competitionId);
  140.         }
  141.         return compn.getCompetitors()
  142.                 .stream()
  143.                 .anyMatch(competitor -> competitor.getId() == comp.getId());
  144.     }
  145.  
  146.     @Override
  147.     public int competitionsCount() {
  148.         return this.competitionList.size();
  149.     }
  150.  
  151.     @Override
  152.     public int competitorsCount() {
  153.         return this.competitorList.size();
  154.     }
  155.  
  156.     @Override
  157.     public Competition getCompetition(int id) {
  158.         Competition current = searchCompetitionById(id);
  159.         if (current == null) {
  160.             throw new IllegalArgumentException("Not valid competition with id " + id);
  161.         }
  162.         return current;
  163.     }
  164.  
  165.     private Competitor searchCompetitorById(int id) {
  166. //        return competitorList
  167. //                .stream()
  168. //                .filter(c -> c.getId() == id)
  169. //                .findFirst()
  170. //                .orElse(null);
  171.  
  172.         for (Competitor currCompetitor : this.competitorList) {
  173.             if (currCompetitor.getId() == id) {
  174.                 return currCompetitor;
  175.             }
  176.         }
  177.         return null;
  178.  
  179.     }
  180.  
  181.     private Competition searchCompetitionById(int id) {
  182. //        return competitionList
  183. //                .stream()
  184. //                .filter(c -> c.getId() == id)
  185. //                .findFirst()
  186. //                .orElse(null);
  187.  
  188.         for (Competition currCompetition : this.competitionList) {
  189.             if (currCompetition.getId() == id) {
  190.                 return currCompetition;
  191.             }
  192.  
  193.         }
  194.         return null;
  195.     }
  196. }
  197.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement