Advertisement
Guest User

Untitled

a guest
Aug 17th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.33 KB | None | 0 0
  1. package motocrossWorldChampionship.core;
  2.  
  3. import motocrossWorldChampionship.common.ExceptionMessages;
  4. import motocrossWorldChampionship.core.interfaces.ChampionshipController;
  5. import motocrossWorldChampionship.entities.PowerMotorcycle;
  6. import motocrossWorldChampionship.entities.RaceImpl;
  7. import motocrossWorldChampionship.entities.RiderImpl;
  8. import motocrossWorldChampionship.entities.SpeedMotorcycle;
  9. import motocrossWorldChampionship.entities.interfaces.Motorcycle;
  10. import motocrossWorldChampionship.entities.interfaces.Race;
  11. import motocrossWorldChampionship.entities.interfaces.Rider;
  12. import motocrossWorldChampionship.repositories.MotorcycleRepository;
  13. import motocrossWorldChampionship.repositories.RaceRepository;
  14. import motocrossWorldChampionship.repositories.RiderRepository;
  15. import motocrossWorldChampionship.repositories.interfaces.Repository;
  16.  
  17. import java.util.List;
  18. import java.util.stream.Collectors;
  19.  
  20. public class ChampionshipControllerImpl implements ChampionshipController {
  21.  
  22.     private static final String RIDER_CREATED = "Rider %s is created.";
  23.     private static final String MOTORCYCLE_CREATED = "%s %s is created.";
  24.     private static final String RIDER_RECEIVED_MOTORCYCLE_MESSAGE = "Rider %s received motorcycle %s.";
  25.     private static final String RIDER_ADDED_TO_RACE_MESSAGE = "Rider %s added in %s race.";
  26.     private static final String RACE_CREATED = "Race %s is created.";
  27.     private static final String FIRST_RIDER_MESSAGE = "Rider %s wins %s race.";
  28.     private static final String SECOND_RIDER_MESSAGE = "Rider %s is second in %s race.";
  29.     private static final String THIRD_RIDER_MESSAGE = "Rider %s is third in %s race.";
  30.     private static final int MIN_PARTICIPANTS_FOR_RACE = 3;
  31.  
  32.     private Repository<Rider> riderRepository;
  33.     private Repository<Motorcycle> motorcycleRepository;
  34.     private Repository<Race> raceRepository;
  35.  
  36.     public ChampionshipControllerImpl() {
  37.         this.riderRepository = new RiderRepository();
  38.         this.motorcycleRepository = new MotorcycleRepository();
  39.         this.raceRepository = new RaceRepository();
  40.     }
  41.  
  42.     @Override
  43.     public String createRider(String riderName) {
  44.         Rider rider = new RiderImpl(riderName);
  45.         this.riderRepository.add(rider);
  46.  
  47.         return String.format(RIDER_CREATED, riderName);
  48.     }
  49.  
  50.     @Override
  51.     public String createMotorcycle(String type, String model, int horsePower) {
  52.         Motorcycle motorcycle = null;
  53.  
  54.         switch (type){
  55.             case "Power":
  56.                 motorcycle = new PowerMotorcycle(model, horsePower);
  57.                 break;
  58.             case "Speed":
  59.                 motorcycle = new SpeedMotorcycle(model, horsePower);
  60.                 break;
  61.         }
  62.  
  63.         this.motorcycleRepository.add(motorcycle);
  64.  
  65.         return String.format(MOTORCYCLE_CREATED, motorcycle.getClass().getSimpleName(), motorcycle.getModel());
  66.     }
  67.  
  68.     @Override
  69.     public String addMotorcycleToRider(String riderName, String motorcycleModel) {
  70.         Rider rider = this.riderRepository.getByName(riderName);
  71.         Motorcycle motorcycle = this.motorcycleRepository.getByName(motorcycleModel);
  72.  
  73.         if (rider == null){
  74.             throw new NullPointerException(String.format(
  75.                     ExceptionMessages.RIDER_NOT_FOUND,
  76.                     riderName
  77.             ));
  78.         }
  79.  
  80.         if (motorcycle == null){
  81.             throw new NullPointerException(String.format(
  82.                     ExceptionMessages.MOTORCYCLE_NOT_FOUND,
  83.                     motorcycleModel
  84.             ));
  85.         }
  86.  
  87.         rider.addMotorcycle(motorcycle);
  88.  
  89.         return String.format(RIDER_RECEIVED_MOTORCYCLE_MESSAGE, riderName, motorcycleModel);
  90.     }
  91.  
  92.     @Override
  93.     public String addRiderToRace(String raceName, String riderName) {
  94.         Race race = this.raceRepository.getByName(raceName);
  95.         Rider rider = this.riderRepository.getByName(riderName);
  96.  
  97.         if (race == null){
  98.             throw new NullPointerException(String.format(
  99.                 ExceptionMessages.RACE_NOT_FOUND,
  100.                     raceName
  101.             ));
  102.         }
  103.  
  104.         if (rider == null){
  105.             throw new NullPointerException(String.format(
  106.                     ExceptionMessages.RIDER_NOT_FOUND,
  107.                     riderName
  108.             ));
  109.         }
  110.  
  111.         race.addRider(rider);
  112.  
  113.         return String.format(RIDER_ADDED_TO_RACE_MESSAGE, riderName, raceName);
  114.     }
  115.  
  116.     @Override
  117.     public String startRace(String raceName) {
  118.         Race race = this.raceRepository.getByName(raceName);
  119.  
  120.         if (race == null){
  121.             throw new NullPointerException(String.format(
  122.                     ExceptionMessages.RACE_NOT_FOUND,
  123.                     raceName
  124.             ));
  125.         }
  126.  
  127.         List<Rider> sortedRiders = this.riderRepository.getAll()
  128.                 .stream()
  129.                 .sorted((r1, r2) -> {
  130.                     Motorcycle m1 = r1.getMotorcycle();
  131.                     Motorcycle m2 = r2.getMotorcycle();
  132.  
  133.                     return Double.compare(m2.calculateRacePoints(race.getLaps()),
  134.                                           m1.calculateRacePoints(race.getLaps()));
  135.  
  136.                 }).collect(Collectors.toList());
  137.  
  138.  
  139.         StringBuilder result = new StringBuilder();
  140.  
  141.         if (sortedRiders.size() < 3){
  142.             throw new IllegalArgumentException(String.format(
  143.                         ExceptionMessages.RACE_INVALID,
  144.                         raceName,
  145.                         MIN_PARTICIPANTS_FOR_RACE
  146.             ));
  147.         } else {
  148.             Rider firstRider = sortedRiders.get(0);
  149.             Rider secondRider = sortedRiders.get(1);
  150.             Rider thirdRider = sortedRiders.get(2);
  151.  
  152.             result.append(String.format(FIRST_RIDER_MESSAGE, firstRider.getName(), raceName)).append(System.lineSeparator())
  153.                     .append(String.format(SECOND_RIDER_MESSAGE, secondRider.getName(), raceName)).append(System.lineSeparator())
  154.                     .append(String.format(THIRD_RIDER_MESSAGE, thirdRider.getName(), raceName)).append(System.lineSeparator());
  155.         }
  156.  
  157.         return result.toString();
  158.     }
  159.  
  160.     @Override
  161.     public String createRace(String name, int laps) {
  162.         Race race = new RaceImpl(name, laps);
  163.         this.raceRepository.add(race);
  164.  
  165.         return String.format(RACE_CREATED, name);
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement