SHARE
TWEET

Untitled

shniaga Aug 10th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package motocrossWorldChampionship.core;
  2.  
  3. import motocrossWorldChampionship.common.ExceptionMessages;
  4. import motocrossWorldChampionship.common.OutputMessages;
  5. import motocrossWorldChampionship.core.interfaces.ChampionshipController;
  6. import motocrossWorldChampionship.entities.PowerMotorcycle;
  7. import motocrossWorldChampionship.entities.RaceImpl;
  8. import motocrossWorldChampionship.entities.RiderImpl;
  9. import motocrossWorldChampionship.entities.SpeedMotorcycle;
  10. import motocrossWorldChampionship.entities.interfaces.Motorcycle;
  11. import motocrossWorldChampionship.entities.interfaces.Race;
  12. import motocrossWorldChampionship.entities.interfaces.Rider;
  13. import motocrossWorldChampionship.repositories.interfaces.Repository;
  14.  
  15. import java.util.List;
  16. import java.util.stream.Collectors;
  17.  
  18.  
  19. public class ChampionshipControllerImpl implements ChampionshipController {
  20.     private static final int MINIMUM_PARTICIPANTS_IN_RACE_NEEDED = 3;
  21.     private Repository<Rider> riderRepository;
  22.     private Repository<Motorcycle> motorCycleRepository;
  23.     private Repository<Race> raceRepository;
  24.  
  25.     public ChampionshipControllerImpl(Repository<Rider> riderRepository,
  26.                                       Repository<Motorcycle> motorCycleRepository,
  27.                                       Repository<Race> raceRepository) {
  28.         this.riderRepository = riderRepository;
  29.         this.motorCycleRepository = motorCycleRepository;
  30.         this.raceRepository = raceRepository;
  31.     }
  32.  
  33.     @Override
  34.     public String createRider(String riderName) {
  35.         if (this.riderRepository.getByName(riderName) != null) {
  36.             throw new IllegalArgumentException(String.format(ExceptionMessages.RIDER_EXISTS, riderName));
  37.         }
  38.         this.riderRepository.add(new RiderImpl(riderName));
  39.         return String.format(OutputMessages.RIDER_CREATED, riderName);
  40.     }
  41.  
  42.     @Override
  43.     public String createMotorcycle(String type, String model, int horsePower) {
  44.         if (this.motorCycleRepository.getByName(model) != null) {
  45.             throw new IllegalArgumentException(String.format(ExceptionMessages.MOTORCYCLE_EXISTS, model));
  46.         }
  47.         Motorcycle motorcycle = null;
  48.         if (type.equals("Speed")) {
  49.             motorcycle = new SpeedMotorcycle(model, horsePower);
  50.         } else {
  51.             motorcycle = new PowerMotorcycle(model, horsePower);
  52.         }
  53.         this.motorCycleRepository.add(motorcycle);
  54.         return String.format(OutputMessages.MOTORCYCLE_CREATED, type + "Motorcycle", model);
  55.     }
  56.  
  57.     @Override
  58.     public String addMotorcycleToRider(String riderName, String motorcycleModel) {
  59.         Rider rider = this.riderRepository.getByName(riderName);
  60.         Motorcycle motorcycle = this.motorCycleRepository.getByName(motorcycleModel);
  61.  
  62.         if (rider == null) {
  63.             throw new IllegalArgumentException(String.format(ExceptionMessages.RIDER_NOT_FOUND, riderName));
  64.         }
  65.         if (motorcycle == null) {
  66.             throw new NullPointerException(String.format(ExceptionMessages.MOTORCYCLE_NOT_FOUND, motorcycleModel));
  67.         }
  68.  
  69.         rider.addMotorcycle(motorcycle);
  70.         return String.format(OutputMessages.MOTORCYCLE_ADDED, riderName, motorcycleModel);
  71.  
  72.     }
  73.  
  74.     @Override
  75.     public String addRiderToRace(String raceName, String riderName) {
  76.         Race race = this.raceRepository.getByName(raceName);
  77.         Rider rider = this.riderRepository.getByName(riderName);
  78.  
  79.         if (race == null) {
  80.             throw new NullPointerException(String.format(ExceptionMessages.RACE_NOT_FOUND, raceName));
  81.         }
  82.  
  83.         if (rider == null) {
  84.             throw new IllegalArgumentException(String.format(ExceptionMessages.RIDER_NOT_FOUND, riderName));
  85.         }
  86.  
  87.         race.addRider(rider);
  88.         return String.format(OutputMessages.RIDER_ADDED, riderName, raceName);
  89.     }
  90.  
  91.     @Override
  92.     public String startRace(String raceName) {
  93.         Race race = this.raceRepository.getByName(raceName);
  94.         if (race == null) {
  95.             throw new NullPointerException
  96.                     (String.format(ExceptionMessages.RACE_NOT_FOUND, raceName));
  97.         }
  98.         int raceParticipants = race.getRiders().size();
  99.         if (raceParticipants < MINIMUM_PARTICIPANTS_IN_RACE_NEEDED) {
  100.             throw new IllegalArgumentException
  101.                     (String.format(ExceptionMessages.RACE_INVALID, raceName, MINIMUM_PARTICIPANTS_IN_RACE_NEEDED));
  102.         }
  103.         List<Rider> winners = race.getRiders().stream()
  104.                 .sorted((f, s) -> Double.compare(s.getMotorcycle().calculateRacePoints(race.getLaps()),
  105.                         f.getMotorcycle().calculateRacePoints(race.getLaps()))).limit(3)
  106.                 .collect(Collectors.toList());
  107.  
  108.  
  109.  
  110.  
  111.         StringBuilder sb = new StringBuilder();
  112.         winners.get(0).winRace();
  113.  
  114.         sb
  115.                 .append(String.format(OutputMessages.RIDER_FIRST_POSITION, winners.get(0).getName(), race.getName()))
  116.                 .append(System.lineSeparator())
  117.                 .append(String.format(OutputMessages.RIDER_SECOND_POSITION, winners.get(1).getName(), race.getName()))
  118.                 .append(System.lineSeparator())
  119.                 .append(String.format(OutputMessages.RIDER_THIRD_POSITION, winners.get(2).getName(), race.getName()));
  120.  
  121.         this.raceRepository.remove(race);
  122.  
  123.         return sb.toString().trim();
  124.     }
  125.  
  126.  
  127.     @Override
  128.     public String createRace(String name, int laps) {
  129.         if (this.raceRepository.getByName(name) != null) {
  130.             throw new IllegalArgumentException(String.format(ExceptionMessages.RACE_EXISTS, name));
  131.         }
  132.         this.raceRepository.add(new RaceImpl(name, laps));
  133.         return String.format(OutputMessages.RACE_CREATED, name);
  134.     }
  135. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top