"depot M315"

This commit is contained in:
JunkJumper
2020-05-01 22:28:41 +02:00
parent 91f5bce946
commit 16368c9c8b
125 changed files with 4506 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
package openClosedPrinciples.core;
/**
* @author Mireille Blay-Fornarino
*
*/
public class AlreadyBooked extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
public AlreadyBooked(String message) {
super(message);
}
}

View File

@@ -0,0 +1,14 @@
package openClosedPrinciples.core;
public class AucunItemCorrespondant extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
public AucunItemCorrespondant(String message) {
super(message);
}
}

View File

@@ -0,0 +1,50 @@
package openClosedPrinciples.core;
/**
* @author Mireille Blay-Fornarino
*
*/
public class Car {
private String numberPlate;
private double dayPrice;
/// Constructeurs
public Car(String numberPlate, double dayPrice) {
super();
this.numberPlate = numberPlate;
this.dayPrice = dayPrice;
}
/// Accesseurs et mutateurs
public String getNumberPlate() {
return numberPlate;
}
public void setNumberPlate(String numberPlate) {
this.numberPlate = numberPlate;
}
public double getDayPrice() {
return dayPrice;
}
public void setDayPrice(double dayPrice) {
this.dayPrice = dayPrice;
}
/// Méthodes
// Afficher
@Override
public String toString() {
return "Car [numberPlate=" + numberPlate + // ", rentals=" + rentals +
", dayPrice=" + super.toString() + "]";
}
}

View File

