-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStock.cpp
99 lines (77 loc) · 2.51 KB
/
Stock.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
#include "Stock.h"
#include "Producto.h"
#include "StockDto.h"
Stock::Stock() {};
//GETTERS
int Stock::getIdProducto() {
return idProducto;
}
int Stock::getCantidad()
{
return cantidadTotal;
}
//
Response<Stock> Stock::gestionarStock(int cantidad, int idProducto, char tipoTransaccion) {
Response<Stock> response;
Response<StockDto> responseDto;
switch (tipoTransaccion) {
case 'v':
responseDto = restarStock<StockDto>(cantidad, idProducto);
break;
case 'c':
responseDto = sumarStock<StockDto>(cantidad, idProducto);
break;
default:
break;
}
return response;
}
//Función para pasar como parametro:
template <class T>
bool Stock::validarStock(int idProducto, T objetoArchivo) {
T stock = (T)objetoArchivo;
if (idProducto == stock.getId()) {
return true;
}
return false;
}
typedef bool(Stock:: *validarStock)(int, int);
template <class T>
Response<StockDto> Stock::sumarStock(int cantidad, int idProducto) {
Response<StockDto> response;
Archivo<StockDto> archivoStock("stock.dat");
int cantidadActualizada = cantidad;
response = archivoStock.buscarUnRegistro(idProducto);
if (response.getSuccess()) {
cantidadActualizada = response.getData().getCantidadTotal() + cantidad;
}
StockDto stock(idProducto, cantidadActualizada);
auto funcionValidar = [this](int idProducto, T objetoArchivo) {return validarStock(idProducto, objetoArchivo); };
response = archivoStock.grabarOModificarRegistro(stock, idProducto, funcionValidar);
return response;
}
typedef bool(Stock::* validarStock)(int, int);
template <class T>
Response<StockDto> Stock::restarStock(int cantidad, int idProducto) {
Response<StockDto> response;
Archivo<StockDto> archivoStock("stock.dat");
int cantidadActualizada = cantidad;
response = archivoStock.buscarUnRegistro(idProducto);
if (response.getSuccess()) {
cantidadActualizada = response.getData().getCantidadTotal() - cantidad;
}
StockDto stock(idProducto, cantidadActualizada);
auto funcionValidar = [this](int idProducto, T idArchivo) {return validarStock(idProducto, idArchivo); };
response = archivoStock.grabarOModificarRegistro(stock, idProducto, funcionValidar);
return response;
}
void Stock::mostrarStock()
{
Archivo <Producto> archivoProducto("productos.dat");
Response <Producto> responseProducto;
Producto producto;
responseProducto = archivoProducto.listarUnRegistro(this->getIdProducto(), producto);
cout << responseProducto.getData().getId() << endl;
cout << responseProducto.getData().getDescripcionProducto() << endl;
cout << this->getCantidad() << endl;
}