-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoulangerie.java
367 lines (282 loc) · 7.56 KB
/
Boulangerie.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
import java.util.ArrayList;
/**
* Classe principale faisant le lien avec les autres classes
* Cette classe ne possede qu'une seule instance : un singleton
* Permet la simulation de vente au sein de ce singleton
*/
public class Boulangerie {
/**
* Attributs et singleton de la classe
*/
private Object[][] terrain;
private int nbEmploye, taille, nbMaxGateaux, nbGateaux, nbMaxPains, nbPains, nbMaxSucreries, nbSucreries, nbMaxViennoiseries, nbViennoiseries;
private double chiffreAffaires, coutMoyGateaux, coutMoyPains, coutMoySucreries, coutMoyViennoiseries;
private Gateau[] gtab;
private Pain[] ptab;
private Sucrerie[] stab;
private Viennoiserie[] vtab;
private ArrayList<Employe> etab;
private Consommateur conso;
private static Boulangerie singletonBoulangerie = null;
/**
* Constructeur
*
* @param n : taille de la boulangerie
* @param g : nombre maximum de {@link Gateau}
* @param p : nombre maximum de {@link Pain}
* @param s : nombre maximum de {@link Sucrerie}
* @param v : nombre maximum de {@link Viennoiserie}
*/
private Boulangerie (int n, int g, int p, int s, int v) {
taille = n; chiffreAffaires = 0; nbEmploye = 1;
terrain = new Object[taille][taille];
etab = new ArrayList<Employe> ();
etab.add(new Employe("Employe"));
nbMaxGateaux = g; nbGateaux = 0;
gtab = new Gateau[nbMaxGateaux];
nbMaxPains = p; nbPains = 0;
ptab = new Pain[nbMaxPains];
nbMaxSucreries = s; nbSucreries = 0;
stab = new Sucrerie[nbMaxSucreries];
nbMaxViennoiseries = v; nbViennoiseries = 0;
vtab = new Viennoiserie[nbMaxViennoiseries];
}
/**
* Redefinition de la methode standard toString()
*
* @return une chaine de caracteres representant la boulangerie
*/
@Override
public String toString() {
String s = "+";
for (int k = 0; k < terrain[0].length; k++) {
s += "-";
}
s += "+\n";
for (int i = 0; i < terrain.length; i++) {
s += "|";
for (int j = 0; j < terrain[0].length; j++) {
if (terrain[i][j] != null) {
s += (terrain[i][j].toString()).charAt(0);
}
else {
if (i <= taille / 3) {
s += "*";
}
else {
s += " ";
}
}
}
s += "|\n";
}
s += "+";
for (int k = 0; k < terrain[0].length; k++) {
s += " ";
}
return s + "+";
}
/**
* Remplissage de l'attribut terrain de la boulangerie
* Selon les tableaux de {@link Consommable}
*/
public void prepTerrain() {
for (int i = 0; i < terrain.length; i++) {
for (int j = 0; j < terrain[0].length; j++) {
terrain[i][j] = null;
}
}
for (Employe e : etab) {
if (e != null) {
terrain[e.getX()][e.getY()] = e;
}
}
if (gtab[0] != null) {
int xG = gtab[0].getX();
int yG = gtab[0].getY();
terrain[xG][yG] = gtab[0];
}
if (ptab[0] != null) {
int xP = ptab[0].getX();
int yP = ptab[0].getY();
terrain[xP][yP] = ptab[0];
}
if (stab[0] != null) {
int xS = stab[0].getX();
int yS = stab[0].getY();
terrain[xS][yS] = stab[0];
}
if (vtab[0] != null) {
int xV = vtab[0].getX();
int yV = vtab[0].getY();
terrain[xV][yV] = vtab[0];
}
}
/**
* Paiement des employes
*/
public void payerEmployes() {
double montant = 0;
for (Employe e : etab) {
if (e != null) {
montant += e.getSalaire();
}
}
chiffreAffaires -= montant;
}
/**
* Reinitialisation de l'etat de la boulangerie entre deux "jours"
*
* @param xG : abscisse des {@link Gateau}
* @param yG : ordonnee des {@link Gateau}
* @param xP : abscisse des {@link Pain}
* @param yP : ordonnee des {@link Pain}
* @param xS : abscisse des {@link Sucrerie}
* @param yS : ordonnee des {@link Sucrerie}
* @param xV : abscisse des {@link Viennoiserie}
* @param yV : ordonnee des {@link Viennoiserie}
*/
public void reinitialiser(int xG, int yG, int xP, int yP, int xS, int yS, int xV, int yV) {
this.remplirTout();
Boulangerie.setAllXY(gtab, xG, yG);
Boulangerie.setAllXY(ptab, xP, yP);
Boulangerie.setAllXY(stab, xS, yS);
Boulangerie.setAllXY(vtab, xV, yV);
}
/**
* Methode permettant de simuler
* la venue d'un consommateur
*/
public void simuler() {
int x = (int) (Math.random() * (taille - taille / 2) + taille / 2);
int y = (int) (Math.random() * taille);
while (terrain[x][y] != null) {
x = (int) (Math.random() * (taille - taille / 2) + taille / 2);
y = (int) (Math.random() * taille);
}
conso = new Consommateur("Individu", x, y);
Strategy s;
double p = Math.random();
if (p < 0.6) {
s = new StrategyDefault();
}
else {
s = new StrategyEnfant();
}
this.prepTerrain();
conso.setStrategy(s);
terrain[conso.getX()][conso.getY()] = conso;
System.out.println(this);
conso.acheter();
chiffreAffaires += conso.payer();
}
/**
* Accesseurs
*/
public static Boulangerie getBoulangerie(int n, int g, int s, int p, int v) {
if (singletonBoulangerie == null) {
singletonBoulangerie = new Boulangerie(n,g,s,p,v);
return singletonBoulangerie;
}
return singletonBoulangerie;
}
public int getNbGateaux() {
return nbGateaux;
}
public int getNbPains() {
return nbPains;
}
public int getNbSucreries() {
return nbSucreries;
}
public int getNbViennoiseries() {
return nbViennoiseries;
}
public double getChiffreAffaires() {
return chiffreAffaires;
}
public Gateau[] getGTab() {
return gtab;
}
public Pain[] getPTab() {
return ptab;
}
public Sucrerie[] getSTab() {
return stab;
}
public Viennoiserie[] getVTab() {
return vtab;
}
public int getTaille() {
return taille;
}
public Consommateur getConsommateur() {
return conso;
}
/**
* Mutateurs
*/
public void addEmploye(Employe e) {
etab.add(new Employe("Employe"));
nbEmploye++;
}
public void decrNbG() {
nbGateaux--;
}
public void decrNbP() {
nbPains--;
}
public void decrNbS() {
nbSucreries--;
}
public void decrNbV() {
nbViennoiseries--;
}
public static void setAllXY(Consommable[] tab, int x, int y) {
for (Consommable c : tab) {
if (c != null) {
c.setXY(x, y);
}
}
}
public void setParametres(double moyG, double moyP, double moyS, double moyV) {
coutMoyGateaux = moyG;
coutMoyPains = moyP;
coutMoySucreries = moyS;
coutMoyViennoiseries = moyV;
}
public void remplirGateaux() {
for (int i = nbGateaux; i < gtab.length; i++) {
gtab[i] = Gateau.make();
chiffreAffaires -= coutMoyGateaux;
}
nbGateaux = gtab.length;
}
public void remplirPains() {
for (int i = nbPains; i < ptab.length; i++) {
ptab[i] = BaguetteFactory.make();
chiffreAffaires -= coutMoyPains;
}
nbPains = ptab.length;
}
public void remplirSucreries() {
for (int i = nbSucreries; i < stab.length; i++) {
stab[i] = Sucrerie.make();
chiffreAffaires -= coutMoySucreries;
}
nbSucreries = stab.length;
}
public void remplirViennoiseries() {
for (int i = nbViennoiseries; i < vtab.length; i++) {
vtab[i] = Viennoiserie.make();
chiffreAffaires -= coutMoyViennoiseries;
}
nbViennoiseries = vtab.length;
}
public void remplirTout() {
this.remplirGateaux();
this.remplirPains();
this.remplirSucreries();
this.remplirViennoiseries();
}
}