-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
273 lines (256 loc) · 6.39 KB
/
main.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
#include <iostream>
#include <stdlib.h>
#include <fstream>
#include <string.h>
#include <windows.h>
using namespace std;
int menu();
bool check(string Product);
void add(ofstream &data);
void view(ifstream &data);
void search(ifstream &data);
void sell(ifstream &data);
void erase(ifstream &data);
int main()
{
ofstream inventoryWrite;
ifstream inventoryRead;
int option;
do
{
system("cls");
option = menu();
switch (option)
{
case 1:
sell(inventoryRead);
break;
case 2:
search(inventoryRead);
break;
case 3:
view(inventoryRead);
break;
case 4:
add(inventoryWrite);
break;
case 5:
erase(inventoryRead);
break;
}
} while (option != 6);
return 0;
}
int menu()
{
int option;
system("cls");
cout << "---> CONTROL DE INVENTARIO SUPERSTORE <---\n\n******** Menu *******\n1.Producto vendido\n2.Buscar producto\n3.Ver inventario\n4.Agregar a inventario\n5.Eliminar un producto\n6.Salir\n\nOpcion: "; // menu
cin >> option;
if (option >= 1 && option <= 6)
{
return option;
}
else
{
cout << "Opcion no valida" << endl;
}
system("pause");
menu();
}
void add(ofstream &data)
{
system("cls");
string product;
int price, quantity;
data.open("./source/inventory.txt", ios::out | ios::app);
cout << "Producto :";
cin >> product;
cout << "Precio :";
cin >> price;
cout << "Cantidad :";
cin >> quantity;
if (check(product))
{
data << product << " " << price << " " << quantity << endl;
}
data.close();
}
bool check(string Product)
{
ifstream read("./source/inventory.txt", ios::in);
string product;
int price, quantity;
read >> product;
while (!read.eof())
{
read >> price;
read >> quantity;
if (Product == product)
{
read.close();
cout << "Este producto ya existe (No se agregara)" << endl;
system("pause");
return false;
}
read >> product;
}
read.close();
return true;
}
void view(ifstream &data)
{
system("cls");
string product;
int price, quantity;
data.open("./source/inventory.txt", ios::in);
if (data.is_open())
{
cout << "---> INVENTARIO <---" << endl;
data >> product;
while (!data.eof())
{
data >> price;
data >> quantity;
cout << "Producto: " << product << endl;
cout << "Precio: $" << price << endl;
cout << "Cantidad: " << quantity << endl;
cout << "---------------------------" << endl;
data >> product;
}
data.close();
}
else
{
cout << "El archivo del inventario no existe" << endl;
}
system("pause");
}
void search(ifstream &data)
{
system("cls");
data.open("./source/inventory.txt", ios::in);
if (data.is_open())
{
string product, auxProduct;
int price, quantity;
bool found = false;
cout << "Ingrese el nombre del producto: ";
cin >> auxProduct;
data >> product;
while (!data.eof() && !found)
{
data >> price;
data >> quantity;
if (product == auxProduct)
{
cout << "Producto: " << product << endl;
cout << "Precio: $" << price << endl;
cout << "Cantidad: " << quantity << endl;
cout << "---------------------------" << endl;
found = true;
}
data >> product;
}
data.close();
if (!found)
{
cout << "Producto no encontrado" << endl;
}
}
else
{
cout << "El archivo del inventario no existe" << endl;
}
system("pause");
}
void sell(ifstream &data)
{
system("cls");
data.open("./source/inventory.txt", ios::in);
ofstream aux("./source/auxiliary.txt", ios::out);
if (data.is_open())
{
string product, auxProduct;
int price, quantity, auxQuantity;
bool found = false;
cout << "Producto a vender: ";
cin >> auxProduct;
data >> product;
while (!data.eof())
{
data >> price;
data >> quantity;
if (product == auxProduct)
{
cout << "Cantidad de productos que va a vender: ";
cin >> auxQuantity;
auxQuantity = quantity - auxQuantity;
aux << product << " " << price << " " << auxQuantity << "\n";
found = true;
}
else
{
aux << product << " " << price << " " << quantity << "\n";
}
data >> product;
}
data.close();
aux.close();
if (!found)
{
cout << "Producto no encontrado" << endl;
Sleep(2000);
}
}
else
{
cout << "No se encuenta el archivo" << endl;
}
remove("./source/inventory.txt");
rename("./source/auxiliary.txt", "./source/inventory.txt");
}
void erase(ifstream &data)
{
system("cls");
data.open("./source/inventory.txt", ios::in);
ofstream aux("./source/auxiliary.txt", ios::out);
if (data.is_open())
{
string product, auxProduct;
int price, quantity, auxQuantity;
bool found = false;
cout << "Producto a eliminar: ";
cin >> auxProduct;
data >> product;
while (!data.eof())
{
data >> price;
data >> quantity;
if (product == auxProduct)
{
cout << product << " Se ha elimidado del inventario correctamente \n";
Sleep(2000);
found = true;
}
else
{
aux << product << " " << price << " " << quantity << "\n";
}
data >> product;
}
if (!found)
{
cout << "Producto no encontrado" << endl;
Sleep(2000);
}
data.close();
aux.close();
}
else
{
cout << "No se encuenta el archivo" << endl;
}
remove("./source/inventory.txt");
rename("./source/auxiliary.txt", "./source/inventory.txt");
}