Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package motocrossWorldChampionship.core;
- import motocrossWorldChampionship.common.ExceptionMessages;
- import motocrossWorldChampionship.core.interfaces.ChampionshipController;
- import motocrossWorldChampionship.entities.PowerMotorcycle;
- import motocrossWorldChampionship.entities.RaceImpl;
- import motocrossWorldChampionship.entities.RiderImpl;
- import motocrossWorldChampionship.entities.SpeedMotorcycle;
- import motocrossWorldChampionship.entities.interfaces.Motorcycle;
- import motocrossWorldChampionship.entities.interfaces.Race;
- import motocrossWorldChampionship.entities.interfaces.Rider;
- import motocrossWorldChampionship.repositories.MotorcycleRepository;
- import motocrossWorldChampionship.repositories.RaceRepository;
- import motocrossWorldChampionship.repositories.RiderRepository;
- import motocrossWorldChampionship.repositories.interfaces.Repository;
- import java.util.List;
- import java.util.stream.Collectors;
- public class ChampionshipControllerImpl implements ChampionshipController {
- private static final String RIDER_CREATED = "Rider %s is created.";
- private static final String MOTORCYCLE_CREATED = "%s %s is created.";
- private static final String RIDER_RECEIVED_MOTORCYCLE_MESSAGE = "Rider %s received motorcycle %s.";
- private static final String RIDER_ADDED_TO_RACE_MESSAGE = "Rider %s added in %s race.";
- private static final String RACE_CREATED = "Race %s is created.";
- private static final String FIRST_RIDER_MESSAGE = "Rider %s wins %s race.";
- private static final String SECOND_RIDER_MESSAGE = "Rider %s is second in %s race.";
- private static final String THIRD_RIDER_MESSAGE = "Rider %s is third in %s race.";
- private static final int MIN_PARTICIPANTS_FOR_RACE = 3;
- private Repository<Rider> riderRepository;
- private Repository<Motorcycle> motorcycleRepository;
- private Repository<Race> raceRepository;
- public ChampionshipControllerImpl() {
- this.riderRepository = new RiderRepository();
- this.motorcycleRepository = new MotorcycleRepository();
- this.raceRepository = new RaceRepository();
- }
- @Override
- public String createRider(String riderName) {
- Rider rider = new RiderImpl(riderName);
- this.riderRepository.add(rider);
- return String.format(RIDER_CREATED, riderName);
- }
- @Override
- public String createMotorcycle(String type, String model, int horsePower) {
- Motorcycle motorcycle = null;
- switch (type){
- case "Power":
- motorcycle = new PowerMotorcycle(model, horsePower);
- break;
- case "Speed":
- motorcycle = new SpeedMotorcycle(model, horsePower);
- break;
- }
- this.motorcycleRepository.add(motorcycle);
- return String.format(MOTORCYCLE_CREATED, motorcycle.getClass().getSimpleName(), motorcycle.getModel());
- }
- @Override
- public String addMotorcycleToRider(String riderName, String motorcycleModel) {
- Rider rider = this.riderRepository.getByName(riderName);
- Motorcycle motorcycle = this.motorcycleRepository.getByName(motorcycleModel);
- if (rider == null){
- throw new NullPointerException(String.format(
- ExceptionMessages.RIDER_NOT_FOUND,
- riderName
- ));
- }
- if (motorcycle == null){
- throw new NullPointerException(String.format(
- ExceptionMessages.MOTORCYCLE_NOT_FOUND,
- motorcycleModel
- ));
- }
- rider.addMotorcycle(motorcycle);
- return String.format(RIDER_RECEIVED_MOTORCYCLE_MESSAGE, riderName, motorcycleModel);
- }
- @Override
- public String addRiderToRace(String raceName, String riderName) {
- Race race = this.raceRepository.getByName(raceName);
- Rider rider = this.riderRepository.getByName(riderName);
- if (race == null){
- throw new NullPointerException(String.format(
- ExceptionMessages.RACE_NOT_FOUND,
- raceName
- ));
- }
- if (rider == null){
- throw new NullPointerException(String.format(
- ExceptionMessages.RIDER_NOT_FOUND,
- riderName
- ));
- }
- race.addRider(rider);
- return String.format(RIDER_ADDED_TO_RACE_MESSAGE, riderName, raceName);
- }
- @Override
- public String startRace(String raceName) {
- Race race = this.raceRepository.getByName(raceName);
- if (race == null){
- throw new NullPointerException(String.format(
- ExceptionMessages.RACE_NOT_FOUND,
- raceName
- ));
- }
- List<Rider> sortedRiders = this.riderRepository.getAll()
- .stream()
- .sorted((r1, r2) -> {
- Motorcycle m1 = r1.getMotorcycle();
- Motorcycle m2 = r2.getMotorcycle();
- return Double.compare(m2.calculateRacePoints(race.getLaps()),
- m1.calculateRacePoints(race.getLaps()));
- }).collect(Collectors.toList());
- StringBuilder result = new StringBuilder();
- if (sortedRiders.size() < 3){
- throw new IllegalArgumentException(String.format(
- ExceptionMessages.RACE_INVALID,
- raceName,
- MIN_PARTICIPANTS_FOR_RACE
- ));
- } else {
- Rider firstRider = sortedRiders.get(0);
- Rider secondRider = sortedRiders.get(1);
- Rider thirdRider = sortedRiders.get(2);
- result.append(String.format(FIRST_RIDER_MESSAGE, firstRider.getName(), raceName)).append(System.lineSeparator())
- .append(String.format(SECOND_RIDER_MESSAGE, secondRider.getName(), raceName)).append(System.lineSeparator())
- .append(String.format(THIRD_RIDER_MESSAGE, thirdRider.getName(), raceName)).append(System.lineSeparator());
- }
- return result.toString();
- }
- @Override
- public String createRace(String name, int laps) {
- Race race = new RaceImpl(name, laps);
- this.raceRepository.add(race);
- return String.format(RACE_CREATED, name);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement