-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListaVentas.cpp
More file actions
290 lines (233 loc) · 6.27 KB
/
ListaVentas.cpp
File metadata and controls
290 lines (233 loc) · 6.27 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
#include "ListaVentas.h"
using namespace std;
ListaVentas::ListaVentas()
{
if((header = new NodoVenta) == nullptr) //creando header
{
cout << "exception, constructor ListaFacturas" << endl;
}
header->setPrev(header);
header->setNext(header);
}
ListaVentas::ListaVentas(ListaVentas &lst)
{
copyAll(lst);
}
ListaVentas::~ListaVentas()
{
deleteAll();
delete header;
}
bool ListaVentas::isEmpty()
{
return header->getNext() == header;
}
void ListaVentas::insertData(NodoVenta *prevNode, Venta &v)
{
if(prevNode != nullptr and !isValidPos(prevNode))
{
cout<< "pocision invalida, intentando insertar lista Venta" << endl;
}
NodoVenta* aux(nullptr);
aux = new NodoVenta(v);
if(prevNode == nullptr)
{
prevNode = header;
}
cout << "insert data Venta .. " << aux->getData().toString() << endl;
aux->setPrev(prevNode);
aux->setNext(prevNode->getNext());
prevNode->getNext()->setPrev(aux);
prevNode->setNext(aux);
}
void ListaVentas::deleteData(NodoVenta *node)
{
if(!isValidPos(node))
{
cout << "posicion invalida, intentando eliminar , lista Venta" << endl;
return;
}
node->getPrev()->setNext(node->getNext());
node->getNext()->setPrev(node->getPrev());
//eliminar la lista anidada
node->getData().getListaProductosVenta().deleteAll();
delete node;
}
void ListaVentas::deleteAll()
{
NodoVenta* aux(nullptr);
while(header->getNext() != header)
{
aux = header->getNext();
header->setNext(aux->getNext());
delete aux;
}
header->setPrev(header);
}
bool ListaVentas::isValidPos(NodoVenta *node)
{
NodoVenta* aux(header->getNext());
while(aux != header)
{
if(aux == node)
{
return true;
}
aux = aux->getNext();
}
return false;
}
void ListaVentas::copyAll(ListaVentas &lista)
{
NodoVenta* aux(lista.header->getNext());
NodoVenta* newNode;
while (aux != lista.header) //**cuando llegue al final, cuando regrese a header, podriamos hacerlo con do_while
{
if ((newNode = new NodoVenta(aux->getData())) == nullptr) {
cout << "excepcion, ListaVentas copyAll" << endl;
}
newNode->setPrev(header->getPrev());
newNode->setNext(header);
header->getPrev()->setNext(newNode);
header->setPrev(newNode);
aux = aux->getNext();
}
}
NodoVenta *ListaVentas::getLastPos()
{
if(isEmpty())
{
return nullptr;
}
return header->getPrev();
}
NodoVenta *ListaVentas::findData(Venta &v)
{
NodoVenta* aux(header->getNext());
while( aux != header)
{
if(aux->getData() == v)
{
return aux;
}
aux = aux->getNext();
}
return nullptr;
}
Venta &ListaVentas::retrieve(NodoVenta *node)
{
if(!isValidPos(node))
{
cout << "excepcion, posicion invalida, intentando recuperar de lista de Ventas" << endl;
}
return node->getData();
}
std::string ListaVentas::toString()
{
NodoVenta* aux(header->getNext());
string result;
while (aux != header)
{
result += aux->getData().toString() + "\n\n";
aux = aux->getNext();
}
return result;
}
ListaVentas &ListaVentas::operator=(ListaVentas &lst)
{
deleteAll();
copyAll(lst);
return *this;
}
void ListaVentas::read() //listaVentas.txt
{
/*
* std::string codigoVenta;
* std::string codigoCliente;
* std::string codigoEmpleado;
* ListProd listaProdVenta;
* float total;
*/
char codigoVenta[15];
char codigoCliente[15];
char codigoEmpleado[15];
float tots;
int tamno(15);
Venta auxVenta;
ListProd prodListAux;
string lastCode("X");
string filename("listaVentas.txt");
deleteAll();
ifstream leer(filename, ios::in);
while (!leer.eof())
{
//leer codigo de venta
leer.read((char*)& codigoVenta, tamno);
string cod(codigoVenta);
if (lastCode == cod)
{
break;
}
//leer el resto de las variables
leer.read((char*)& codigoCliente, tamno);
leer.read((char*)& codigoEmpleado, tamno);
leer.read((char*)& tots, sizeof(float));
string filenm("venta");
filenm += cod;
filenm += ".txt";
prodListAux.read(filenm);
//convertir a string.
string codeSale(codigoVenta);
string codeClnt(codigoCliente);
string codeEmply(codigoEmpleado);
// asignar valores
auxVenta.setCodigoVenta(codeSale);
auxVenta.setCodigoCliente(codeClnt);
auxVenta.setCodigoEmpleado(codeEmply);
auxVenta.setTotal(tots);
auxVenta.setListaProductosVenta(prodListAux);
//agregar a la lista
insertData(getLastPos(), auxVenta);
lastCode = cod;
}
leer.close();
}
void ListaVentas::write()
{
char codigoVenta[15];
char codigoCliente[15];
char codigoEmpleado[15];
float tots;
int tamno(15);
string filename("listaVentas.txt");
ofstream escribir(filename, ios::trunc);
NodoVenta* aux(header->getNext());
while (aux != header)
{
//dimensiones
int l1 = size(aux->getData().getCodigoVenta());
int l2 = size(aux->getData().getCodigoCliente());
int l3 = size(aux->getData().getCodigoEmpleado());
//convertir a chars
strncpy(codigoVenta, aux->getData().getCodigoVenta().c_str(), l1);
strncpy(codigoCliente, aux->getData().getCodigoCliente().c_str(), l2);
strncpy(codigoEmpleado, aux->getData().getCodigoEmpleado().c_str(), l3);
tots = aux->getData().getTotal();
// agregar '\0' al final
codigoVenta[l1] = '\0';
codigoCliente[l2] = '\0';
codigoEmpleado[l3] = '\0';
//escribir en el disco
escribir.write((char*)& codigoVenta, tamno);
escribir.write((char*)& codigoCliente, tamno);
escribir.write((char*)& codigoEmpleado, tamno);
escribir.write((char*)& tots, sizeof(float));
//escribir su lista de productos
string filenm("venta");
filenm += aux->getData().getCodigoVenta();
filenm += ".txt";
aux->getData().writeListaProductos(filenm);
aux = aux->getNext();
}
escribir.close();
}