/!\ POST#1 Mis à jour en continu pour le code /!\
Bonjour à tous
Je crée un nouveau fil qui sera plus approprié pour poser mes questions au fur et à mesure.
Le but de mon projet est de parvenir à simuler, de manière simplifiée, le fonctionnement d'un fast food.
Pour l'instant, grâce à l'aide de nombreux forumers (que je remercie) j'avance progressivement.
Avancement du projet: [terminé] [en cours] [à faire]
-Permettre la création de nouveaux ingrédients que l'on ajoute à un stock d'ingrédients (méthode createIngredients())
-Permettre la création de nouveaux produits (simple et composé) que l'on ajoute à un objet qui recense les produits disponibles à la vente (méthode createProduits())
-Afficher la constitution d'un Produit_compose (méthode DecrisProduitCompose())
-Lors de la création d'un Produit_compose, il faut retirer les Ingredient utilisés pour cette création au stock d'ingrédients
-Mettre en place la partie "commande par un client"
-------------------------------------------------------------------
Un ingrédient est caractérisé par le fait qu'il n'est pas disponible à la commande mais intervient dans la préparation d'un produit_compose.
Ex*: tomate, steack, cornichon, ...
Un produit est caractérisé par le fait qu'il est disponible à la commande par un client.
Un produit_compose est un produit qui se compose de plusieurs Ingrédient.
Ex*: hamburger, glace 3 boules, ...
Un produit_simple est, comme son nom l'indique, un produit qui «*ne se compose que de lui-même*».
Ex*: frite, ketchup, soda, …
---------------------------------------------------------------------
Un petit diagramme UML pour vous montrer où j'en suis:
Uploaded with ImageShack.us
Petite description des classes/méthodes:
Classe Ingredient*: Elle permet de créer de nouveaux ingrédients qui se caractérisent par leur nom et leur prix unitaire. On y trouve les méthodes get et set associées ainsi q'une méthode permettant la description d'un tel objet.
Classe Ingredients*: Elle représente les ingrédients en stock. Ici, on utilise le pattern design Singleton pour s'assurer qu'elle ne pourra être instanciée qu'une seule fois pendant la durée de l'application. On y trouve la méthode createIngredients() qui va créer un certain nombre d'ingrédients identiques et les ajouter au stock d'ingrédients.
Classe Produit*: Il s'agit d'une classe abstraite qui est la super-classe des deux classes suivantes et qui comporte les habituelles méthodes get et set pour le nom du produit et son prix.
Classe Produit_compose*: C'est une sous-classe de Produit qui permet la création de nouveaux produits composés via une liste de Ingredient passée en paramètre. Elle comporte également une méthode DecrisProduitCompose() qui retourne une chaîne de caractère indiquant les Ingrédient qui composent le produit composé.
Classe Produit_simple*: C'est une sous-classe de Produit qui permet la création de nouveaux produits simples.
Classe ProduitEnVente*: Elle représente les produits disponibles à la vente. On utilise également ici le pattern design Singleton pour s'assurer que nous n'aurons à faire qu'à une seule instance. La méthode createProduits() permet de créer plusieurs Produit_simple ou Produit_compose et de les ajouter à la liste qui représente les produits disponibles à la vente.
Classe Test*: C'est notre main*!
Les classes:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 public class Ingredient { protected String nom_ingredient; protected double prix_unitaire; Ingredient(String nom_ingredient, double prix_unitaire, Ingredients stock){ this.nom_ingredient=nom_ingredient; this.prix_unitaire=prix_unitaire; } public String getNom_ingredient(){ return this.nom_ingredient; } public void setNom_ingredient(String nom_ingredient){ this.nom_ingredient=nom_ingredient; } public double getPrix_unitaire(){ return this.prix_unitaire; } public void setPrix_unitaire(int prix_unitaire){ this.prix_unitaire=prix_unitaire; } public String DecrisIngredient(){ return "L'ingrédient "+this.nom_ingredient+" est au prix unitaire de "+this.prix_unitaire+" "; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 import java.util.Arrays; import java.util.List; import java.util.ArrayList; /** * Permet de créer de nouveaux ingrédients. */ public final class Ingredients { /** * Singleton statique qui permet de ne référencer qu'une seule instance * de la classe Ingredients. */ private static Ingredients ingredients ; /** * Liste de tous les ingrédients créés par l'intermédiaire de l'instance * unique d'ingrédients. */ private static List<Ingredient> stockIngredient; /** * Constructeur privé interdisant son instanciation à l'extérieur de la classe. */ private Ingredients() { Ingredients.stockIngredient = new ArrayList<Ingredient>(); } public static Ingredients getInstance(){ if (ingredients==null){ ingredients=new Ingredients(); } return ingredients; } public static List<Ingredient> getStockIngredients(){ return stockIngredient; } /** * Crée une nouvelle liste d'ingrédients. * @param ingredientType nom des ingrédients * @param price prix des ingrédients créés * @param quantity nombre d'ingrédients à créer * @return liste de tous les ingrédients créés */ public static List<Ingredient> createIngredients(String ingredientType, double price, int quantity) { List<Ingredient> newIngredients = new ArrayList<Ingredient>(); for (int i = 0; i < quantity; i++) { newIngredients.add(new Ingredient(ingredientType, price, ingredients)); } System.out.println(quantity+" "+ ingredientType+" ont été ajoutés au stock"); Ingredients.stockIngredient.addAll(newIngredients); return newIngredients; } /** * Prendre un(des) ingrédient(s) du stock et les supprimer. * @param nom_ingredient nom des ingrédients * @param quantity nombre d'ingrédients à prendre du stock * @return liste de tous les ingrédients pris du stock */ public static List<Ingredient> PrendreIngredientFromStockAndDeleteFromStock(String nom_ingredient, int quantity ){ List<Ingredient> liste_ingredient_pris=new ArrayList<Ingredient>(); int j = 0; for (Ingredient ingred : Ingredients.stockIngredient){ if ( nom_ingredient== ingred.getNom_ingredient() && j<quantity){ liste_ingredient_pris.add(ingred); Ingredients.stockIngredient.remove(ingred); j++; } } return liste_ingredient_pris; } /** * Decrire le stock d'ingrédients */ public static void DecrisStock(){ String comp="Le stock d'ingrédients se composent de:"; for (Ingredient ing : stockIngredient){ comp = comp +"\n"+"-"+ ing.getNom_ingredient(); } System.out.println(comp); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 /** * Classe abstraite qui "définit" un produit. */ public abstract class Produit { /** * Variables d'instance. */ protected String nom_produit; protected double prix_produit; /** * Constructeur */ protected Produit(String nom_produit, double prix_produit, ProduitsEnVente stock) { if (stock == null) { throw new IllegalArgumentException("Le stock ne peut pas être nul!"); } this.nom_produit=nom_produit; this.prix_produit=prix_produit; } /** * Retourne le nom d'un produit. * @return nom d'un produit */ public String getNom_produit(){ return this.nom_produit; } /** * Modifie le nom d'un produit. * @param nom_produit nom du produit */ public void setNom_produit(String nom_produit){ this.nom_produit=nom_produit; } /** * Retourne le prix d'un produit. * @return prix du produit */ public double getPrix_produit(){ return this.prix_produit; } /** * Modifie le prix d'un produit. * @param prix_produit prix du produit */ public void setPrix_produit(double prix_produit){ this.prix_produit=prix_produit; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 import java.util.Arrays; import java.util.List; /** * Classe qui définit un produit compose. */ public class Produit_compose extends Produit { /** * liste des ingrédients qui entrent dans la composition d'un produit compose. */ protected List<Ingredient> liste_ingredient; /** * Constructeur */ Produit_compose(String nom_produit, double prix_produit, ProduitsEnVente stock, Ingredient... liste_ingredient) { super(nom_produit, prix_produit, stock); this.liste_ingredient = Arrays.asList(liste_ingredient); } /** * Decris un produit compose. * @return description du produit compose */ public String DecrisProduitCompose() { String composition=""; for (Ingredient ing : this.liste_ingredient){ composition= composition + ing.getNom_ingredient()+"\n "; } return composition; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7 public class Produit_simple extends Produit { public Produit_simple(String nom_produit, double prix_produit, ProduitsEnVente stock) { super(nom_produit, prix_produit, stock); } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73 import java.util.ArrayList; import java.util.List; /** * Référence et créée de nouveaux produits. */ public final class ProduitsEnVente { /** * Singleton de ProduitsEnVente utilisé pour référencer de manière unique * les produits créés par le système. */ private static ProduitsEnVente produits = new ProduitsEnVente(); /** * Liste des produits créés par ProduitsEnVente. */ private List<Produit> products; /** * Constructeur privé qui permet à la classe de ne pas pouvoir être * instanciée. */ private ProduitsEnVente() { products = new ArrayList<Produit>(); } /** * Crée une liste de nouveaux Produit_simple. * @param productName nom des produits à créer * @param price prix des produits * @param quantité nom de Produit_simple à créer */ public static List<Produit_simple> createProduits(String productName, double price, int quantity) { List<Produit_simple> newProducts = new ArrayList<Produit_simple>(); for (int i = 0; i < quantity; i++) { newProducts.add(new Produit_simple(productName, price, produits)); } System.out.println(quantity+" "+productName+" sont dorénavant disponibles à la commande"); produits.products.addAll(newProducts); return newProducts; } /** * Crée une liste de nouveaux Produit_simple. * @param productName nom des produits à créer * @param price prix des produits * @param quantite quantite du même produit compose à créer * @param ingredients liste des ingrédients necessaire à la préparation du produit compose * @return liste des nouveaux produits identiquement créés. */ public static List<Produit_compose> createProduits(String productName, double price, int quantite, Ingredient... ingredients) { // Creation d'un Produit temporaire qui sert simplement à appliquer la méthode DecrisProduitCompose (comme on peut le voir, //il n'est pas ajouté à newproducts) List<Produit_compose> newProducts = new ArrayList<Produit_compose>(); Produit_compose temp = new Produit_compose(productName, price, produits, ingredients); for (int i = 0; i < quantite; i++) { newProducts.add(new Produit_compose(productName, price, produits, ingredients)); } System.out.println(quantite+" "+productName+" ont été préparés en cuisine et sont disponible à la commande"); //System.out.println("Un "+productName+" se compose de:\n"+ temp.DecrisProduitCompose()); produits.products.addAll(newProducts); return newProducts; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 import java.util.Arrays; import java.util.List; import java.util.ArrayList; public class Test { public static void main(String[] args) { Ingredients.getInstance(); // On crée une liste composée de 4 tomates List<Ingredient> tomates = Ingredients.createIngredients("tomate", 0.5, 4); // Description d'une tomate System.out.println(tomates.get(0).DecrisIngredient()); // On rajoute 0 tomates tomates.addAll(Ingredients.createIngredients("tomate", 0.5, 0)); // On ajoute 4 steacks List<Ingredient> steacks = Ingredients.createIngredients("steack", 2, 2); System.out.println(steacks.get(0).DecrisIngredient()); List<Ingredient> cornichons = Ingredients.createIngredients("cornichon", 0.2, 4); Ingredients.DecrisStock(); // On crée une liste d'ingrédients nécessaires à la préparation d'un seul BigMac List<Ingredient> ingredients = new ArrayList<Ingredient>(); //On y ajoute 2 tomates ingredients.addAll(Ingredients.PrendreIngredientFromStockAndDeleteFromStock( "tomate", 2 )); //... puis 1 steack... ingredients.addAll(Ingredients.PrendreIngredientFromStockAndDeleteFromStock( "steack", 1 )); Ingredients.DecrisStock(); // On crée une liste d'ingrédients nécessaires à la préparation d'un CheeseBurger //List<Ingredient> ingredients2 = Ingredients.createIngredients("cornichon", .3, 2); // ingredients2.addAll(Ingredients.createIngredients("tomate", .5, 1)); //ingredients2.addAll(Ingredients.createIngredients("steack", 1, 1)); //ingredients2.addAll(Ingredients.createIngredients("cheddar", 1, 2)); // On crée 5 BigMacs qui sont automatiquement ajoutés aux produits disponibles à la vente List<Produit_compose> BigMacs = ProduitsEnVente.createProduits("Big Mac", 4.5, 2, ingredients.toArray(new Ingredient[ingredients.size()])); //// On crée 3 CheeseBurgers qui sont automatiquement ajoutés aux produits disponibles à la vente //List<Produit_compose> CheeseBurgers = ProduitsEnVente.createProduits("Cheeseburger", 4.5, 3, ingredients2.toArray(new Ingredient[ingredients2.size()])); // On crée une liste de 12 frites qui sont automatiquement ajoutées aux produits disponibles à la vente List<Produit_simple> Frites = ProduitsEnVente.createProduits("frite", 4.5, 12); Ingredients.DecrisStock(); } }
Partager