From 5da7508ac7f593a35baac296d6b418ac6e3dfc0c Mon Sep 17 00:00:00 2001 From: JunkJumper Date: Wed, 14 Oct 2020 13:59:30 +0200 Subject: [PATCH] TD4 --- 2020-2021/src/TD2/ForumManager.java | 3 + 2020-2021/src/TD2/TD2.ucls | 174 +++++++++++------ 2020-2021/src/TD2/interactions/App.java | 20 ++ .../src/TD2/interactions/Controleur.java | 180 ++++++++++++++++++ 2020-2021/src/TD2/interactions/Memory.java | 62 ++++++ .../src/TD2/interactions/UserConsole.java | 102 ++++++++++ 2020-2021/src/TD4/cars/Car.java | 43 +++++ 2020-2021/src/TD4/cars/CarRental.java | 96 ++++++++++ 2020-2021/src/TD4/cars/CarRentalService.java | 91 +++++++++ 2020-2021/src/TD4/core/PayingItem.java | 5 + 2020-2021/src/TD4/core/Service4PI.java | 37 ++++ 2020-2021/src/TD4/flights/Flight.java | 95 +++++++++ 2020-2021/src/TD4/flights/FlightService.java | 62 ++++++ 2020-2021/src/TD4/util/AlreadyBooked.java | 20 ++ 2020-2021/src/TD4/util/DateTools.java | 50 +++++ .../util/NotPossibleCarRentalException.java | 19 ++ .../tests/TD4/cars/CarRentalServiceTest.java | 104 ++++++++++ 2020-2021/tests/TD4/cars/CarRentalTest.java | 61 ++++++ .../tests/TD4/flights/FlightServiceTest.java | 75 ++++++++ 2020-2021/tests/TD4/flights/FlightTest.java | 29 +++ 20 files changed, 1275 insertions(+), 53 deletions(-) create mode 100644 2020-2021/src/TD2/interactions/App.java create mode 100644 2020-2021/src/TD2/interactions/Controleur.java create mode 100644 2020-2021/src/TD2/interactions/Memory.java create mode 100644 2020-2021/src/TD2/interactions/UserConsole.java create mode 100644 2020-2021/src/TD4/cars/Car.java create mode 100644 2020-2021/src/TD4/cars/CarRental.java create mode 100644 2020-2021/src/TD4/cars/CarRentalService.java create mode 100644 2020-2021/src/TD4/core/PayingItem.java create mode 100644 2020-2021/src/TD4/core/Service4PI.java create mode 100644 2020-2021/src/TD4/flights/Flight.java create mode 100644 2020-2021/src/TD4/flights/FlightService.java create mode 100644 2020-2021/src/TD4/util/AlreadyBooked.java create mode 100644 2020-2021/src/TD4/util/DateTools.java create mode 100644 2020-2021/src/TD4/util/NotPossibleCarRentalException.java create mode 100644 2020-2021/tests/TD4/cars/CarRentalServiceTest.java create mode 100644 2020-2021/tests/TD4/cars/CarRentalTest.java create mode 100644 2020-2021/tests/TD4/flights/FlightServiceTest.java create mode 100644 2020-2021/tests/TD4/flights/FlightTest.java diff --git a/2020-2021/src/TD2/ForumManager.java b/2020-2021/src/TD2/ForumManager.java index d1f8f20..3725554 100644 --- a/2020-2021/src/TD2/ForumManager.java +++ b/2020-2021/src/TD2/ForumManager.java @@ -3,6 +3,7 @@ package TD2; import java.util.ArrayList; import java.util.List; + public class ForumManager { private List l; @@ -32,5 +33,7 @@ public class ForumManager { public List getListeForum() { return this.l; } + + } diff --git a/2020-2021/src/TD2/TD2.ucls b/2020-2021/src/TD2/TD2.ucls index 76fb7ca..664c925 100644 --- a/2020-2021/src/TD2/TD2.ucls +++ b/2020-2021/src/TD2/TD2.ucls @@ -64,118 +64,186 @@ - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - + - + - - - - - - - - - - - - - + - + - - - + + + - + - - - - + + + + + + + + - + - - - - - + + + + + + + + + + + - + - + + + + + + + + + + + + + + + + + + + + + + + + + - - + + + + + + - - - - - - - - + + + + - + - + - + - - - + + + + + + + + + + + - + - + ensembleDesNomsDeForumsExistants = registre.getNomForums(); + String nomDuForum = + ui.getNomDuForum(ensembleDesNomsDeForumsExistants); + Forum forumNouveau = registre.creerForum(nomDuForum); + if (forumNouveau != null) + ui.afficher("Forum " + nomDuForum + ":"+ forumNouveau); + else + ui.afficher("Pbme de creation Forum " + nomDuForum); + } + + private void creerCanal(){ + Forum forum = getForum(); + if (forum != null) { + //Récupérer les noms des canaux pré-existants + Set nomDesCanaux = forum.getChannelNames(); + String nomDeCanal = ui.getNomCanal(nomDesCanaux); + if (forum.addChannel(nomDeCanal)) + ui.afficher("Canal créé : "+ nomDeCanal); + else + ui.afficher("Canal non créé : "+ nomDeCanal); + } + } + + private void creerCanalDeBreves() { + Forum forum = getForum(); + if (forum != null) { + //Récupérer les noms des canaux pré-existants + Set nomDesCanaux = forum.getChannelNames(); + String nomDeCanal = ui.getNomCanal(nomDesCanaux); + int maxSize = ui.getSize(); + if (forum.addChannelOfBriefs(nomDeCanal,maxSize )) + ui.afficher("Canal de brève créé : "+ nomDeCanal); + else + ui.afficher("Canal de brève non créé : "+ nomDeCanal); + } + } + + private void posterMessage() { + Forum forum = getForum(); + if (forum == null) { + return; + } + String nomDeCanal = ui.getNomCanal(forum.getChannelNames()); + String msg = ui.getValeur("saisir le message : "); + + Message message = new Message(msg,currentMember ); + + if(nomDeCanal.equals("*")){ + for(Channel c : forum.getChannels()){ + c.addMessage(message); + } + return; + } + Channel canal = forum.getChannel(nomDeCanal); + if (canal == null) + ui.afficher("Ce Canal n'existe pas"); + else { + if (canal.addMessage(message)) + ui.afficher("Message envoyé"); + else + ui.afficher("Probleme a l'envoi"); + ui.afficher("Messages dans " + nomDeCanal); + ui.afficherMessages(canal.getMessages()); + } + + } + + private void lireMessage() { + Forum forum = getForum(); + if (forum == null) { + return; + } + String nomDeCanal = ui.getNomCanal(forum.getChannelNames()); + Channel canal = forum.getChannel(nomDeCanal); + if (canal == null) + ui.afficher("Ce Canal "+ nomDeCanal + " n'existe pas"); + else { + ui.afficherMessages(canal.getNewMessages(currentMember)); + } + } +*/ +} + + diff --git a/2020-2021/src/TD2/interactions/Memory.java b/2020-2021/src/TD2/interactions/Memory.java new file mode 100644 index 0000000..9acd559 --- /dev/null +++ b/2020-2021/src/TD2/interactions/Memory.java @@ -0,0 +1,62 @@ +package TD2.interactions; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Based on http://www.jmdoudoux.fr/java/dej/chap-serialisation.htm + * + * @author Mireille Blay + * + */ + +public class Memory { + + static Logger monLog = Logger.getLogger(Memory.class.getName()); + + private Memory() { + super(); + } + + + /** + * @param Object to save in a file, it must be serializable + * @param file Name where to write the "object" + * @throws IOException + */ + public static void save (Object o, String fileName) throws IOException{ + ObjectOutputStream oos = null; + try (FileOutputStream fichier = new FileOutputStream(fileName)){ + oos = new ObjectOutputStream(fichier); + oos.writeObject(o); + oos.flush(); + } + } + + /** + * @param fileName + * @return an object corresponding to the serialization written in the file. + * @throws IOException + * @throws ClassNotFoundException + */ + public static Object read (String fileName) throws IOException, ClassNotFoundException{ + ObjectInputStream ois = null; + Object o = null; + File f = new File(fileName); + if(!(f.isFile())) + { + return null; + } + try (FileInputStream fichier = new FileInputStream(fileName)){ + ois = new ObjectInputStream(fichier); + o = ois.readObject(); + } + return o; + } +} diff --git a/2020-2021/src/TD2/interactions/UserConsole.java b/2020-2021/src/TD2/interactions/UserConsole.java new file mode 100644 index 0000000..9ee82d0 --- /dev/null +++ b/2020-2021/src/TD2/interactions/UserConsole.java @@ -0,0 +1,102 @@ +package TD2.interactions; + +import java.util.List; +import java.util.Scanner; +import java.util.Set; + +import TD2.Message; + +public class UserConsole { + Scanner sc = new Scanner(System.in); + + + public static final char COM_CREER_FORUM = 'f'; + public static final char COM_CREER_CANAL = 'c'; + public static final char COM_LIRE_MESSAGES = 'l'; + public static final char COM_POSTER_MESSAGES = 'p'; + public static final char COM_STOP = 's'; + public static final char COM_CHANGER_MEMBRE = 'm'; + public static final char COM_INSCRIRE = 'i'; + public static final char COM_CREER_CANAL_DE_BREVES = 'b'; + + //On pourra modifier facilement pour par exemple utiliser des logger. + private final void sop(String s) { + System.out.println(s); + } + + public String lireCommande() { + sop("------- COMMANDES ----------"); + sop("creer Forum : " + COM_CREER_FORUM ) ; + sop("creer Canal : " + COM_CREER_CANAL); + sop("creer Canal de brèves: " + COM_CREER_CANAL_DE_BREVES); + sop("lire messages : " + COM_LIRE_MESSAGES); + sop("poster un nouveau message : " + COM_POSTER_MESSAGES); + sop("changer de membre : " + COM_CHANGER_MEMBRE ) ; + sop("s'inscrire : " + COM_INSCRIRE ) ; + sop("stop : " + COM_STOP); + sop("Que voulez-vous faire ?"); + String str = sc.nextLine().toLowerCase(); + sop("Vous avez saisi la commande: " + str); + return str; + } + + public void afficher(String str) { + sop(str); + } + + + public String getNomMembre() { + sop("quel est votre nom ?"); + return sc.nextLine(); + } + + public void afficherMessages(List arrayList) { + int i = 1; + sop("\t messages : "); + if (arrayList == null) { + sop("\t Pas de messages "); + } + else + for (Message m : arrayList) { + sop("\t" + i + "-\t" + m); + i++; + } + } + + public String getNomDuForum(Set nomsForum) { + sop("--- Forum disponibles --- "); + afficheListe(nomsForum); + sop("==> Nom du Forum ?"); + return sc.nextLine(); + } + + public void afficheListe(Set noms) { + for (String nom : noms) + sop("\t"+ nom); + } + + + public String getNomCanal(Set nomsCanaux) { + sop("--- Canals disponibles --- "); + afficheListe(nomsCanaux); + sop("==> Nom du Canal ?"); + return sc.nextLine(); + } + + + public String getValeur(String message) { + sop(message); + return sc.nextLine(); + } + + public String write(){ + return sc.nextLine(); + } + + public int getSize() { + sop("===> quelle taille ? "); + int i = sc.nextInt(); + sc.nextLine(); + return i; + } +} diff --git a/2020-2021/src/TD4/cars/Car.java b/2020-2021/src/TD4/cars/Car.java new file mode 100644 index 0000000..be61e2d --- /dev/null +++ b/2020-2021/src/TD4/cars/Car.java @@ -0,0 +1,43 @@ +package TD4.cars; + + + +/** + * This class defines the notion of a car to be rented by associating it with a price per day. + * + * @author Mireille Blay-Fornarino + * + * @version 1.0 + */ + +public class Car { + + private String numberPlate; + private double dayPrice; + + + public Car(String numberPlate, double dayPrice) { + this.numberPlate = numberPlate; + this.dayPrice = dayPrice; + } + + 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; + } + + @Override + public String toString() { + return "Car [numberPlate=" + numberPlate + // ", rentals=" + rentals + + ", dayPrice=" + dayPrice + "]"; + } + +} diff --git a/2020-2021/src/TD4/cars/CarRental.java b/2020-2021/src/TD4/cars/CarRental.java new file mode 100644 index 0000000..7341d19 --- /dev/null +++ b/2020-2021/src/TD4/cars/CarRental.java @@ -0,0 +1,96 @@ +package TD4.cars; + +import java.time.LocalDate; + +/** + * + * The Car Rental class represents the rental of a car, for a given duration and start date. + * + * @author Mireille Blay-Fornarino + * + * 6 + */ +/** + * @author blay + * + */ +public class CarRental { + + private Car car; + private double dayPrice; + private int duration; + private LocalDate beginDate; + + + private CarRental(Car car, double dayPrice, int duration, LocalDate beginDate) { + super(); + this.car = car; + this.dayPrice = dayPrice; + this.duration = duration; + this.beginDate = beginDate; + } + + /** + * the price of the rental should not change even if the price of the car changes. + * @param c : the car to rent + * @param beginDate : the date from which the car is rented out + * @param duration : rental duration in days + */ + public CarRental(Car c, LocalDate beginDate, int duration) { + this(c,c.getDayPrice(),duration,beginDate); + } + + public String getCarNumber() { + return car.getNumberPlate(); + } + + public Car getCar() { + return car; + } + + /** + * When we change the car assigned to a rental, the price does not change. + * @param aCar + */ + public void setCar(Car aCar) { + this.car = aCar; + } + + 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; + } + + public double getPrice() { + return dayPrice*duration; + } + + /** + * + * @param dates : an array of dates + * @return true when one of the dates in the parameter is included in the duration of the car rental + */ + 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; + } + + +} diff --git a/2020-2021/src/TD4/cars/CarRentalService.java b/2020-2021/src/TD4/cars/CarRentalService.java new file mode 100644 index 0000000..7ec358a --- /dev/null +++ b/2020-2021/src/TD4/cars/CarRentalService.java @@ -0,0 +1,91 @@ +package TD4.cars; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; + +import TD4.util.DateTools; +import TD4.util.NotPossibleCarRentalException; + + +/** + * + * This class allows the management of a set of car rentals + * + * + * @author Mireille Blay-Fornarino + * + * + */ +public class CarRentalService { + + //Set of cars for rent + private List cars; + + //All registered car rentals + private List carRentals = new ArrayList<>(); + + + + + + + //To create a car rental service, you need to have cars. + public CarRentalService(List cars) { + super(); + this.cars = cars; + } + + + /** + * @param d : first day + * @param nbDays : number of days + * @return the available cars from the first day during {@code nbDays} days + */ + public List getAvailableCars(LocalDate d, int nbDays) { + ArrayList availableCars = new ArrayList<>(); + LocalDate[] dates = DateTools.getDays(d, nbDays); + for (Car c : cars) { + if (isAvailable(c, dates)) { + availableCars.add(c); + } + } + return availableCars; + } + + + private boolean isAvailable(Car c, LocalDate[] dates) { + for (CarRental carRental : carRentals) { + if (c.equals(carRental.getCar()) && + (carRental.includeADate(dates)) ) { + return false; + } + } + return true; + } + + + + /** + * It books the car rental and returns the created {@code CarRental} + * @param c : {@code Car} for rent + * @param fromDate : {@code LocalDate} first day for rental + * @param numberOfDays + * @return the rental of {@code Car} {@code c} from the first day {@code fromDate} during {@code numberOfDays} + * @throws NotPossibleCarRentalException + */ + public CarRental book(Car c, LocalDate fromDate, int numberOfDays) throws NotPossibleCarRentalException { + CarRental carRental = null; + if (cars == null || !(cars.contains(c)) ) + throw new NotPossibleCarRentalException("Not known car"); + LocalDate[] dates = DateTools.getDays(fromDate, numberOfDays); + if (isAvailable(c, dates)) { + carRental = new CarRental(c, fromDate, numberOfDays); + carRentals.add(carRental); + } + return carRental; + } + + + +} diff --git a/2020-2021/src/TD4/core/PayingItem.java b/2020-2021/src/TD4/core/PayingItem.java new file mode 100644 index 0000000..ffd432f --- /dev/null +++ b/2020-2021/src/TD4/core/PayingItem.java @@ -0,0 +1,5 @@ +package TD4.core; + +public interface PayingItem { + public double getPrice(); +} diff --git a/2020-2021/src/TD4/core/Service4PI.java b/2020-2021/src/TD4/core/Service4PI.java new file mode 100644 index 0000000..d2597d5 --- /dev/null +++ b/2020-2021/src/TD4/core/Service4PI.java @@ -0,0 +1,37 @@ +package TD4.core; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; + + + +public class Service4PI { + protected List payingItemList = new ArrayList<>(); + + public Service4PI(List payingItemList) { + this.payingItemList = payingItemList; + } + + protected List getpayingItemList() { + return payingItemList; + } + + public List sortedByPrice() { + payingItemList.sort(Comparator.comparing(PayingItem::getPrice)); + return new ArrayList(payingItemList); + } + + private void sortedByPriceInPlace() { + payingItemList.sort(Comparator.comparing(PayingItem::getPrice)); + } + public T lessExpensiveItem() { + sortedByPriceInPlace(); + return payingItemList.get(0); + } + + public void add(T payingItem) { + payingItemList.add(payingItem); + } + +} \ No newline at end of file diff --git a/2020-2021/src/TD4/flights/Flight.java b/2020-2021/src/TD4/flights/Flight.java new file mode 100644 index 0000000..03276a2 --- /dev/null +++ b/2020-2021/src/TD4/flights/Flight.java @@ -0,0 +1,95 @@ +package TD4.flights; + +import java.time.LocalDate; +import java.time.LocalTime; +import java.util.Random; + +/** + * + * + * Simplified definition of a flight + * departing from a given airport on a given date. + * It is assumed that the flight arrives at the destination airport on the same day. + * + * @author Mireille Blay-Fornarino + * + * + */ +public class Flight { + + private static final int NOT_ASSIGNED = -1; + private static final String DEFAULT_DESTINATION= "Paris"; + private double price = NOT_ASSIGNED; + private LocalDate departDate; + private String departureAirport; + private String arrivalAirport; + private LocalTime departureTime; + + public LocalTime getDepartTime() { + return departureTime; + } + public void setDepartureTime(LocalTime departTime) { + this.departureTime = departTime; + } + public Flight(String departureAirport) { + this(NOT_ASSIGNED, LocalDate.now(), LocalTime.now(), departureAirport, DEFAULT_DESTINATION); + } + public Flight(double price, LocalDate departDate, LocalTime departTime, String departAirport, String arrivalAirport) { + super(); + this.price = price; + this.departDate = departDate; + this.departureAirport = departAirport; + this.arrivalAirport = arrivalAirport; + this.departureTime= departTime; + } + public LocalDate getDepartDate() { + return departDate; + } + public void setDepartDate(LocalDate departDate) { + this.departDate = departDate; + } + + + public String getDepartAirport() { + return departureAirport; + } + public void setDepartureAirport(String departureAirport) { + this.departureAirport = departureAirport; + } + + public String getArrivalAirport() { + return arrivalAirport; + } + public void setArrivalAirport(String arrivalAirport) { + this.arrivalAirport = arrivalAirport; + } + + public void setPrice(double price) { + this.price = price; + } + + + /** + * @return the price of the flight. If the price is not affected, the price is calculated randomly, almost as in reality + */ + public double getPrice() { + if (price == NOT_ASSIGNED) { + double start = 10; + double end = 1000; + double random = new Random().nextDouble(); + price = start + (random * (end - start)); + } + return price; + } + + /** + * @param d date for the flight + * @param from name of the departure airport + * @param to name of the arrival airport + * @return true if the different parameters match with the current flight + */ + public boolean match(LocalDate d, String from, String to) { + return getDepartDate().equals(d) && getDepartAirport().equals(from) && getArrivalAirport().equals(to); + + } +} diff --git a/2020-2021/src/TD4/flights/FlightService.java b/2020-2021/src/TD4/flights/FlightService.java new file mode 100644 index 0000000..87df449 --- /dev/null +++ b/2020-2021/src/TD4/flights/FlightService.java @@ -0,0 +1,62 @@ +package TD4.flights; + +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; + +/** + * This class allows the management of a set of flights + * + * @author Mireille Blay-Fornarino + * + * + */ + +public class FlightService { + + private List flights = new ArrayList<>(); + + public FlightService(List flights) { + this.flights = flights; + } + + /** + * @param aDate : {@code LocalDate} + * @return the list of flights available on a given date {@code LocalDate} + */ + public List getFlights(LocalDate aDate) { + Stream parallelStream = flights.parallelStream(); + Stream results = parallelStream.filter(f -> (f.getDepartDate().equals(aDate))); + return results.collect(Collectors.toCollection(ArrayList::new)); + } + + /** + * @param d : : {@code LocalDate} + * @param from : departure airport name + * @param to : arrival airport name + * @return the list of flights available on a given date {@code LocalDate} from + * a place to another place + */ + public List getFlights(LocalDate d, String from, String to) { + Stream parallelStream = flights.parallelStream(); + Stream results = parallelStream.filter(f -> f.match(d, from, to)); + return results.collect(Collectors.toCollection(ArrayList::new)); + } + + /** + * @return a copy of the flight list sorted by price. The flights themselves are + * not cloned. + */ + public List sortedByPrice() { + flights.sort(Comparator.comparing(Flight::getPrice)); + return new ArrayList<>(flights); + } + + public Flight getFlyWithBestPrice() { + return this.sortedByPrice().get(0); + } + +} diff --git a/2020-2021/src/TD4/util/AlreadyBooked.java b/2020-2021/src/TD4/util/AlreadyBooked.java new file mode 100644 index 0000000..85c50cf --- /dev/null +++ b/2020-2021/src/TD4/util/AlreadyBooked.java @@ -0,0 +1,20 @@ +package TD4.util; + +/** + * + * This exception is raised when something is already rented and attempts are made to over-rent it. + * @author Mireille Blay-Fornarino + * + */ +public class AlreadyBooked extends Exception { + + /** + * + */ + private static final long serialVersionUID = 1L; + + public AlreadyBooked(String message) { + super(message); + } + +} diff --git a/2020-2021/src/TD4/util/DateTools.java b/2020-2021/src/TD4/util/DateTools.java new file mode 100644 index 0000000..7f5842e --- /dev/null +++ b/2020-2021/src/TD4/util/DateTools.java @@ -0,0 +1,50 @@ +package TD4.util; + +import java.time.LocalDate; + +/** + * Utility class to manage dates + * + * + * @author Mireille Blay-Fornarino + * + * + * 6 oct. 2018 + */ +public final class DateTools { + + + private DateTools() { + throw new IllegalStateException("Utility class"); + } + + /** + * @param date + * @param nbDays number of days to be added + * @return the {@code LocalDate} corresponding to the {@code Date} to which {@code nbDays} days have been added + */ + public static LocalDate addDays(LocalDate date, int nbDays) { + return date.plusDays(nbDays); + } + + + /** + * @param date + * @param nbDays + * @return the {@code LocalDate} array containing the {@code date} and the following {@code nbDays}-1; + */ + public static LocalDate[] getDays(LocalDate date, int nbDays) { + if (nbDays == 0) + return new LocalDate[nbDays]; + int i = 0; + LocalDate[] dates = new LocalDate[nbDays]; + dates[i] = date; + i+=1; + while (i < nbDays) { + dates[i] = addDays(date,i); + i++; + } + return dates; + } + +} diff --git a/2020-2021/src/TD4/util/NotPossibleCarRentalException.java b/2020-2021/src/TD4/util/NotPossibleCarRentalException.java new file mode 100644 index 0000000..2c108fa --- /dev/null +++ b/2020-2021/src/TD4/util/NotPossibleCarRentalException.java @@ -0,0 +1,19 @@ +package TD4.util; + +/** + * @author Mireille Blay-Fornarino + * + * 6 oct. 2018 + */ +public class NotPossibleCarRentalException extends Exception { + + /** + * + */ + private static final long serialVersionUID = 1L; + + public NotPossibleCarRentalException(String message) { + super(message); + } + +} diff --git a/2020-2021/tests/TD4/cars/CarRentalServiceTest.java b/2020-2021/tests/TD4/cars/CarRentalServiceTest.java new file mode 100644 index 0000000..16eeaf1 --- /dev/null +++ b/2020-2021/tests/TD4/cars/CarRentalServiceTest.java @@ -0,0 +1,104 @@ +package TD4.cars; + +import static org.junit.Assert.*; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +import TD4.util.NotPossibleCarRentalException; + + + + + +public class CarRentalServiceTest { + + CarRentalService service ; + Car myCar0 = new Car("1111 AB 06",50); + Car myCar1 = new Car("1111 AB 75",100); + Car myCar2 = new Car("1111 AB 83",75); + LocalDate currentDate; + + @Before + public void setUp() { + myCar0 = new Car("1111 AB 06",50); + myCar1 = new Car("1111 AB 75",100); + myCar2 = new Car("1111 AB 83",75); + service = new CarRentalService( new ArrayList<>(Arrays.asList(myCar0, myCar1, myCar2) ) ) ; + + } + + @Test + public void testGetAvailableCars() throws NotPossibleCarRentalException { + LocalDate current = LocalDate.of(2020,9,11); + List possibleCars = + service.getAvailableCars(current, 2); + assertEquals(3, possibleCars.size()); + + CarRental carRental = service.book(myCar0,current,2); + assertTrue(carRental != null); + possibleCars = + service.getAvailableCars(current, 1); + assertEquals(2, possibleCars.size()); + possibleCars = + service.getAvailableCars(current, 2); + assertEquals(2, possibleCars.size()); + possibleCars = + service.getAvailableCars(LocalDate.of(2020,9,12), 1); + assertEquals(2, possibleCars.size()); + possibleCars = + service.getAvailableCars(LocalDate.of(2020,9,13), 1); + assertEquals(3, possibleCars.size()); + + } + + + @Test + public void testBookAnAvalaibleCar() throws NotPossibleCarRentalException { + CarRental carRental = service.book(myCar0,LocalDate.of(2018,9,11), 2); + assertFalse(carRental==null); + List possibleCars = service.getAvailableCars(LocalDate.of(2018,9,11), 1); + assertEquals(2, possibleCars.size()); + + possibleCars = service.getAvailableCars(LocalDate.of(2018,9,12), 3); + assertEquals(2, possibleCars.size()); + + possibleCars = service.getAvailableCars(LocalDate.of(2018,9,13), 3); + assertEquals(3, possibleCars.size()); + + possibleCars = service.getAvailableCars(LocalDate.of(2018,9,9), 4); + assertEquals(2, possibleCars.size()); + + possibleCars = service.getAvailableCars(LocalDate.of(2018,9,19), 7); + assertEquals(3, possibleCars.size()); + + } + + + @Test + public void testBookANonAvalaibleCar() throws NotPossibleCarRentalException { + CarRental carRental = service.book(myCar0,LocalDate.of(2020,9,11), 2); + assertFalse(carRental==null); + carRental = service.book(myCar0,LocalDate.of(2020,9,12), 2); + assertTrue(carRental==null); + } + + @Test + public void testGetNotAvailableCars() throws NotPossibleCarRentalException { + service.book(myCar0,LocalDate.of(2020,9,11), 2); + List possibleCars = service.getAvailableCars(LocalDate.of(2020,9,11), 2); + assertEquals(2, possibleCars.size()); + possibleCars = service.getAvailableCars(LocalDate.of(2020,9,12), 2); + assertEquals(2, possibleCars.size()); + possibleCars = service.getAvailableCars(LocalDate.of(2020,9,13), 2); + assertEquals(3, possibleCars.size()); + } + + + +} diff --git a/2020-2021/tests/TD4/cars/CarRentalTest.java b/2020-2021/tests/TD4/cars/CarRentalTest.java new file mode 100644 index 0000000..c7c0a1e --- /dev/null +++ b/2020-2021/tests/TD4/cars/CarRentalTest.java @@ -0,0 +1,61 @@ +package TD4.cars; +import static org.junit.jupiter.api.Assertions.*; + +import java.time.LocalDate; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import TD4.util.DateTools; + + + + +class CarRentalTest { + + Car myCar ; + CarRental carRental; + + @BeforeEach + public void setUp() { + myCar = new Car("1111 AB 06",50); + } + + + @Test + public void testCarRentalCreation() { + carRental = new CarRental(myCar, LocalDate.of(2018, 8, 30), 3); + assertEquals( myCar.getDayPrice()*3, carRental.getPrice()); + } + + @Test + public void testCarRentalAvailability() { + carRental = new CarRental(myCar, LocalDate.of(2018, 8, 30), 3); + LocalDate[] dates = DateTools.getDays(LocalDate.of(2018, 8, 30), 3); + assertEquals(3, dates.length ); + dates = DateTools.getDays(LocalDate.of(2018, 8, 30), 1); + assertTrue(carRental.includeADate(dates),"date de début OK"); + + dates = DateTools.getDays(LocalDate.of(2018, 8, 29), 1); + assertFalse(carRental.includeADate(dates),"date à un jour avant"); + + dates = DateTools.getDays(LocalDate.of(2018, 9, 2), 1); + assertFalse(carRental.includeADate(dates),"date à un jour après"); + + dates = DateTools.getDays(LocalDate.of(2018, 8, 25), 10); + assertTrue(carRental.includeADate(dates),"date incluse après"); + + dates = DateTools.getDays(LocalDate.of(2018, 9, 1), 1); + assertTrue(carRental.includeADate(dates),"date incluse sur la fin"); + dates = DateTools.getDays(LocalDate.of(2018, 8, 31), 10); + assertTrue(carRental.includeADate(dates),"date incluse sur la fin"); + } + + @Test + public void testGetPrice() { + carRental = new CarRental(myCar, LocalDate.of(2017, 8, 31), 3); + assertEquals(50,myCar.getDayPrice()); + assertEquals(50.0*3,carRental.getPrice()); + } + +} diff --git a/2020-2021/tests/TD4/flights/FlightServiceTest.java b/2020-2021/tests/TD4/flights/FlightServiceTest.java new file mode 100644 index 0000000..2602ada --- /dev/null +++ b/2020-2021/tests/TD4/flights/FlightServiceTest.java @@ -0,0 +1,75 @@ +package TD4.flights; + +import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.time.LocalDate; +import java.time.LocalTime; +import java.util.ArrayList; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + + + +public class FlightServiceTest { + + private static final String PARIS = "Paris"; + private static final String NICE = "Nice"; + private static final LocalDate dateToTest = LocalDate.of(2017, 12, 24); + + FlightService service ; + + @Before + public void setUp() { + ArrayList list = new ArrayList<>(); + list.add(new Flight("Belfort")); + list.add(new Flight(NICE)); + list.add(new Flight(100, dateToTest, LocalTime.of(7, 45),NICE, PARIS)); + list.add(new Flight(20, dateToTest, LocalTime.of(9, 30), NICE, PARIS)); + list.add(new Flight(150, dateToTest, LocalTime.of(18, 30), PARIS, NICE)); + service = new FlightService(list); + } + + + @Test + public void testGetFlightsOnADivenDate() { + List flights = service.getFlights(LocalDate.now()); + assertEquals(2, flights.size()); + flights = service.getFlights(dateToTest); + assertEquals(3, flights.size()); + } + + @Test + public void testGetFlights() { + List flights = service.getFlights(LocalDate.now(),NICE,PARIS); + assertEquals(1, flights.size()); + flights = service.getFlights(dateToTest,NICE,PARIS); + assertEquals(2, flights.size()); + } + + @Test + public void testSortedByPrice() { + ArrayList list = new ArrayList<>(); + list.add(new Flight(100, dateToTest, LocalTime.of(7, 45),NICE, PARIS)); + list.add(new Flight(20, dateToTest, LocalTime.of(9, 30), NICE, PARIS)); + list.add(new Flight(150, dateToTest, LocalTime.of(18, 30), PARIS, NICE)); + service = new FlightService(list); + List flights = service.sortedByPrice(); + assertEquals(20,flights.get(0).getPrice(),0.01); + assertEquals(100,flights.get(1).getPrice(),0.01); + assertEquals(150,flights.get(2).getPrice(),0.01); + } + + @Test + public void testGetBestPrice() { + ArrayList list = new ArrayList<>(); + list.add(new Flight(100, dateToTest, LocalTime.of(7, 45),NICE, PARIS)); + list.add(new Flight(20, dateToTest, LocalTime.of(9, 30), NICE, PARIS)); + list.add(new Flight(150, dateToTest, LocalTime.of(18, 30), PARIS, NICE)); + service = new FlightService(list); + assertEquals(20, service.getFlyWithBestPrice().getPrice()); + } + +} diff --git a/2020-2021/tests/TD4/flights/FlightTest.java b/2020-2021/tests/TD4/flights/FlightTest.java new file mode 100644 index 0000000..19e6157 --- /dev/null +++ b/2020-2021/tests/TD4/flights/FlightTest.java @@ -0,0 +1,29 @@ +package TD4.flights; + +import static org.junit.Assert.*; + +import java.time.LocalDate; +import java.time.LocalTime; + +import org.junit.Before; +import org.junit.Test; + +public class FlightTest { + + + Flight f1 ; + @Before + public void setUp() { + f1 = new Flight(100, LocalDate.of(2017,11,11), LocalTime.of(7, 45),"Nice","Paris"); + } + + + @Test + public void testGetPrice() { + assertEquals(100,f1.getPrice(),0); + f1.setPrice(-1); + assertTrue(f1.getPrice()>=10); + assertTrue(f1.getPrice()<=1000); + } + +}