-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cpp
More file actions
397 lines (374 loc) · 13.3 KB
/
Program.cpp
File metadata and controls
397 lines (374 loc) · 13.3 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
//
// Created by micha on 18/06/2023.
//
#include "Program.h"
#include <fstream>
#include <sstream>
void Program::AddNewClient() {
string name, surname, address1;
char gender1;
cout << "Podaj dane klienta do dodania: \n";
cout << "Plec: (K/M)" << endl;
cin >> gender1;
while (gender1 != 'K' && gender1 != 'M') {
cout << "Niepoprawna plec" << endl;
cout << "Podaj plec: (K lub M)" << endl;
cin >> gender1;
}
cout << "Imie: " << endl;
cin >> name;
cout << "Nazwisko: " << endl;
cin >> surname;
cout << "Adres: " << endl;
cin >> address1;
Client client(name, surname, address1, gender1);
clients.push_back(client);
cout << "Klient zostal dodany do bazy danych" << endl;
}
void Program::ModifyClient() {
cout << "Podaj nr klienta do edycji: " << endl;
int clientNumber;
cin >> clientNumber;
while (clientNumber > clients.size() || clientNumber < 1) {
cout << "Nie ma takiego klienta" << endl;
cout << "Podaj nr klienta do edycji: " << endl;
cin >> clientNumber;
}
cout << "Co chcesz zmienic? (wpisz cyfre od 1 do 4)" << endl;
cout << "1 >>> Imie" << endl;
cout << "2 >>> Nazwisko" << endl;
cout << "3 >>> Adres" << endl;
cout << "4 >>> Plec" << endl;
cout << "5 >>> Powrot do menu" << endl;
int choice;
cin >> choice;
string newName, newSurname, newAddress;
char newGender;
while (choice < 1 || choice > 5) {
cout << "Niepoprawny wybor" << endl;
cout << "Co chcesz zmienic? (wpisz cyfre od 1 do 4)" << endl;
cout << "1 >>> Imie" << endl;
cout << "2 >>> Nazwisko" << endl;
cout << "3 >>> Adres" << endl;
cout << "4 >>> Plec" << endl;
cout << "5 >>> Powrot do menu" << endl;
cin >> choice;
}
if (choice != 5) {
switch (choice) {
case 1:
cout << "Podaj nowe imie: " << endl;
cin >> newName;
clients[clientNumber - 1].setFirstName(newName);
break;
case 2:
cout << "Podaj nowe nazwisko: " << endl;
cin >> newSurname;
clients[clientNumber - 1].setLastName(newSurname);
break;
case 3:
cout << "Podaj nowy adres: " << endl;
cin >> newAddress;
clients[clientNumber - 1].setAddress(newAddress);
break;
case 4:
cout << "Podaj nowa plec: (K/M)" << endl;
cin >> newGender;
if (newGender != 'K' && newGender != 'M') {
cout << "Podano niepoprawna plec" << endl;
break;
}
clients[clientNumber - 1].setGender(newGender);
break;
case 5:
cout << "Powrot do menu" << endl;
break;
default:
cout << "Niepoprawny wybor" << endl;
break;
}
}
}
void Program::SaveClientsToFile() {
ofstream file;
file.open("clients.txt");
if (file.good()) {
for (int i = 0; i < clients.size(); i++) {
file << i + 1 << ";";
file << clients[i].getFirstName() << ";";
file << clients[i].getLastName() << ";";
file << clients[i].getAddress() << ";";
file << clients[i].getGender() << endl;
}
cout << "Zapis do pliku tekstowego przebiegl pomyslnie" << endl;
} else {
cout << "Blad zapisu" << endl;
}
//saving to binary file
ofstream fileBinary;
fileBinary.open("clients.dat", ios::out | ios::binary);
if (fileBinary.good() && fileBinary.is_open()) {
for (int i = 0; i < clients.size(); i++) {
fileBinary.write(reinterpret_cast<char *>(&i), sizeof(int));
fileBinary.write(reinterpret_cast<char *>(&clients[i]), clients.size());
}
cout << "Zapis do pliku binarnego przebiegl pomyslnie" << endl;
} else {
cout << "Blad zapisu" << endl;
}
file.close();
}
void Program::AddNewOrder() {
string productName1, orderDate1, clientName1;
double vat1, price1, totalValue1;
int quantity1, clientNumber1;
PaymentMethod paymentMethod1;
cout << "Podaj nr klienta skladajacego zamowienie: " << endl;
cin >> clientNumber1;
while (clientNumber1 > clients.size() || clientNumber1 < 1) {
cout << "Nie ma takiego klienta" << endl;
cout << "Podaj nr klienta skladajacego zamowienie: " << endl;
cin >> clientNumber1;
}
cout << "Wybrano klienta: " << clients[clientNumber1 - 1].getFirstName() << " "
<< clients[clientNumber1 - 1].getLastName() << endl;
cout << "Podaj nazwe produktu: " << endl;
cin >> productName1;
cout << "Podaj ilosc: " << endl;
cin >> quantity1;
ifstream file1;
file1.open("C:products.txt");
string productNameF;
float quantityF, priceF, valueF;
valueF = 0;
for (int i = 0; i < 6; i++) {
file1 >> productNameF >> quantityF >> priceF;
if (productName1 == productNameF) {
price1 = priceF;
vat1 = quantity1 * (0.23 * priceF);
valueF += quantity1 * priceF + (quantity1 * (0.23 * priceF));
}
}
cout << "Podaj sposob zaplaty: " << endl;
cout << "1 >>> Karta" << endl;
cout << "2 >>> Gotowka" << endl;
cout << "3 >>> Przelew" << endl;
cout << "4 >>> Blik" << endl;
int choice;
cin >> choice;
while (choice != 1 && choice != 2 && choice != 3 && choice != 4) {
cout << "Niepoprawny wybor" << endl;
cout << "Podaj sposob zaplaty: " << endl;
cout << "1 >>> Karta" << endl;
cout << "2 >>> Gotowka" << endl;
cout << "3 >>> Przelew" << endl;
cout << "4 >>> Blik" << endl;
cin >> choice;
}
switch (choice) {
case 1:
paymentMethod1 = PaymentMethod::karta;
break;
case 2:
paymentMethod1 = PaymentMethod::gotowka;
break;
case 3:
paymentMethod1 = PaymentMethod::przelew;
break;
case 4:
paymentMethod1 = PaymentMethod::blik;
break;
default:
cout << "Niepoprawny wybor" << endl;
break;
}
cout << "Podaj date zamowienia: " << endl;
cin.ignore();
getline(std::cin, orderDate1);
clientName1 = clients[clientNumber1 - 1].getFirstName() + " " + clients[clientNumber1 - 1].getLastName();
Order order(productName1, quantity1, price1, vat1, orderDate1, valueF, paymentMethod1, clientName1);
orders.push_back(order);
cout << "Zamowienie zostalo dodane do bazy danych" << endl;
}
void Program::displayOrders() {
int i = 0;
for (const auto &order: orders) {
cout << "Nr zamowienia: " << i + 1 << endl;
cout << "Imie i nazwisko klienta: " << order.getClientName() << endl;
cout << "Data: " << order.getOrderDate() << endl;
cout << "Produkt: " << order.getProductName() << endl;
cout << "Cena: " << order.getPrice() << endl;
cout << "Ilosc: " << order.getQuantity() << endl;
cout << "Koszt VAT: " << order.getVatRate() << endl;
cout << "Calkowita wartosc zamowienia: " << order.getTotalValue() << endl;
cout << "Metoda platnosci: " << order.getPaymentMethod() << endl;
cout << "===============================================" << endl;
i++;
}
}
void Program::EditOrder() {
int index;
string productName, orderDate, clientName;
PaymentMethod paymentMethod;
int quantity;
double vatRate, price, totalValue;
cout << "Podaj nr zamowienia do edycji: " << endl;
cin >> index;
while (index - 1 > orders.size() || index - 1 < 0) {
cout << "Nie ma takiego zamowienia" << endl;
cout << "Podaj indeks zamowienia do edycji: " << endl;
cin >> index;
}
cout << "Wybrano zamowienie: \n" << endl;
cout << "Imie i nazwisko klienta: " << orders[index - 1].getClientName() << endl;
cout << "Data: " << orders[index - 1].getOrderDate() << endl;
cout << "Produkt: " << orders[index - 1].getProductName() << endl;
cout << "Cena: " << orders[index - 1].getPrice() << endl;
cout << "Ilosc: " << orders[index - 1].getQuantity() << endl;
cout << "Vat: " << orders[index - 1].getVatRate() << endl;
cout << "Calkowita wartosc zamowienia: " << orders[index - 1].getTotalValue() << endl;
cout << "Metoda platnosci: " << orders[index - 1].getPaymentMethod() << endl;
cout << "===============================================" << endl;
cout << "Edycja imienia: ";
cin.ignore();
getline(cin, clientName);
cout << "Edycja nazwy produktu: ";
getline(cin, productName);
cout << "Edycja ilosci ";
cin >> quantity;
cout << "Edycja daty: ";
cin.ignore();
getline(cin, orderDate);
cout << "Edycja metody platnosci: " << endl;
cout << "1 >>> Karta" << endl;
cout << "2 >>> Gotowka" << endl;
cout << "3 >>> Przelew" << endl;
cout << "4 >>> Blik" << endl;
int choice;
cin >> choice;
while (choice != 1 && choice != 2 && choice != 3 && choice != 4) {
cout << "Niepoprawny wybor" << endl;
cout << "Podaj sposob zaplaty: " << endl;
cout << "1 >>> Karta" << endl;
cout << "2 >>> Gotowka" << endl;
cout << "3 >>> Przelew" << endl;
cout << "4 >>> Blik" << endl;
cin >> choice;
}
switch (choice) {
case 1:
paymentMethod = PaymentMethod::karta;
break;
case 2:
paymentMethod = PaymentMethod::gotowka;
break;
case 3:
paymentMethod = PaymentMethod::przelew;
break;
case 4:
paymentMethod = PaymentMethod::blik;
break;
default:
cout << "Niepoprawny wybor" << endl;
break;
}
totalValue = price * quantity * (1 + vatRate);
orders[index - 1].setProductName(productName);
orders[index - 1].setQuantity(quantity);
orders[index - 1].setOrderDate(orderDate);
orders[index - 1].setTotalValue(totalValue);
orders[index - 1].setPaymentMethod(paymentMethod);
orders[index - 1].setClientName(clientName);
cout << "Zmiana danych zamowienia zakonczona sukcesem" << endl;
}
void Program::loadProductsFromFile() {
string fileName;
ifstream inputFile("products.txt");
if (inputFile.is_open()) {
string product;
while (getline(inputFile, product)) {
products.push_back(product);
system("cls");
}
cout << "Zaladowano produkty z pliku" << endl;
} else {
cout << "Blad podczas proby zaladowania produktow z pliku" << endl;
}
}
void Program::SaveOrdersToFile() {
ofstream outputFile("orders.txt");
if (outputFile.is_open() && !orders.empty()) {
for (const auto &order: orders) {
outputFile << " Nazwa produktu: " << order.getProductName() << ","
<< " Ilosc: " << order.getQuantity() << ","
<< " Koszt VAT: " << order.getVatRate() << ","
<< " Cena: " << order.getPrice() << ","
<< " Data: " << order.getOrderDate() << ","
<< " Calkowita wartosc zamowienia: " << order.getTotalValue() << ","
<< " Metoda platnosci: " << order.getPaymentMethod() << ","
<< " Imie i nazwisko klienta: " << order.getClientName() << endl;
}
cout << "Zapis do pliku zakonczyl sie powodzeniem" << endl;
} else {
cout << "Blad zapisu " << endl;
}
}
void Program::displayProducts() {
cout << "Wyswietlam dostepne produkty..." << endl;
for (const auto &product: products) {
cout << product << endl;
}
}
void Program::menu() {
int choice;
loadProductsFromFile();
choice = 1;
while (choice != 0) {
cout << "Witaj w programie do obslugi zamowien" << endl;
cout << "Wybierz opcje z menu: " << endl;
cout << "1 >>> Dodaj nowego klienta" << endl;
cout << "2 >>> Zmodyfikuj klienta" << endl;
cout << "3 >>> Dodaj nowe zamowienie" << endl;
cout << "4 >>> Edytuj zamowienie" << endl;
cout << "5 >>> Wyswietl produkty" << endl;
cout << "6 >>> Wyswietl zamowienia" << endl;
cout << "7 >>> Zapisz informacje o klientach do pliku" << endl;
cout << "8 >>> Zapisz informacje o zamowieniach do pliku" << endl;
cout << "0 >>> Exit" << endl;
cin >> choice;
switch (choice) {
case 1:
AddNewClient();
break;
case 2:
ModifyClient();
break;
case 3:
AddNewOrder();
break;
case 4:
EditOrder();
break;
case 5:
displayProducts();
break;
case 6:
displayOrders();
break;
case 7:
SaveClientsToFile();
break;
case 8:
SaveOrdersToFile();
break;
case 0:
cout << "Koniec dzialania programu" << endl;
cout << "Exiting..." << endl << endl;
break;
default:
cout << "Niepoprawny wybor" << endl;
break;
}
system("pause");
}
}