From 29082acd3e4ae705769c954f7a3159d611a0b4da Mon Sep 17 00:00:00 2001 From: JunkJumper Date: Mon, 25 May 2020 00:27:11 +0200 Subject: [PATCH] TD3 ex2 ok --- src/TD3/fleuriste/Bouquet.java | 38 ++++++------ src/TD3/fleuriste/Fleur.java | 25 +++++--- src/TD3/fleuriste/LotFleur.java | 43 +++++++------- src/TD3/fleuriste/Stock.java | 100 +++++++++++++++++++++++--------- 4 files changed, 135 insertions(+), 71 deletions(-) diff --git a/src/TD3/fleuriste/Bouquet.java b/src/TD3/fleuriste/Bouquet.java index ede321a..cef9d8a 100644 --- a/src/TD3/fleuriste/Bouquet.java +++ b/src/TD3/fleuriste/Bouquet.java @@ -1,33 +1,37 @@ package TD3.fleuriste; public class Bouquet { - private LotFleurs lot1; - private LotFleurs lot2; - private LotFleurs lot3; + private LotFleur lot0; + private LotFleur lot1; + private LotFleur lot2; - public Bouquet(LotFleurs un, LotFleurs deux, LotFleurs trois) { - if (un.fleur.getNom() == deux.fleur.getNom() || un.fleur.getNom() == trois.fleur.getNom() || deux.fleur.getNom() == trois.fleur.getNom()) + public Bouquet(LotFleur un, LotFleur deux, LotFleur trois) { + if (un.getFleur().getNom() == deux.getFleur().getNom() || un.getFleur().getNom() == trois.getFleur().getNom() || deux.getFleur().getNom() == trois.getFleur().getNom()) throw new RuntimeException("Même fleurs"); - lot1 = un; - lot2 = deux; - lot3 = trois; + this.lot0 = un; + this.lot1 = deux; + this.lot2 = trois; } public double prix() { - double prix = lot1.fleur.prix*lot1.nombre+lot2.fleur.prix*lot2.nombre+lot3.fleur.prix*lot3.nombre; - return prix += prix*15/100; + return (lot0.getPrix() + lot1.getPrix() + lot2.getPrix()); } - public LotFleurs getLot1() { - return lot1; + public LotFleur getLot0() { + return this.lot0; + } + + public LotFleur getLot1() { + return this.lot1; } - public LotFleurs getLot2() { - return lot2; + public LotFleur getLot2() { + return this.lot2; } - - public LotFleurs getLot3() { - return lot3; + + @Override + public String toString() { + return "Le bouquet est composé de " + lot0.getQuantite() + " " + lot0.getFleur().getNom() + "s, " + lot1.getQuantite() + " " + lot1.getFleur().getNom() + "s et " + lot2.getQuantite() + " " + lot2.getFleur().getNom() + ". " + lot0.toString() + " " + lot1.toString() + " " + lot2.toString() + ". Le bouquet a donc un prix de " + this.prix(); } } diff --git a/src/TD3/fleuriste/Fleur.java b/src/TD3/fleuriste/Fleur.java index 372eab2..62c3628 100644 --- a/src/TD3/fleuriste/Fleur.java +++ b/src/TD3/fleuriste/Fleur.java @@ -1,19 +1,30 @@ package TD3.fleuriste; public class Fleur { - String nom; - double prix; + private String nom; + private double prix; - public Fleur(String nomf, double prix) { - this.nom = nomf; - this.prix = prix; + public Fleur(String n, double p) { + this.nom = n; + this.prix = p; } - public Fleur clone() { - return (new Fleur(this.nom, this.prix)); + @Override + protected Object clone() throws CloneNotSupportedException { + return new Fleur(this.nom, this.prix); } public String getNom() { return this.nom; } + + public double getPrix() { + return prix; + } + + @Override + public String toString() { + return "1 " + this.getNom() + " a un prix unitaire de " + this.getPrix() + "€."; + } + } diff --git a/src/TD3/fleuriste/LotFleur.java b/src/TD3/fleuriste/LotFleur.java index 2f84a50..ee28473 100644 --- a/src/TD3/fleuriste/LotFleur.java +++ b/src/TD3/fleuriste/LotFleur.java @@ -5,32 +5,35 @@ public class LotFleur { private int quantite; private Fleur Fleur; -//getters - public int getQuantite() { - return quantite; - } - - public Fleur getFleur() { - return Fleur; - } - -//setters - public void setQuantite(int quantite) { - this.quantite = quantite; - } - - public void setFleur(Fleur fleur) { - Fleur = fleur; - } - -//instructions public LotFleur(Fleur nomFleur, int quantiteFleur) { this.quantite = quantiteFleur; this.setFleur(nomFleur); } + public int getQuantite() { + return quantite; + } + + public Fleur getFleur() { + return Fleur; + } + + public double getPrix() { + return this.getFleur().getPrix() * this.getQuantite(); + } - + public void setQuantite(int quantite) { + this.quantite = quantite; + } + + public void setFleur(Fleur fleur) { + Fleur = fleur; + } + + @Override + public String toString() { + return this.getFleur().toString() + " Le lot de " + this.getQuantite() + " " + this.getFleur().getNom() + "s coute donc " + this.getPrix() + "€."; + } diff --git a/src/TD3/fleuriste/Stock.java b/src/TD3/fleuriste/Stock.java index 56e2079..3810c06 100644 --- a/src/TD3/fleuriste/Stock.java +++ b/src/TD3/fleuriste/Stock.java @@ -1,39 +1,85 @@ package TD3.fleuriste; public class Stock { - Fleur roses; - Fleur tulipes; - Fleur oeillet; - int nbr; - int nbt; - int nbo; + + //roses = 0, tulipes = 1, œillets = 2 + + private Fleur[] fleurs = new Fleur[3]; + private int quantite[] = {0, 0, 0}; + + public Stock() { + this(null, null, null); + } public Stock(Fleur r, Fleur t, Fleur o) { - roses = r; - tulipes = t; - oeillet = o; - nbr = 0; - nbt = 0; - nbo = 0; + this.fleurs[0] = r; + this.fleurs[1] = t; + this.fleurs[2] = o; } - public void ajouteRose(int nb) { - nbr += nb; - } - - public void ajouteOeillet(int nb) { - nbo = nb; - } - - public void ajouteTulipe(int nb) { - nbt = nb; + public void ajouteFleur(Fleur f, int q) { + switch (f.getNom().toLowerCase()) { + case "rose": + this.setQuantite(0, this.getFleurQuantity(f.getNom()) + q); + break; + case "tulipe": + this.setQuantite(1, this.getFleurQuantity(f.getNom()) + q); + break; + case "oeillet": + this.setQuantite(2, this.getFleurQuantity(f.getNom()) + q); + break; + default: + System.err.println("Aucune fleur n'a été ajoutée"); + break; + } } public boolean bouquetFaisable(Bouquet b) { - boolean faisable = true; - if (b.getLot1().nombre > nbr || b.getLot2().nombre > nbt || b.getLot3().nombre > nbo) { - faisable = false; - } - return faisable; + return (this.quantite[0] > b.getLot0().getQuantite() || this.quantite[1] > b.getLot1().getQuantite() || this.quantite[2] > b.getLot2().getQuantite()); } + + @Override + public String toString() { + return "Il y a en stock " + this.getFleurQuantity("rose") + " roses, " + this.getFleurQuantity("tulipe") + " tulipes et " + this.getFleurQuantity("oeillet") + " oeillets. "; + } + + public int getFleurQuantity(String s) { + switch (s.toLowerCase()) { + case "rose": + return this.getQuantite(0); + case "tulipe": + return this.getQuantite(1); + case "oeillet": + return this.getQuantite(2); + default: + System.err.println("Aucune fleur n'a été ajoutée"); + return 0; + } + } + + public void setFleurQuantity(String f, int q) { + switch (f.toLowerCase()) { + case "rose": + this.setQuantite(0, q); + break; + case "tulipe": + this.setQuantite(1, q); + break; + case "oeillet": + this.setQuantite(2, q); + break; + default: + System.err.println("Aucune fleur n'a été ajoutée"); + break; + } + } + + private void setQuantite(int index, int i) { + this.quantite[index] = i; + } + + private int getQuantite(int i) { + return this.quantite[i]; + } + }