/
tarea.cpp
1065 lines (1009 loc) · 34.7 KB
/
tarea.cpp
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
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <limits>
#include <vector>
#include <iomanip>
#include <fstream> // Esto debe venir antes del uso de ifstream
#include <unordered_map>
#include <map>
#include "Cliente.h"
#include "Vehiculo.h"
#include "Auto.h"
#include "Camion.h"
#include "Moto.h"
#include "Venta.h"
#include "json.hpp"
using namespace std;
using json = nlohmann::json;
vector<Venta> ventas;
// crea un mapa de clientes por su rut
map<string, Cliente> clientes;
void clearScreen()
{
#ifdef _WIN32
system("cls");
#else
// ANSI escape sequence to clear screen
cout << "\033[2J\033[1;1H";
#endif
}
Cliente &getOrCreateCliente(string rut, string nombre)
{
// Busca el cliente por su RUT. Si no se encuentra, lo crea y lo añade al mapa.
auto it = clientes.find(rut);
if (it == clientes.end())
{
auto [new_it, inserted] = clientes.emplace(rut, Cliente(nombre, rut));
return new_it->second;
}
return it->second;
}
int precioBaseAuto(const string &marca)
{
if (marca == "Toyota")
{
return 19000000;
}
else if (marca == "Hyundai")
{
return 16000000;
}
else if (marca == "Suzuki")
{
return 12000000;
}
else if (marca == "Ford")
{
return 17000000;
}
else if (marca == "Chevrolet")
{
return 18000000;
}
else if (marca == "Honda")
{
return 21000000;
}
else
{
cout << "Marca no valida" << endl;
cout << "Marcas validas: Toyota, Hyundai, Suzuki, Ford, Chevrolet, Honda" << endl;
return 0;
}
}
int precioBaseCamion(const string &marca)
{
if (marca == "Volvo")
{
return 100000000;
}
else if (marca == "Iveco")
{
return 85000000;
}
else if (marca == "Mercedes")
{
return 110000000;
}
else if (marca == "Isuzu")
{
return 70000000;
}
else if (marca == "MAN")
{
return 105000000;
}
else if (marca == "Scania")
{
return 95000000;
}
else
{
cout << "Marca no valida" << endl;
cout << "Marcas validas: Volvo, Iveco, Mercedes, Isuzu, MAN, Scania" << endl;
return 0;
}
}
int precioBaseMoto(const string &marca)
{
if (marca == "Yamaha")
{
return 14000000;
}
else if (marca == "Suzuki")
{
return 8000000;
}
else if (marca == "Honda")
{
return 15000000;
}
else if (marca == "KTM")
{
return 5000000;
}
else if (marca == "BMW")
{
return 18000000;
}
else if (marca == "Kawasaki")
{
return 13000000;
}
else
{
cout << "Marca no valida" << endl;
cout << "Marcas validas: Yamaha, Suzuki, Honda, KTM, BMW, Kawasaki" << endl;
return 0;
}
}
void readAndCreateVehicles()
{
ifstream i("vehicles.json");
if (!i.is_open())
{
cerr << "No se pudo abrir el archivo vehicles.json" << endl;
return;
}
json j;
i >> j;
for (auto &venta : j)
{
Cliente &cliente = getOrCreateCliente(venta["cliente"]["rut"], venta["cliente"]["nombre"]);
string tipo = venta["vehiculo"]["tipo"];
int numeroMotor = venta["vehiculo"]["numeroMotor"];
int numeroRuedas = venta["vehiculo"]["numeroRuedas"];
string tipoCombustible = venta["vehiculo"]["tipoCombustible"];
TipoCombustible tipoCombustibleParseado;
if (tipoCombustible == "Gas")
{
tipoCombustibleParseado = TipoCombustible::Gas;
}
else if (tipoCombustible == "Electrico")
{
tipoCombustibleParseado = TipoCombustible::Electrico;
}
else if (tipoCombustible == "Gasolina")
{
tipoCombustibleParseado = TipoCombustible::Gasolina;
}
else
{
cout << "Tipo de combustible no valido" << endl;
return;
}
int maxLitros = venta["vehiculo"]["maxLitros"];
string marca = venta["vehiculo"]["marca"];
int anio = venta["vehiculo"]["anio"];
if (tipo == "Auto")
{
int numeroPuertas = venta["vehiculo"]["numeroPuertas"];
long long precio = precioBaseAuto(marca);
Auto *vehiculo = new Auto(numeroMotor, numeroRuedas, tipoCombustibleParseado, maxLitros, marca,
precio, anio, numeroPuertas);
Venta nueva_venta = *new Venta(*vehiculo, cliente, ventas.size());
ventas.push_back(nueva_venta); // Agregar la nueva venta al vector
}
else if (tipo == "Camion")
{
int numeroPuertas = venta["vehiculo"]["numeroPuertas"];
long long precio = precioBaseCamion(marca);
Camion *vehiculo = new Camion(numeroMotor, numeroRuedas, tipoCombustibleParseado, maxLitros, marca,
precio, anio, numeroPuertas);
Venta nueva_venta = *new Venta(*vehiculo, cliente, ventas.size());
ventas.push_back(nueva_venta); // Agregar la nueva venta al vector
}
else if (tipo == "Moto")
{
long long precio = precioBaseMoto(marca);
Moto *vehiculo = new Moto(numeroMotor, numeroRuedas, tipoCombustibleParseado, maxLitros, marca,
precio, anio);
Venta nueva_venta = *new Venta(*vehiculo, cliente, ventas.size());
ventas.push_back(nueva_venta); // Agregar la nueva venta al vector
}
}
}
void mostrarVehiculo(Vehiculo *vehiculo, Cliente *cliente)
{
clearScreen();
cout << "-------------------" << endl;
cout << "Cliente: " << cliente->getNombre() << " (Rut: " << cliente->getRut() << ")" << endl;
cout << "-------------------" << endl;
cout << "Venta de vehiculo de tipo: " << vehiculo->getTipo() << endl;
cout << "-------------------" << endl;
cout << "Marca: " << vehiculo->getMarca() << endl;
cout << "Anio: " << vehiculo->getAnio() << endl;
cout << "Numero de motor: " << vehiculo->getNumeroMotor() << endl;
cout << "Numero de ruedas: " << vehiculo->getNumeroRuedas() << endl;
cout << "Tipo de combustible: " << vehiculo->getTipoCombustibleString() << endl;
if (vehiculo->getTipoCombustibleString() == "Gasolina")
{
cout << "Tanque: " << vehiculo->getMaxLitros() << endl;
}
else
{
cout << "Tanque: NO APLICA" << endl;
}
cout << "Valor Original: " << vehiculo->getPrecio() << endl;
cout << "Valor Final: " << vehiculo->calcularPrecioFinal() << endl;
}
void agregarAuto(Cliente *cliente)
{
int numeroMotor, numeroRuedas, maxLitros, anio, numeroPuertas;
TipoCombustible tipoCombustible;
string marca;
long long precio;
cout << " <---- Ingresa el Auto ---> " << endl;
cout << "Marcas validas: Toyota, Hyundai, Suzuki, Ford, Chevrolet, Honda" << endl;
cout << "Ingresa la marca: ";
cin >> marca;
precio = precioBaseAuto(marca);
if (precio == 0)
{
return;
}
cout << "Precio base del auto: " << precio << endl;
cout << "Ingresa el anio: ";
cin >> anio;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entero. Intente de nuevo." << endl;
return;
}
cout << "Ingresa el numero de motor: ";
cin >> numeroMotor;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entero. Intente de nuevo." << endl;
return;
}
cout << "Ingresa el numero de ruedas: ";
cin >> numeroRuedas;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entero. Intente de nuevo." << endl;
return;
}
cout << "Tipo de Combustible:" << endl;
cout << " 1 -> Gasolina" << endl;
cout << " 2 -> Gas" << endl;
cout << " 3 -> Electrico" << endl;
cout << "Selecciona el tipo de combustible: ";
int tipoCombustibleVal;
cin >> tipoCombustibleVal;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entre 1 y 3. Intente de nuevo." << endl;
return;
}
if (tipoCombustibleVal == 1)
{
tipoCombustible = TipoCombustible::Gasolina;
}
else if (tipoCombustibleVal == 2)
{
tipoCombustible = TipoCombustible::Gas;
}
else if (tipoCombustibleVal == 3)
{
tipoCombustible = TipoCombustible::Electrico;
}
else
{
cout << "Tipo de combustible no valido" << endl;
return;
}
if (tipoCombustible == TipoCombustible::Gasolina)
{
cout << "Ingresa el maximo de litros: ";
cin >> maxLitros;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entero. Intente de nuevo." << endl;
return;
}
}
else
{
maxLitros = 0;
}
cout << "Ingresa el numero de puertas: ";
cin >> numeroPuertas;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entero. Intente de nuevo." << endl;
return;
}
Vehiculo *autoVehiculo = new Auto(numeroMotor, numeroRuedas, tipoCombustible, maxLitros, marca, precio, anio,
numeroPuertas);
if (cliente != NULL)
{
Venta venta = *new Venta(*autoVehiculo, *cliente, ventas.size());
ventas.push_back(venta);
cout << "Venta agregada correctamente" << endl;
mostrarVehiculo(autoVehiculo, cliente);
}
else
{
cout << "Necesitas tener un cliente registrado para hacer una venta." << endl;
}
return;
}
void agregarCamion(Cliente *cliente)
{
int numeroMotor, numeroRuedas, maxLitros, anio, numeroPuertas;
TipoCombustible tipoCombustible;
string marca;
long long precio;
cout << " <---- Ingresa el Camion ---> " << endl;
cout << "Marcas validas: Volvo, Iveco, Mercedes, Isuzu, MAN, Scania" << endl;
cout << "Ingresa la marca: ";
cin >> marca;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar una marca. Intente de nuevo." << endl;
return;
}
precio = precioBaseCamion(marca);
if (precio == 0)
{
return;
}
cout << "Precio base del Camion: " << precio << endl;
cout << "Ingresa el anio: ";
cin >> anio;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entero. Intente de nuevo." << endl;
return;
}
cout << "Ingresa el numero de motor: ";
cin >> numeroMotor;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entero. Intente de nuevo." << endl;
return;
}
cout << "Ingresa el numero de ruedas: ";
cin >> numeroRuedas;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entero. Intente de nuevo." << endl;
return;
}
cout << "Tipo de Combustible:" << endl;
cout << " 1 -> Gasolina" << endl;
cout << " 2 -> Gas" << endl;
cout << " 3 -> Electrico" << endl;
cout << "Selecciona el tipo de combustible: ";
int tipoCombustibleVal;
cin >> tipoCombustibleVal;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entre 1 y 3. Intente de nuevo." << endl;
return;
}
if (tipoCombustibleVal == 1)
{
tipoCombustible = TipoCombustible::Gasolina;
}
else if (tipoCombustibleVal == 2)
{
tipoCombustible = TipoCombustible::Gas;
}
else if (tipoCombustibleVal == 3)
{
tipoCombustible = TipoCombustible::Electrico;
}
else
{
cout << "Tipo de combustible no valido" << endl;
return;
}
if (tipoCombustible == TipoCombustible::Gasolina)
{
cout << "Ingresa el maximo de litros: ";
cin >> maxLitros;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entero. Intente de nuevo." << endl;
return;
}
}
else
{
maxLitros = 0;
}
cout << "Ingresa el numero de puertas: ";
cin >> numeroPuertas;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entero. Intente de nuevo." << endl;
return;
}
Vehiculo *autoVehiculo = new Camion(numeroMotor, numeroRuedas, tipoCombustible, maxLitros, marca, precio, anio,
numeroPuertas);
if (cliente != NULL)
{
Venta venta = *new Venta(*autoVehiculo, *cliente, ventas.size());
ventas.push_back(venta);
cout << "Venta agregada correctamente" << endl;
mostrarVehiculo(autoVehiculo, cliente);
}
else
{
cout << "Necesitas tener un cliente registrado para hacer una venta." << endl;
}
return;
}
void agregarMoto(Cliente *cliente)
{
int numeroMotor, numeroRuedas, maxLitros, anio;
TipoCombustible tipoCombustible;
string marca;
long long precio;
cout << " <---- Ingresa la Moto ---> " << endl;
cout << "Marcas validas: Yamaha, Suzuki, Honda, KTM, BMW, Kawasaki" << endl;
cout << "Ingresa la marca: ";
cin >> marca;
precio = precioBaseMoto(marca);
if (precio == 0)
{
return;
}
cout << "Precio base de la moto: " << precio << endl;
cout << "Ingresa el anio: ";
cin >> anio;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entero. Intente de nuevo." << endl;
return;
}
cout << "Ingresa el numero de motor: ";
cin >> numeroMotor;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entero. Intente de nuevo." << endl;
return;
}
cout << "Ingresa el numero de ruedas: ";
cin >> numeroRuedas;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entero. Intente de nuevo." << endl;
return;
}
cout << "Tipo de Combustible:" << endl;
cout << " 1 -> Gasolina" << endl;
cout << " 2 -> Gas" << endl;
cout << " 3 -> Electrico" << endl;
cout << "Selecciona el tipo de combustible: ";
int tipoCombustibleVal;
cin >> tipoCombustibleVal;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entre 1 y 3. Intente de nuevo." << endl;
return;
}
if (tipoCombustibleVal == 1)
{
tipoCombustible = TipoCombustible::Gasolina;
}
else if (tipoCombustibleVal == 2)
{
tipoCombustible = TipoCombustible::Gas;
}
else if (tipoCombustibleVal == 3)
{
tipoCombustible = TipoCombustible::Electrico;
}
else
{
cout << "Tipo de combustible no valido" << endl;
return;
}
if (tipoCombustible == TipoCombustible::Gasolina)
{
cout << "Ingresa el maximo de litros: ";
cin >> maxLitros;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entero. Intente de nuevo." << endl;
return;
}
}
else
{
maxLitros = 0;
}
Vehiculo *autoVehiculo = new Moto(numeroMotor, numeroRuedas, tipoCombustible, maxLitros, marca, precio, anio);
if (cliente != NULL)
{
Venta venta = *new Venta(*autoVehiculo, *cliente, ventas.size());
ventas.push_back(venta);
cout << "Venta agregada correctamente" << endl;
mostrarVehiculo(autoVehiculo, cliente);
}
else
{
cout << "Necesitas tener un cliente registrado para hacer una venta." << endl;
}
return;
}
void mostrarMejorCliente()
{
unordered_map<string, int> contadorCompras; // Mapa para contar las compras por RUT de cliente
Cliente *mejorCliente = nullptr;
int maximo = 0;
for (const auto &venta : ventas)
{
const string &rut = venta.getCliente().getRut();
contadorCompras[rut]++; // Incrementa el contador para este cliente
if (contadorCompras[rut] > maximo)
{
maximo = contadorCompras[rut];
mejorCliente = &const_cast<Cliente &>(venta.getCliente());
}
}
if (mejorCliente != nullptr)
{
cout << "El cliente que compro mas vehiculos es: " << mejorCliente->getNombre() << " ("
<< mejorCliente->getRut() << ")" << endl;
cout << "Con un total de: " << std::fixed << std::setprecision(2) << maximo << " vehiculos comprados" << endl;
}
else
{
cout << "No hay clientes registrados" << endl;
}
}
void mostrarCantidadCliente(Cliente *cliente, string tipoDeVehiculo)
{
int totalCompras = 0;
for (const auto &venta : ventas)
{
const string &rut = venta.getCliente().getRut();
if (rut == cliente->getRut())
{
if (tipoDeVehiculo == "Vehiculo" || venta.getVehiculo().getTipo() == tipoDeVehiculo)
{
totalCompras++;
}
}
}
const string vehiculoPlural = tipoDeVehiculo == "Camion" ? "es" : "s";
if (totalCompras > 0)
{
cout << "El cliente " << cliente->getNombre() << " (" << cliente->getRut() << ")" << endl;
cout << "Compro " << std::fixed << std::setprecision(2) << totalCompras << " " << tipoDeVehiculo << vehiculoPlural << endl;
}
else
{
cout << "El cliente " << cliente->getNombre() << " (" << cliente->getRut() << ")" << endl;
cout << "No ha comprado " << tipoDeVehiculo << vehiculoPlural << endl;
}
}
void mostrarTotalVentas(string tipoDeVehiculo)
{
int totalCompras = 0;
for (const auto &venta : ventas)
{
if (tipoDeVehiculo == "Vehiculo" || venta.getVehiculo().getTipo() == tipoDeVehiculo)
{
totalCompras++;
}
}
const string vehiculoPlural = tipoDeVehiculo == "Camion" ? "nes" : "s";
if (totalCompras > 0)
{
cout << "Se han comprado " << std::fixed << std::setprecision(2) << totalCompras << " " << tipoDeVehiculo << vehiculoPlural << endl;
}
else
{
cout << "No se han comprado " << tipoDeVehiculo << vehiculoPlural << endl;
}
}
void mostrarPromedioVentas(string tipoDeVehiculo)
{
long long sumaPrecios = 0;
int contador = 0;
for (const auto &venta : ventas)
{
if (tipoDeVehiculo == "Vehiculo" || venta.getVehiculo().getTipo() == tipoDeVehiculo)
{
sumaPrecios += venta.getVehiculo().calcularPrecioFinal();
contador++;
}
}
if (contador == 0)
{
cout << "No hay ventas para este tipo de vehiculo: " << tipoDeVehiculo << endl;
return;
}
double promedio = static_cast<double>(sumaPrecios) / contador;
cout << "El precio promedio de ventas para " << tipoDeVehiculo << " es: " << std::fixed << std::setprecision(2) << promedio << endl;
}
void mostrarCabecera(Cliente *cliente)
{
clearScreen();
cout << " " << endl;
cout << " Autos Buenos " << endl;
cout << " " << endl;
cout << " + Cliente Actual: " << cliente->getNombre() << " (" << cliente->getRut() << ")" << endl;
cout << " " << endl;
}
void menu(void)
{
int opcion = 0;
Cliente *cliente = NULL;
/*
// TODO: TESTING Profe cuando vea esto, lo puede descomentar para poder cargar el archivo con 100 ventas para diferentes usuarios
readAndCreateVehicles();
cliente = &getOrCreateCliente("22222222-2", "Pedro");
// TODO: TESTING
*/
while (opcion != 4)
{
if (cliente == NULL)
{
string nombreCliente, rutCliente;
cout << " " << endl;
cout << " <---- Inicio de Sesion ---> " << endl;
cout << " " << endl;
cout << "Ingresa el nombre del cliente: ";
cin >> nombreCliente;
cout << "Ingresa el rut del cliente: ";
cin >> rutCliente;
cliente = &getOrCreateCliente(rutCliente, nombreCliente);
cout << " " << endl;
}
mostrarCabecera(cliente);
cout << " 1. Ventas" << endl;
cout << " 2. Estadisticas" << endl;
cout << " 3. Cerrar Sesion" << endl;
cout << " 4. Salir" << endl;
cout << " Seleccione una opcion: ";
cin >> opcion;
// Verificar si la entrada es válida
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entre 1 y 4. Intente de nuevo." << endl;
continue;
}
if (opcion == 1)
{
int opcion_venta = -1;
while (opcion_venta != 4)
{
clearScreen();
cout << " " << endl;
cout << " <---- Venta de Vehiculos ---> " << endl;
cout << " " << endl;
cout << " 1. Agregar Auto" << endl;
cout << " 2. Agregar Moto" << endl;
cout << " 3. Agregar Camion" << endl;
cout << " 4. Volver " << endl;
cout << " " << endl;
cout << " Seleccione una opcion: ";
cin >> opcion_venta;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entre 1 y 4. Intente de nuevo." << endl;
continue;
}
if (opcion_venta == 1)
{
agregarAuto(cliente);
cout << "Presione ENTER para continuar...";
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin.get();
}
else if (opcion_venta == 2)
{
agregarMoto(cliente);
cout << "Presione ENTER para continuar...";
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin.get();
}
else if (opcion_venta == 3)
{
agregarCamion(cliente);
cout << "Presione ENTER para continuar...";
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin.get();
}
else if (opcion_venta == 4)
{
cout << "Volviendo al menu anterior" << endl;
}
else
{
cout << "Ingresa una opcion entre 1 y 4" << endl;
}
}
}
else if (opcion == 2)
{
int opcion_stats = 0;
while (opcion_stats != 5)
{
clearScreen();
cout << " " << endl;
cout << " <---- Estadisticas de Ventas ---> " << endl;
cout << " " << endl;
cout << " 1. Cliente que compro mas Vehiculos" << endl;
cout << " 2. Total de Compras" << endl;
cout << " 3. Total de Ventas" << endl;
cout << " 4. Promedio de Ventas" << endl;
cout << " 5. Volver" << endl;
cout << "Seleccione una opcion : ";
cin >> opcion_stats;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entre 1 y 5. Intente de nuevo." << endl;
continue;
}
if (opcion_stats == 1)
{
mostrarMejorCliente();
// hacer una pausa
cout << "Presione ENTER para continuar...";
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin.get();
}
else if (opcion_stats == 2)
{
int opcion_stats_promedio = 0;
while (opcion_stats_promedio != 5)
{
clearScreen();
cout << " " << endl;
cout << " <---- Estadisticas | Cantidad de ventas del Usuario ---> " << endl;
cout << " " << endl;
cout << " 1. Cantidad de Autos comprados" << endl;
cout << " 2. Cantidad de Motos compradas" << endl;
cout << " 3. Cantidad de Camiones comprados" << endl;
cout << " 4. Cantidad de Vehiculos comprados" << endl;
cout << " 5. Volver" << endl;
cout << "Seleccione una opcion : ";
cin >> opcion_stats_promedio;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entre 1 y 5. Intente de nuevo." << endl;
continue;
}
if (opcion_stats_promedio == 1)
{
mostrarCantidadCliente(cliente, "Auto");
cout << "Presione ENTER para continuar...";
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin.get();
}
else if (opcion_stats_promedio == 2)
{
mostrarCantidadCliente(cliente, "Moto");
cout << "Presione ENTER para continuar...";
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin.get();
}
else if (opcion_stats_promedio == 3)
{
mostrarCantidadCliente(cliente, "Camion");
cout << "Presione ENTER para continuar...";
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin.get();
}
else if (opcion_stats_promedio == 4)
{
mostrarCantidadCliente(cliente, "Vehiculo");
cout << "Presione ENTER para continuar...";
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin.get();
}
else if (opcion_stats_promedio == 5)
{
cout << "Volviendo al menu anterior" << endl;
}
else
{
cout << "Ingresa una opcion entre 1 y 5" << endl;
}
}
}
else if (opcion_stats == 3)
{
int opcion_stats_total = 0;
while (opcion_stats_total != 5)
{
clearScreen();
cout << " " << endl;
cout << " <---- Estadisticas | Ventas Total de la empresa ---> " << endl;
cout << " " << endl;
cout << " 1. Total de ventas de Autos" << endl;
cout << " 2. Total de ventas de Motos" << endl;
cout << " 3. Total de ventas de Camiones" << endl;
cout << " 4. Total de ventas de Vehiculos" << endl;
cout << " 5. Volver" << endl;
cout << "Seleccione una opcion : ";
cin >> opcion_stats_total;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entre 1 y 5. Intente de nuevo." << endl;
continue;
}
if (opcion_stats_total == 1)
{
mostrarTotalVentas("Auto");
cout << "Presione ENTER para continuar...";
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin.get();
}
else if (opcion_stats_total == 2)
{
mostrarTotalVentas("Moto");
cout << "Presione ENTER para continuar...";
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin.get();
}
else if (opcion_stats_total == 3)
{
mostrarTotalVentas("Camion");
cout << "Presione ENTER para continuar...";
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin.get();
}
else if (opcion_stats_total == 4)
{
mostrarTotalVentas("Vehiculo");
cout << "Presione ENTER para continuar...";
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin.get();
}
else if (opcion_stats_total == 5)
{
cout << "Volviendo al menu anterior" << endl;
}
else
{
cout << "Ingresa una opcion entre 1 y 5" << endl;
}
}
}
else if (opcion_stats == 4)
{
int opcion_stats_prom = 0;
while (opcion_stats_prom != 5)
{
clearScreen();
cout << " " << endl;
cout << " <---- Estadisticas | Promedios Totales de la empresa ---> " << endl;
cout << " " << endl;
cout << " 1. Promedio de ventas de Autos" << endl;
cout << " 2. Promedio de ventas de Motos" << endl;
cout << " 3. Promedio de ventas de Camiones" << endl;
cout << " 4. Promedio de ventas de Vehiculos" << endl;
cout << " 5. Volver" << endl;
cout << "Seleccione una opcion : ";
cin >> opcion_stats_prom;
if (cin.fail())
{
cin.clear();
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cout << "Debe ingresar un numero entre 1 y 5. Intente de nuevo." << endl;
continue;
}
if (opcion_stats_prom == 1)
{
mostrarPromedioVentas("Auto");
cout << "Presione ENTER para continuar...";
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
cin.get();
}
else if (opcion_stats_prom == 2)