From 2a124a89346218819d123a4079614a3babac8fed Mon Sep 17 00:00:00 2001 From: Florent Madelaine Date: Fri, 22 Oct 2021 18:57:48 +0200 Subject: [PATCH] brouillon un peu mieux de ReservationFactoryNP -- disclaimer : use at your own risk --- Makefile | 15 +++++--- .../MNP/ReservationFactoryNP.java | 34 +++++++++++------- .../projetIHM2021FI2/Test/TestTexteMNP.java | 36 +++++++++++++++++++ 3 files changed, 67 insertions(+), 18 deletions(-) diff --git a/Makefile b/Makefile index 7f6064f..d7320ea 100644 --- a/Makefile +++ b/Makefile @@ -53,6 +53,7 @@ ${BUILD}/API/Prereservation.class : ${SRC}/API/Prereservation.java \ ${JAVAC} ${JAVAC_OPTIONS} ${SRC}/API/Prereservation.java ${BUILD}/API/Reservation.class : ${SRC}/API/Reservation.java \ + ${BUILD}/API/Chambre.class \ ${BUILD}/API/Prereservation.class ${JAVAC} ${JAVAC_OPTIONS} ${SRC}/API/Reservation.java @@ -61,7 +62,7 @@ ${BUILD}/API/PrereservationFactory.class : ${SRC}/API/PrereservationFactory.java ${JAVAC} ${JAVAC_OPTIONS} ${SRC}/API/PrereservationFactory.java ${BUILD}/API/ReservationFactory.class : ${SRC}/API/ReservationFactory.java \ - ${BUILD}/API/Prereservation.class + ${BUILD}/API/Reservation.class ${JAVAC} ${JAVAC_OPTIONS} ${SRC}/API/ReservationFactory.java ## MNP ## @@ -83,18 +84,22 @@ ${BUILD}/MNP/ReservationNP.class : ${SRC}/MNP/ReservationNP.java \ ${BUILD}/API/Reservation.class ${JAVAC} ${JAVAC_OPTIONS} ${SRC}/MNP/ReservationNP.java -${BUILD}/MNP/PrereservationFactory.class : ${SRC}/API/PrereservationFactory.java - ${JAVAC} ${JAVAC_OPTIONS} ${SRC}/MNP/PrereservationFactory.java - ${BUILD}/MNP/PrereservationFactoryNP.class : ${SRC}/MNP/PrereservationFactoryNP.java \ ${BUILD}/API/PrereservationFactory.class ${JAVAC} ${JAVAC_OPTIONS} ${SRC}/MNP/PrereservationFactoryNP.java +${BUILD}/MNP/ReservationFactoryNP.class : ${SRC}/MNP/ReservationFactoryNP.java \ + ${BUILD}/MNP/ChambreNP.class \ + ${BUILD}/MNP/ReservationNP.class \ + ${BUILD}/API/ReservationFactory.class + ${JAVAC} ${JAVAC_OPTIONS} ${SRC}/MNP/ReservationFactoryNP.java + ## TEST ## ${BUILD}/Test/TestTexteMNP.class : ${SRC}/Test/TestTexteMNP.java \ ${BUILD}/MNP/PrereservationNP.class \ - ${BUILD}/MNP/PrereservationFactoryNP.class + ${BUILD}/MNP/PrereservationFactoryNP.class \ + ${BUILD}/MNP/ReservationFactoryNP.class ${JAVAC} -Xlint:deprecation ${JAVAC_OPTIONS} ${SRC}/Test/TestTexteMNP.java # ## JARS ## diff --git a/src/fr/iutfbleau/projetIHM2021FI2/MNP/ReservationFactoryNP.java b/src/fr/iutfbleau/projetIHM2021FI2/MNP/ReservationFactoryNP.java index 996c65f..c13753c 100644 --- a/src/fr/iutfbleau/projetIHM2021FI2/MNP/ReservationFactoryNP.java +++ b/src/fr/iutfbleau/projetIHM2021FI2/MNP/ReservationFactoryNP.java @@ -28,16 +28,18 @@ public class ReservationFactoryNP implements ReservationFactory{ // similaire pour stocker la valeur max, même un hotel magique devrait faire le ménage. private EnumMap brokenMagic; - + + // BUGFIX. ^ c'est le xor en java donc 10^6 ça ne fait pas un million mais autre chose + // Du coup en dessous j'écris 1000000 à la place. // plus petite et plus grande valeur pour les chambres UNLS private static int MIN_UNLS = 0; - private static int MAX_UNLS = 10^6 -1; + private static int MAX_UNLS = 1000000 -1; // plus petite et plus grande valeur pour les chambres DEUXLS - private static int MIN_DEUXLS = 10^6; - private static int MAX_DEUXLS = 2*10^6 -1; + private static int MIN_DEUXLS = 1000000; + private static int MAX_DEUXLS = 2*1000000 -1; // plus petite et plus grande valeur pour les chambres UNLD - private static int MIN_UNLD = 2*10^6; - private static int MAX_UNLD = 3*10^6 -1; + private static int MIN_UNLD = 2*1000000; + private static int MAX_UNLD = 3*1000000 -1; // plus grand nombre de chambres à proposer si le client souhaite choisir (va automatiquement salir les chambres précédentes) private static int ADEQUATE = 5; @@ -93,7 +95,7 @@ public class ReservationFactoryNP implements ReservationFactory{ this.nextFreeRoom.put(TypeChambre.UNLS,Integer.valueOf(this.MIN_UNLS)); this.nextFreeRoom.put(TypeChambre.DEUXLS,Integer.valueOf(this.MIN_DEUXLS)); this.nextFreeRoom.put(TypeChambre.UNLD,Integer.valueOf(this.MIN_UNLD)); - + brokenMagic = new EnumMap<>(TypeChambre.class); this.brokenMagic.put(TypeChambre.UNLS,Integer.valueOf(this.MAX_UNLS)); this.brokenMagic.put(TypeChambre.DEUXLS,Integer.valueOf(this.MAX_DEUXLS)); @@ -143,7 +145,7 @@ public class ReservationFactoryNP implements ReservationFactory{ int howMany = Math.min(this.brokenMagic.get(p.getTypeChambre()) - this.nextFreeRoom.get(p.getTypeChambre()), this.ADEQUATE); for (int i=0; i< howMany ;i++) { - Chambre c = new ChambreNP(numero,p.getTypeChambre()); + Chambre c = new ChambreNP(numero+i,p.getTypeChambre()); res.add(c); } @@ -161,7 +163,7 @@ public class ReservationFactoryNP implements ReservationFactory{ */ private void addReservationToBrain(Reservation r){ // mise à jour de la prochaine chambre libre à 1 de plus que moi - this.nextFreeRoom.put(r.getChambre().getType(),Integer.valueOf(r.getChambre().getNumero() + 1)); + this.nextFreeRoom.put(r.getChambre().getType(),Integer.valueOf(r.getChambre().getNumero() + 1)); // Ajout pour toutes les dates de la réservation. LocalDate d = r.getDateDebut(); for (int i=0; i< r.getJours() ;i++) { @@ -184,7 +186,13 @@ public class ReservationFactoryNP implements ReservationFactory{ this.brain.put(d,s); } - + public String prochainesChambresLibres(){ + StringBuilder sb = new StringBuilder("prochaines chambres libres par type:\n"); + for (TypeChambre t : TypeChambre.values()){ + sb.append("\t _ " + t + " " + this.nextFreeRoom.get(t) + "\n"); + } + return sb.toString(); + } /** * Fabrique (ajoute) une réservation @@ -203,10 +211,10 @@ public class ReservationFactoryNP implements ReservationFactory{ if (c.getType()!=p.getTypeChambre()) { throw new IllegalArgumentException("Erreur sur le type de la chambre: la préréservation indique " + p.getTypeChambre() + " mais la chambre est " + c.getType()); } - else if (false) // n'arrive jamais dans l'hôtel magique. + else if (false) // on fait comme si ça n'arrive jamais dans l'hôtel magique (pour l'instant). { - throw new IllegalArgumentException("Pas de chambre disponible de type " + p.getTypeChambre()); - } + throw new IllegalArgumentException("La chambre " + c.monPrint() + " n'est pas disponible pour fabriquer une réservation à partir de la préréservation " + p.monPrint()); + } else { Reservation r = new ReservationNP(p.getReference(), p.getDateDebut(), p.getJours(), c, p.getClient()); this.addReservationToBrain(r); diff --git a/src/fr/iutfbleau/projetIHM2021FI2/Test/TestTexteMNP.java b/src/fr/iutfbleau/projetIHM2021FI2/Test/TestTexteMNP.java index c0d1111..0c8ba5f 100644 --- a/src/fr/iutfbleau/projetIHM2021FI2/Test/TestTexteMNP.java +++ b/src/fr/iutfbleau/projetIHM2021FI2/Test/TestTexteMNP.java @@ -3,6 +3,7 @@ import fr.iutfbleau.projetIHM2021FI2.API.*; import fr.iutfbleau.projetIHM2021FI2.MNP.*; import java.time.LocalDate; import java.util.*; +//import java.util.Random; public class TestTexteMNP{ @@ -52,6 +53,8 @@ public class TestTexteMNP{ // On peut donc changer l'implémentation du modèle tant qu'on ne change pas l'API en préservant le bon fonctionement de la vue et du controleur. // Pour l'instant, nous n'avons ni vue, ni controleur, mais nous pouvons faire semblant en interagissant avec le modèle via l'API. + // pour choisir au hasard. + Random random = new Random(); System.out.print("Simulation manuelle du genre de chose que la vue pourrait faire.\n"); @@ -99,6 +102,39 @@ public class TestTexteMNP{ System.out.print("I found your bookings.\n"); for(Prereservation p : preresas){ System.out.println(p.monPrint()); + Chambre libre = null; + Set disponibles=null; + try{ + disponibles = grandLivreDOrAPISeulement.getChambres(p); + System.out.print("I have great rooms for you :\n"); + for(Chambre c : disponibles){ + System.out.println(c.monPrint()); + } + // choix de la chambre au hasard simulant le choix du client + int choix = random.nextInt(disponibles.size()); + System.out.println("choix: " + choix); + int compter = 0; + for(Chambre c : disponibles){ + if (compter == choix){ + libre = c; + break; + } + compter++; + } + } + catch(IllegalStateException e){ + System.out.print(e.getMessage()); + } + try{ + System.out.println(libre.monPrint()); + System.out.print("You like this room? Excellent! Let me confirm the reservation for you.\n"); + Reservation resa = grandLivreDOrAPISeulement.createReservation(p,libre); + System.out.println(resa.monPrint()); + } + catch(IllegalStateException e){ + System.out.print(e.getMessage()); + } + } } catch(IllegalStateException e){