@@ -0,0 +1,77 @@
package openClosedPrinciples.core;
import java.time.LocalDate;
/**
* @author Mireille Blay-Fornarino
*
* 6 oct. 2018
*/
public class CarRental extends PayingItem {
private String carNumber;
private int duration;
private LocalDate beginDate;
/// Constructeurs
private CarRental(String carNumber, double dayPrice, int duration, LocalDate beginDate) {
super(dayPrice);
this.carNumber = carNumber;
this.duration = duration;
this.beginDate = beginDate;
}
public CarRental(Car c, LocalDate beginDate, int duration) {
//On considere que le prix de la location ne doit plus bouger même si le prix de la voiture change
this(c.getNumberPlate(),c.getDayPrice(),duration,beginDate);
}
/// Accesseurs et mutateurs
public String getCarNumber() {
return carNumber;
}
public void setCarNumber(String carNumber) {
this.carNumber = carNumber;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public LocalDate getBeginDate() {
return beginDate;
}
public void setBeginDate(LocalDate beginDate) {
this.beginDate = beginDate;
}
/// Méthodes
// Récupérer le prix
public double getPrice() {
return super.price*duration;
}
// Rechercher si dispo un jour
public boolean includeADate(LocalDate[] dates) {
for (LocalDate d : dates) {
if (d.equals(beginDate))
return true;
if ( (d.isAfter(beginDate)) &&
(d.isBefore(beginDate.plusDays(duration) ) ) )
return true;
}
return false;
}
// Afficher
@Override
public String toString() {
return "CarRental [carNumber=" + carNumber.toString() + ", duration=" + duration + ", beginDate=" + beginDate + ", price=" +super.toString() + "]";
}
}

View File

@@ -0,0 +1,77 @@
package openClosedPrinciples.core;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/**
* @author Mireille Blay-Fornarino
*
* 6 oct. 2018
*/
public class CarRentalService extends Service<CarRental> {
private ArrayList<Car> cars;
/// Constructeurs
public CarRentalService(ArrayList<Car> cars) {
super(new ArrayList<CarRental>());
this.cars = cars;
}
/// Méthodes
// Récupérer la liste des voitures dispo tel date et telle durée
public List<Car> getAvailableCars(LocalDate d, int duration) {
ArrayList<Car> availableCars = new ArrayList<>();
LocalDate[] dates = DateTools.getDays(d, duration);
for (Car c : cars) {
if (isAvailable(c, dates)) {
availableCars.add(c);
}
}
return availableCars;
}
// On voit si une voiture est dispo pendant ces dates
private boolean isAvailable(Car c, LocalDate[] dates) {
for (CarRental carRental : payingItemList) {
if (c.getNumberPlate().equals(carRental.getCarNumber()) &&
(carRental.includeADate(dates)) ) {
return false;
}
}
return true;
}
// On réserve une voiture
public CarRental book(Car c, LocalDate d, int duration) throws NotPossibleCarRentalException {
CarRental carRental = null;
if (cars == null || !(cars.contains(c)) )
throw new NotPossibleCarRentalException("Not known car");
LocalDate[] dates = DateTools.getDays(d, duration);
if (isAvailable(c, dates)) {
carRental = new CarRental(c, d, duration);
payingItemList.add(carRental);
}
return carRental;
}
// Recherche d'une voiture correspondant à la description
public CarRental find(Description description) {
List<Car> listeVoitures = this.getAvailableCars(description.getDateDepart(), description.getDuree());
listeVoitures.sort(Comparator.comparing(Car::getDayPrice));
CarRental cr = null;
try{
if(listeVoitures.isEmpty()) {
return null;
}
cr = this.book(listeVoitures.get(0), description.getDateDepart(), description.getDuree());
}catch(NotPossibleCarRentalException e) {
e.getMessage();
}
return cr;
}
}

View File

@@ -0,0 +1,37 @@
package openClosedPrinciples.core;
import java.time.LocalDate;
/**
* Classe Utilitaire
*
*
* @author Mireille Blay-Fornarino
*
*
* 6 oct. 2018
*/
public final class DateTools {
private DateTools() {
throw new IllegalStateException("Utility class");
}
public static LocalDate addDays(LocalDate date, int nbJour) {
return date.plusDays(nbJour);
}
public static LocalDate[] getDays(LocalDate date, int nbJour) {
int i = 0;
LocalDate[] dates = new LocalDate[nbJour];
dates[i] = date;
i+=1;
while (i < nbJour) {
dates[i] = addDays(date,i);
i++;
}
return dates;
}
}

View File

@@ -0,0 +1,68 @@
package openClosedPrinciples.core;
import java.time.LocalDate;
import java.util.Date;
public class Description {
private String depart;
private String destination;
private LocalDate dateDepart;
private int duree;
/// Constructeurs
// Vide
public Description() {
depart = null;
destination = null;
dateDepart = null;
duree = 0;
}
// Avec tout rempli
public Description(String depart, String destination, LocalDate dateDepart, int duree) {
this.depart = depart;
this.destination = destination;
this.dateDepart = dateDepart;
this.duree = duree;
}
/// Accesseurs et mutateurs
public String getDestination() {
return destination;
}
public void setDestination(String destination) {
this.destination = destination;
}
public String getDepart() {
return depart;
}
public void setDepart(String depart) {
this.depart = depart;
}
public LocalDate getDateDepart() {
return dateDepart;
}
public void setDateDepart(LocalDate dateDepart) {
this.dateDepart = dateDepart;
}
public int getDuree() {
return duree;
}
public void setDuree(int duree) {
this.duree = duree;
}
}

View File

@@ -0,0 +1,92 @@
package openClosedPrinciples.core;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Random;
/**
* @author Mireille Blay-Fornarino
*
* 6 oct. 2018
*/
public class Flight extends PayingItem {
private LocalDate departDate;
private String departAirport;
private String arrivalAirport;
private LocalTime departTime;
/// Constructeurs
public Flight(String departAirport) {
this(-1, LocalDate.now(), LocalTime.now(), departAirport, "Paris");
}
public Flight(double price, LocalDate departDate, LocalTime departTime, String departAirport, String arrivalAirport) {
super(price);
this.departDate = departDate;
this.departAirport = departAirport;
this.arrivalAirport = arrivalAirport;
this.departTime= departTime;
}
/// Accesseurs et mutateurs
public LocalTime getDepartTime() {
return departTime;
}
public void setDepartTime(LocalTime departTime) {
this.departTime = departTime;
}
public LocalDate getDepartDate() {
return departDate;
}
public void setDepartDate(LocalDate departDate) {
this.departDate = departDate;
}
public String getDepartAirport() {
return departAirport;
}
public void setDepartAirport(String departAirport) {
this.departAirport = departAirport;
}
public String getArrivalAirport() {
return arrivalAirport;
}
public void setArrivalAirport(String arrivalAirport) {
this.arrivalAirport = arrivalAirport;
}
public void setPrice(double price) {
super.setPrice(price);
}
public double getPrice() {
if (super.price == -1) {
double start = 10;
double end = 1000;
double random = new Random().nextDouble();
super.setPrice(start + (random * (end - start)));
}
return super.price;
}
/// Méthodes
// On voit si le vol correspond à celui entré en paramètres
public boolean match(LocalDate d, String from, String to) {
return getDepartDate().equals(d) && getDepartAirport().equals(from) && getArrivalAirport().equals(to);
}
// Affichage
@Override
public String toString() {
return "Flight [price=" + super.toString() + ", departDate=" + departDate + ", departAirport=" + departAirport
+ ", arrivalAirport=" + arrivalAirport + ", departTime=" + departTime + "]";
}
}

View File

@@ -0,0 +1,50 @@
package openClosedPrinciples.core;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author Mireille Blay-Fornarino
*
* 6 oct. 2018
*/
public class FlightService extends Service<Flight> {
/// Constructeurs
public FlightService(ArrayList<Flight> flights) {
super(flights);
}
/// Méthodes
// Récupérer les vols de cette date
public List<Flight> getFlights(LocalDate d) {
Stream<Flight> parallelStream = super.getpayingItemList().parallelStream();
Stream<Flight> results = parallelStream.filter(f -> (f.getDepartDate().equals(d)) ) ;
return results.collect(Collectors.toCollection(ArrayList::new));
}
// Récupérer les vols de cette date + lieux départ et destination
public ArrayList<Flight> getFlights(LocalDate d, String from, String to) {
Stream<Flight> parallelStream = super.getpayingItemList().parallelStream();
Stream<Flight> results = parallelStream.filter(f ->
f.match(d, from, to)) ;
return results.collect(Collectors.toCollection(ArrayList::new));
}
// Recherche d'un vol correspondant à la description
public Flight find(Description description) {
ArrayList<Flight> listeVols = this.getFlights(description.getDateDepart(), description.getDepart(), description.getDestination());
listeVols.sort(Comparator.comparing(Flight::getPrice));
if(listeVols.isEmpty()) {
return null;
}
return listeVols.get(0);
}
}

View File

@@ -0,0 +1,112 @@
package openClosedPrinciples.core;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;
import java.util.ArrayList;
import java.util.Scanner;
public class GestionnaireVoyages {
private static TravelOrganizer to = new TravelOrganizer();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//// GESTION DU VOYAGE CHOISI PAR L'UTILISATEUR
/// On récupère le départ
System.out.println("De où partez-vous ?");
String depart = sc.nextLine();
/// On récupère la destination
System.out.println("Où voulez-vous aller ?");
String destination = sc.nextLine();
/// On récupère la durée du voyage
System.out.println("Combien de temps voulez-vous partir?");
int duree = Integer.parseInt(sc.nextLine());
//Date (non demandée par l'utilisateur)
LocalDate ld = LocalDate.of(2019, Month.OCTOBER, 18);
//Date pour tester
LocalDate ldr = LocalDate.of(2019, Month.JUNE, 18);
LocalTime lt = LocalTime.now();
//Description du voyage de l'utilisateur
Description d = new Description(depart, destination, ld, duree);
/// GESTION DES VOLS
// Première compagnie de vols
Flight f1 = new Flight(350, ld, lt, depart, destination);
Flight f2 = new Flight(100, ld, lt, depart, destination); // moins cher
Flight f3 = new Flight(56, ld, lt, "Cannes", destination); // n'a pas le bon départ
ArrayList<Flight> flights1 = new ArrayList<>();
flights1.add(f1);
flights1.add(f2);
flights1.add(f3);
FlightService fs1 = new FlightService(flights1);
to.addAirlineCompany(fs1);
// Deuxième compagnie de vols
Flight f4 = new Flight(20, ld, lt, depart, "Tokyo"); // n'a pas la bonne destination
Flight f5 = new Flight(22, ldr, lt, depart, destination); // n'a pas la bonne date
ArrayList<Flight> flights2 = new ArrayList<>();
flights2.add(f4);
flights2.add(f5);
FlightService fs2 = new FlightService(flights2);
to.addAirlineCompany(fs2);
/// GESTION DES LOCATIONS DE VOITURES
// Première compagnie qui contiendra 3 voitures
Car c1 = new Car("111", 10); //sera réservée plus tard
Car c2 = new Car("222", 15);
Car c3 = new Car("333", 30); //sera réservée plus tard
ArrayList<Car> cs1 = new ArrayList<>();
cs1.add(c1);
cs1.add(c2);
cs1.add(c3);
CarRentalService crs1 = new CarRentalService(cs1);
to.addCarRentCompany(crs1);
// Deuxième compagnie qui contiendra 2 voitures S
Car c4 = new Car("444", 4); //voiture la moins chère, mais sera réservée plus tard
Car c5 = new Car("555", 50);
ArrayList<Car> cs2 = new ArrayList<>();
cs2.add(c4);
cs2.add(c5);
CarRentalService crs2 = new CarRentalService(cs2);
to.addCarRentCompany(crs2);
try {
// On réserve 3 voitures
crs1.book(c1, ld, duree);
crs1.book(c3, ld, duree);
crs2.book(c4, ld, duree);
} catch (NotPossibleCarRentalException e) {
System.out.println(e.getMessage());
}
try {
Trip voyageChoisi = to.createATrip(d);
//Affichage du meilleur vol
System.out.println("Le meilleur vol pour partir de "+ depart +" et arriver à " + destination +
" le 18/10/2019 est : \n\t" + voyageChoisi.getItems().get(0));
//Affichage de la meilleure location de voiture
System.out.println("La meilleure voiture à louer pour une durée de " + duree +
" jours à partir du " + ld +" est : \n\t" + voyageChoisi.getItems().get(1));
} catch(AucunItemCorrespondant e) {
System.out.println(e.getMessage());
}
}
}

