-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsommateur.java
167 lines (126 loc) · 3.08 KB
/
Consommateur.java
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* {@link Personne} achetant un certain nombre d'articles dans la
* {@link Boulangerie} a laquelle elle est affiliee
*/
public class Consommateur extends Personne {
/**
* Attributs et compteur
*/
private double pRachat;
private Commande cmd;
private Strategy strategy;
private static int nbConsommateurs = 0;
/**
* Constructeur 01
*
* @param n : nom
* @param b : {@link Boulangerie} affiliee
* @param pR : probabilite de racheter un {@link Consommable}
* @param x : abscisse
* @param y : ordonnee
*/
public Consommateur (String n, double pR, int x, int y) {
super(n + ++nbConsommateurs, x, y);
cmd = new Commande();
pRachat = pR;
}
/**
* Constructeur 02
*
* @param n : nom
* @param b : {@link Boulangerie} affiliee
* @param x : abscisse
* @param y : ordonnee
*/
public Consommateur (String n, int x, int y) {
this(n, Math.random(), x, y);
}
/**
* Redefinition de la methode standard toString()
*
* @return les caracteristiques du consommateur
*/
@Override
public String toString() {
return "Consommateur : " + super.toString();
}
/**
* Redefinition de la methode standard equals()
*
* @return l'egalite des attributs entre deux consommateurs
*/
@Override
public boolean equals(Object obj) {
if ((this != null) && (this.getClass() == obj.getClass())) {
Consommateur c = (Consommateur) obj;
return super.equals(obj) && (pRachat == c.pRachat) && (strategy.equals(c.strategy)) && (cmd.equals(c.cmd));
}
return false;
}
/**
* Ajout d'un consommable dans le tableau dynamique cmd
*
* @param tab : tableau duquel est pris le {@link Consommable}
* @param cpt : quantite actuelle de ce type de {@link Consommable}
*/
public boolean ajouter(Consommable[] tab, int cpt) {
Consommable c;
try {
switch (cpt) {
case 0:
throw new StockException("Stock insuffisant");
case 1:
if (tab[0] instanceof Refaisable) {
((Refaisable) tab[0]).refaire();
Boulangerie.setAllXY(tab, tab[0].getX(), tab[0].getY());
c = tab[tab.length-1];
tab[tab.length-1] = null;
}
else {
c = tab[0];
tab[0] = null;
}
cmd.ajouterConsommable(c);
return true;
default:
c = tab[cpt-1];
tab[cpt-1] = null;
cmd.ajouterConsommable(c);
return true;
}
}
catch (StockException e) {
return false;
}
}
/**
* Application du comportement defini dans l'objet {@link Strategy}
*/
public void acheter() {
strategy.acheter(this);
}
/**
* Calcul du montant de la commande
*/
public double payer() {
return cmd.montantTot();
}
/**
* Accesseurs
*/
public Commande getCommande() {
return cmd;
}
public double getPRachat() {
return pRachat;
}
public static int getNbConsommateurs() {
return nbConsommateurs;
}
/**
* Mutateur
*/
public void setStrategy(Strategy s) {
strategy = s;
}
}