-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
751 lines (553 loc) · 22.4 KB
/
main.cpp
File metadata and controls
751 lines (553 loc) · 22.4 KB
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
#include <iostream>
//#include <conio.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include "magasin.h"
#include "client.h"
//#include "main.h"
using namespace std;
Magasin *magasin;
/*!
* \brief readFromFiles Cette methode nous permet de repondre a la question:
* 8.b) Ecrire le code nécessaire à la lecture des données (produits, clients et commandes) dans des fichiers.
* \param _fichier
* \return
*/
int readFromFiles(std::string _fichier)
{
std::stringstream ss;
ss <<"C:/temp/mootez/tp_3easystore/" << _fichier <<".txt";
std::string s = ss.str();
cout << s << endl;
ifstream fichier(s, ios::in); // on ouvre le fichier en lecture
if(fichier) // si l'ouverture a réussi
{
string line;
while (getline(fichier, line))
{
vector<string> fields;
istringstream ss(line);
string word;
while (getline(ss, word,';'))
fields.push_back(word);
if(_fichier=="produits"){
unsigned int productId=0;
try {
productId=stoi(fields[0]);
string _titre=fields[1];
string _description=fields[2];
unsigned int _quantiteDisponible=stoi(fields[3]);
double _prix=stod(fields[4]);
Product *p1=new Product(productId,_titre, _description, _quantiteDisponible, _prix);
magasin->newProduct(p1);
}
catch(string const& e) //On rattrape les strings lancés
{
cout << "Exception occurred while reading line # "<< productId<< endl;
cerr << e << endl;
}
}
else if(_fichier=="clients"){
unsigned int clientId=0;
try {
clientId=stoi(fields[0]);
string fname=fields[1];
string lname=fields[2];
Client *c1=new Client(clientId,fname, lname);
magasin->newClient(c1);
}
catch(string const& e) //On rattrape les strings lancés
{
cout << "Exception occurred while reading line # "<< clientId<< endl;
cerr << e << endl;
}
}
else if(_fichier=="orders"){
unsigned int orderId;
try {
orderId=stoi(fields[0]);
unsigned int clientId=stoi(fields[1]);
string status=fields[2];
Order *o=new Order();
o->setOrderId(orderId);
o->setCilent( magasin->findClientById(clientId));
o->setStatus(status);
magasin->newOrder(o);
}
catch(string const& e) //On rattrape les strings lancés
{
cout << "Exception occurred while reading line # "<< orderId<< endl;
cerr << e << endl;
}
}
else if(_fichier=="orderDetails"){
unsigned int orderId;
try {
orderId=stoi(fields[0]);
unsigned int productId=stoi(fields[1]);
unsigned int quantity=stoi(fields[2]);
Product *p=new Product();
Order *o=new Order();
*p=magasin->findProductById(productId);
*o=magasin->findOrderById(orderId);
p->setQuantiteDisponible(quantity);
o->addProduct(*p);
for(unsigned int i = 0; i <magasin->orders().size(); i++)
{
if(magasin->orders()[i].getOrderId()==orderId){
magasin->updateOrders(i,*o);
break;
}
}
}
catch(string const& e) //On rattrape les strings lancés
{
cout << "Exception occurred while reading line # "<< orderId<< endl;
cerr << e << endl;
}
}
}
fichier.close(); // on ferme le fichier
}
else // sinon
cerr << "Impossible d'ouvrir le fichier !" << endl;
return 0;
}
/*!
* \brief writeToFiles Cette methode nous permet de repondre a la question:
* 8.b) Ecrire le code nécessaire à l'enregistrement des données (produits, clients et commandes) dans des fichiers.
* \param filename
*/
void writeToFiles( std::string filename)
{
std::stringstream ss;
ss <<"C:/temp/mootez/tp_3easystore/" << filename <<".txt";
std::string s = ss.str();
cout << s << endl;
ofstream fichier(s, ios::out | ios::trunc); //déclaration du flux et ouverture du fichier
if(fichier) // si l'ouverture a réussi
{
if(filename=="produits"){
for(unsigned int i = 0; i < magasin->products().size(); i++)
{
std::string line=magasin->products()[i].toString();
fichier << line <<endl;
}
}
else if(filename=="clients")
{
for(unsigned int i = 0; i < magasin->clients().size(); i++)
{
std::string line=magasin->clients()[i].toString();
fichier << line <<endl;
}
}
else if(filename=="orders")
{
for(unsigned int i = 0; i < magasin->orders().size(); i++)
{
std::string line=magasin->orders()[i].toFile();
fichier << line <<endl;
}
}
else if(filename=="orderDetails")
{
for(unsigned int i = 0; i < magasin->orders().size(); i++){
for(unsigned int j = 0; j < magasin->productNumberInOrder(i); j++){
stringstream ss;
ss<<magasin->orders()[i].getOrderId() ;
ss<<";";
ss<<magasin->orders()[i].getProductOrderId(j) ;
ss<<";";
ss<<magasin->orders()[i].getProducts()[j].getQuantiteDisponible() ;
std::string line=ss.str();
fichier << line <<endl;
}
}
}
fichier.close(); // on referme le fichier
}
else // sinon
cerr << "Erreur à l'ouverture !" << endl;
// r
}
/*!
* \brief Menu::menu_gestion_des_commandes permet de tester les methodes suivantes:
* 6.a) Créer la classe Commande avec ses variables membres (client, produits achetés, statut).
* 6.f) afficher toutes les informations de la commande.
* 7.a) valider une commande
* 7.b) mettre à jour le statut d'une commande
* 7.c) afficher toutes les commandes passées.
* 7.d) afficher toutes les commandes d'un client donné.
*/
void menu_gestion_des_commandes(){
int selection=0;
do{
cout<<"\n Menu (1,2,3 or 4, 0 pour sortir)"<<"\n";
cout<<"\n 0: pour revenir au menu principal";
cout<<"\n 1: afficher toutes les informations de la commande";
cout<<"\n 2: valider une commande";
cout<<"\n 3: mettre à jour le statut d'une commande";
cout<<"\n 4: afficher toutes les commandes passées";
cout<<"\n 5: afficher toutes les commandes d'un client donné";
cout<<"\n\n Enter selection: ";
cin>>selection;
switch(selection)
{
case 0:
{
//exit(0);
break;
}
case 1:
{
//6.f) Surcharger l'opérateur << pour pouvoir afficher toutes les informations de la commande.
magasin->diplayAllOrders();
}
break;
case 2:
{
//7.a) Ajouter à la classe Magasin une méthode ou une fonction helper permettant de valider une commande
string _firstName;
cout<<"\n 1: Entrer le Nom du client." <<endl;
cin >> _firstName;
string _lastName;
cout<<"\n 1: Entrer le Prenom du client." <<endl;
cin >> _lastName;
unsigned int _clientId=magasin->findClientByName(_firstName,_lastName);
if(magasin->validateAnOrder(_clientId))
{
cout<<"La commande a ete bien validee"<<endl;
}
}
break;
case 3:
{
//7.b) Ajouter une méthode ou une fonction helper permettant de mettre à jour le statut d'une commande
string _firstName;
cout<<"\n 1: Entrer le Nom du client." <<endl;
cin >> _firstName;
string _lastName;
cout<<"\n 1: Entrer le Prenom du client." <<endl;
cin >> _lastName;
unsigned int _ordertId;
cout<<"\n 1: Entrer le numero de la commande." <<endl;
cin >> _ordertId;
string newSatate;
cout<<"\n 1: Entrer le nouveau etat de la commande." <<endl;
cin >> newSatate;
unsigned int _clientId=magasin->findClientByName(_firstName,_lastName);
if(magasin->updateAnOrderState(_clientId,_ordertId,newSatate))
{
cout<<"La commande a ete bien mise a jour"<<endl;
}
}
break;
case 4:
{
// 7.c) afficher toutes les commandes passées.
string state;
cout<<"\n 1: Entrer l'etat des commandes a afficher." <<endl;
cin >> state;
magasin->diplayAllOrdersByState(state);
}
break;
case 5:
{
//7.d) Ajouter une méthode ou une fonction helper permettant d'afficher toutes les commandes d'un client donné.
string _firstName;
cout<<"\n 1: Entrer le Nom du client." <<endl;
cin >> _firstName;
string _lastName;
cout<<"\n 1: Entrer le Prenom du client." <<endl;
cin >> _lastName;
unsigned int _clientId=magasin->findClientByName(_firstName,_lastName);
magasin->displayOrderOfAClient(_clientId);
}
break;
}
system("cls");
}while((selection > 0) && (selection < 6));
}
//////////////////////+++++++++++++++++++++++++++++++++////////////////////////
/*!
* \brief Menu::menu_gestion_des_utilisateurs permet de tester les methodes suivantes:
* 4.a) Créer la classe Client avec ses variables membres (identifiant, prenom, nom, panier d'achat) et ses fonctions getters.
* 4.b) ajouter un produit au panier d'achat.
* 4.c) vider le panier d'achat.
* 4.d) modifier la quantité d'un produit ajouté au panier d'achat
* 4.e) supprimer un produit du panier d'achat.
* 4.f) afficher toutes les informations du client (incluant les produits du panier d'achat.
* 5.a) ajouter un nouveau client au magasin.
* 5.b) afficher à l'écran tous les clients du magasin.
* 5.c) afficher à l'écran un client sélectionné par son nom ou son identifiant.
* 5.d) ajouter un produit au panier d'achat d'un client.
* 5.e) supprimer un produit au panier d'achat d'un client.
* 5.f) modifier la quantité d'un produit du panier d'achat d'un client.
*/
void menu_gestion_des_utilisateurs(){
int flag4=0;
// system("cls");
do{
cout<<"\n Menu (1,2,3,4,5 or 6, 0 pour revenir au menu pricipal)"<<"\n";
cout<<"\n 0: pour revenir au menu principal";
cout<<"\n 1: Tester la classe Client avec ses variables membres";
cout<<"\n 2: Ajouter un produit au panier d'achat";
cout<<"\n 3: Vider le panier d'achat";
cout<<"\n 4: Modifier la quantité d'un produit ajouté au panier d'achat";
cout<<"\n 5: supprimer un produit du panier d'achat";
cout<<"\n 6: afficher toutes les informations du client (incluant les produits du panier d'achat)";
cout<<"\n\n Enter selection: ";
cin>>flag4;
switch(flag4)
{
case 0:
{
system("cls");
break;
}
case 1:
{
// 4.a) Créer la classe Client avec ses variables membres (identifiant, prenom, nom, panier d'achat) et ses fonctions getters.
string _firstName;
cout<<"\n 1: Entrer le Nom du client." <<endl;
cin >> _firstName;
string _lastName;
cout<<"\n 1: Entrer le Prenom du client." <<endl;
cin >> _lastName;
unsigned int _clientId=magasin->findClientByName(_firstName,_lastName);
Client *cl=new Client(_clientId,_firstName,_lastName);
magasin->newClient(cl);
}
break;
case 2:
{
//4.b) Ajouter une méthode ou une fonction helper permettant
//d'ajouter un produit au panier d'achat.
string _firstName;
cout<<"\n 1: Entrer le Nom du client." <<endl;
cin >> _firstName;
string _lastName;
cout<<"\n 1: Entrer le Prenom du client." <<endl;
cin >> _lastName;
string _titre;
cout<<"\n 1: Entrer le titre du produit." <<endl;
cin >>_titre;
unsigned int quantity;
cout<<"\n 1: Entrer la quantite du produit." <<endl;
cin >>quantity;
unsigned int _clientId=magasin->findClientByName(_firstName,_lastName);
unsigned int _productId=magasin->findProductKeyByName(_titre);
magasin->AddProductToBasket(_clientId,_productId,quantity) ;
}
break;
case 3:
{
//4.c) Ajouter une méthode ou une fonction helper permettant de vider le panier d'achat.
string _firstName;
cout<<"\n 1: Entrer le Nom du client." <<endl;
cin >> _firstName;
string _lastName;
cout<<"\n 1: Entrer le Prenom du client." <<endl;
cin >> _lastName;
unsigned int _clientId=magasin->findClientByName(_firstName,_lastName);
magasin->clearBasket(_clientId);
}
break;
case 4:
{
//4.d) Ajouter une méthode ou une fonction helper permettant
//de modifier la quantité d'un produit ajouté au panier d'achat
string _firstName;
cout<<"\n 1: Entrer le Nom du client." <<endl;
cin >> _firstName;
string _lastName;
cout<<"\n 1: Entrer le Prenom du client." <<endl;
cin >> _lastName;
string _titre;
cout<<"\n 1: Entrer le titre du produit." <<endl;
cin >>_titre;
unsigned int new_quantity;
cout<<"\n 1: Entrer la nouvelle quantite du produit(nombre)" <<endl;
cin >> new_quantity;
unsigned int _clientId=magasin->findClientByName(_firstName,_lastName);
unsigned int _productId=magasin->findProductKeyByName(_titre);
magasin->modifyProductQuantityInBasket(_clientId, _productId, new_quantity);
}
break;
//4.e) Ajouter une méthode ou une fonction helper permettant de supprimer un produit
//du panier d'achat.
case 5:
{
string _firstName;
cout<<"\n 1: Entrer le Nom du client." <<endl;
cin >> _firstName;
string _lastName;
cout<<"\n 1: Entrer le Prenom du client." <<endl;
cin >> _lastName;
string _titre;
cout<<"\n 1: Entrer le titre du produit." <<endl;
cin >>_titre;
unsigned int _clientId=magasin->findClientByName(_firstName,_lastName);
unsigned int _productId=magasin->findProductKeyByName(_titre);
magasin->deleteProductFromBasket(_clientId, _productId);
}
break;
//4.f) Surcharger l'opérateur << pour pouvoir afficher toutes les
//informations du client (incluant les produits du panier d'achat.
case 6:
{
string _firstName;
cout<<"\n 1: Entrer le Nom du client." <<endl;
cin >> _firstName;
string _lastName;
cout<<"\n 1: Entrer le Prenom du client." <<endl;
cin >> _lastName;
unsigned int _clientId=magasin->findClientByName(_firstName,_lastName);
magasin->displayProductOfAClient(_clientId);
}
break;
}
system("cls");
}while((flag4 > 0) && (flag4 < 7));
}
////////////////////++++++++++++++++++++++++++++++++++++++++///////////////////
/*!
* \brief Menu::menu_gestion_du_magasin permet de tester les methodes suivantes:
* 2.a) modifier la quantité disponible d'un produit.
* 2.b) afficher un produit.
* 3.a) ajouter un nouveau produit au magasin.
* 3.b) afficher tous les produits référencés dans le magasin.
* 3.c) afficher un produit sélectionné par son nom.
* 3.c) Mettre à jour la quantité d'un produit sélectionné par son nom.
*/
void menu_gestion_du_magasin(){
int flag=0;
do{
cout<<"\n Menu (1,2,3 or 4, 0 pour revenir au menu pricipal)"<<"\n";
cout<<"\n 0: pour revenir au menu principal";
cout<<"\n 1: ajouter un nouveau produit au magasin.";
cout<<"\n 2: afficher tous les produits reférences dans le magasin.";
cout<<"\n 3: afficher un produit selectionne par son nom";
cout<<"\n 4: mettre à jour la quantite d'un produit selectionne par son nom";
cout<<"\n\n Enter selection: ";
cin>>flag;
switch(flag)
{
case 0:
{
system("cls");
break;
}
case 1:
{
string _titre;
cout<<"\n 1: Entrer le titre du produit." <<endl;
cin >>_titre;
string _description;
cout<<"\n 1: Entrer une description du produit." <<endl;
cin >>_description;
unsigned int _quantiteDisponible;
cout<<"\n 1: Entrer _quantite disponible du produit(nombre)" <<endl;
cin >> _quantiteDisponible;
double _prix;
cout<<"\n 1: Entrer le prix du produit(double)" <<endl;
cin >>_prix;
unsigned int _productId=magasin->findProductKeyByName(_titre);
Product *p1=new Product(_productId,_titre, _description, _quantiteDisponible, _prix);
magasin->newProduct(p1);
}
break;
case 2:
{
magasin->diplayAllProducts();
}
break;
case 3:
{
string ptitle;
cout<<"\n 1: Entrer le titre du produit." <<endl;
cin >> ptitle;
magasin->diplayProductsByName(ptitle);
}
break;
case 4:
{
string ptitle;
cout<<"\n 1: Entrer le titre du produit." <<endl;
cin >> ptitle;
unsigned int new_quantity;
cout<<"\n 1: Entrer _quantite disponible du produit(nombre)" <<endl;
cin >> new_quantity;
magasin->updateProductsByQuantityByName(ptitle, new_quantity);
}
break;
}
system("cls");
}while((flag > 0) && (flag < 5));
}
/*!
* \brief menu_principal ce menu permet de repondre a la question:
* 8.a) créer un menu permettant de sélectionner l'action à faire :
* gestion du magasin, gestion des utilisateurs, gestion des commandes.
*/
void menu_principal(){
int selection=0;
system("cls");
do{
cout<<"\n Menu (1,2 ou 3; 0 pour sortir)"<<"\n";
cout<<"\n 0: pour sortir";
cout<<"\n 1: Gestion du Magasin";
cout<<"\n 2: Gestion des utilisateurs";
cout<<"\n 3: Gestion des commandes";
cout<<"\n\n Enter selection: ";
cin>>selection;
switch(selection)
{
case 0:
{
cout<<"sauvegarde des donnees du Magasin dans les fichiers suivants:"<<endl<<endl;
writeToFiles("produits");
writeToFiles("clients");
writeToFiles("orders");
writeToFiles("orderDetails");
exit(0);
}
case 1:
{
menu_gestion_du_magasin();
}
break;
case 2:
{
menu_gestion_des_utilisateurs();
}
break;
case 3:
{
menu_gestion_des_commandes();
}
break;
}
system("cls");
}while((selection > 0) && (selection < 6));
}
int main(){
//1.b) Ecrire un programme principal permettant de créer un objet (vide) de type magasin.
magasin = new Magasin();
//8.b) Ecrire le code nécessaire à l'enregistrement et à la lecture des données (produits, clients et commandes) dans des fichiers.
readFromFiles("produits");
readFromFiles("clients");
readFromFiles("orders");
readFromFiles("orderDetails");
readFromFiles("oorderDetails");
menu_principal();
return 0;
}