View File

@@ -0,0 +1,19 @@
package openClosedPrinciples.core;
/**
* @author Mireille Blay-Fornarino
*
* 6 oct. 2018
*/
public class NotPossibleCarRentalException extends Exception {
/**
*
*/
private static final long serialVersionUID = 1L;
public NotPossibleCarRentalException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,30 @@
package openClosedPrinciples.core;
public abstract class PayingItem {
protected double price;
/// Constructeurs
public PayingItem(double price) {
this.price = price;
}
/// Accesseurs et mutateurs
// Méthode abstraite parce que le calcul du prix n'est pas le même pour une voiture et pour une vol
public abstract double getPrice();
public void setPrice(double price) {
this.price = price;
}
/// Méthodes
// Affichage
@Override
public String toString() {
return "" + price;
}
}

View File

@@ -0,0 +1,46 @@
package openClosedPrinciples.core;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public abstract class Service<T extends PayingItem>{
protected List<T> payingItemList = new ArrayList<>();
/// Constructeurs
public Service(List<T> payingItemList) {
this.payingItemList = payingItemList;
}
/// Accesseur
protected List<T> getpayingItemList(){
return payingItemList;
}
/// Méthodes
// Trier la liste d'item par prix
public List<T> sortedByPrice() {
payingItemList.sort(Comparator.comparing(PayingItem::getPrice));
return payingItemList;
}
// Récupérer l'item le moins cher
public T lessExpensiveItem(){
sortedByPrice();
return payingItemList.get(0);
}
// Ajouter un item à la liste
public void add(T payingItem) {
payingItemList.add(payingItem);
}
// Trouver
public abstract T find(Description description) throws AucunItemCorrespondant;
}

View File

@@ -0,0 +1,99 @@
package openClosedPrinciples.core;
import java.time.LocalDate;
import java.time.LocalTime;
import java.time.Month;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class TravelOrganizer {
List<FlightService> airlinesCompanies;
List<CarRentalService> carRentCompanies;
/// Constructeurs
public TravelOrganizer() {
airlinesCompanies = new ArrayList<>();
carRentCompanies = new ArrayList<>();
}
/// Accesseurs et mutateurs
public List<FlightService> getAirlinesCompanies() {
return airlinesCompanies;
}
public void setAirlinesCompanies(List<FlightService> airlinesCompanies) {
this.airlinesCompanies = airlinesCompanies;
}
public List<CarRentalService> getCarRentCompanies() {
return carRentCompanies;
}
public void setCarRentCompanies(List<CarRentalService> carRentCompanies) {
this.carRentCompanies = carRentCompanies;
}
/// Méthodes
// Ajouter une compagnie aérienne
public void addAirlineCompany(FlightService company) {
airlinesCompanies.add(company);
}
// Ajouter une compagnie de location de voitures
public void addCarRentCompany(CarRentalService company) {
carRentCompanies.add(company);
}
// Créer un voyage
public Trip createATrip(Description description) throws AucunItemCorrespondant {
Trip t = new Trip(description);
Flight f = this.findFlight(description);
CarRental cr = this.findCar(description);
if(f!=null && cr!=null) {
t.addItem(f);
t.addItem(cr);
}else {
throw new AucunItemCorrespondant("Aucun voyage ne correspond aux critères recherchés.");
}
return t;
}
// Recherche un vol
public Flight findFlight(Description description) throws AucunItemCorrespondant {
List<Flight> flights = new ArrayList();
for(FlightService fs : airlinesCompanies) {
Flight f = fs.find(description);
if(f != null)
flights.add(f);
}
flights.sort(Comparator.comparing(Flight::getPrice));
if(flights.isEmpty()) {
throw new AucunItemCorrespondant("Aucun vol correspondant à votre recherche.");
}
return flights.get(0);
}
// Recherche une voiture
public CarRental findCar(Description description) throws AucunItemCorrespondant {
List<CarRental> cars = new ArrayList();
for(CarRentalService c : carRentCompanies) {
CarRental cr = c.find(description);
if(cr!=null)
cars.add(cr);
}
cars.sort(Comparator.comparing(CarRental::getPrice));
if(cars.isEmpty()) {
throw new AucunItemCorrespondant("Aucune location de voiture correspondant à votre recherche.");
}
return cars.get(0);
}
}

View File

@@ -0,0 +1,49 @@
package openClosedPrinciples.core;
import java.util.ArrayList;
import java.util.List;
public class Trip {
private Description description;
private List<PayingItem> items;
/// Constructeurs
// Vide
public Trip() {
description = null;
items = new ArrayList();
}
// Avec la description
public Trip(Description description) {
this.description = description;
items = new ArrayList();
}
/// Accesseurs et mutateurs
public Description getDescription() {
return description;
}
public void setDescription(Description description) {
this.description = description;
}
public List<PayingItem> getItems() {
return items;
}
public void setItems(List<PayingItem> items) {
this.items = items;
}
/// Méthodes
public void addItem(PayingItem item) {
items.add(item);
}
}