Repository navigation
Expand file tree
/
Copy pathCustomer.cpp
More file actions
360 lines (329 loc) · 11.2 KB
/
Copy pathCustomer.cpp
File metadata and controls
360 lines (329 loc) · 11.2 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
//Да се креира класа Customer за опишување на купувачи на една книжара. За секој купувач се чуваат податоци за:
//
//името (низа од 50 знаци),
//електронска адреса (низа од 50 знаци),
//вид на купувач (стандардни, лојални или вип),
//основен попуст (цел број изразен во проценти),
// дополнителен попуст (цел број изразен во проценти) и
// број на купени производи. (5 поени)
//Сите лојални купувачи со покажување на клуб картичка имаат право на основниот попуст при купување од книжарата. Основниот попуст е ист за сите лојални купувачи и изнесува 10%. Оваа вредност може да се смени со одлука на раководството на книжарата. Дополнителниот попуст е фиксен и може да го користат само вип купувачите и изнесува 20%. Стандардните купувачи немаат право на попуст (5 поени).
//
//За оваа класа да се имплементира оператор << за печатење на купувач во формат:
//
//[ime_na_kupuvac]
//[email_na_kupuvac]
//[broj_na_proizvodi]
//[vid_na_kupuvac] [popust_sto_moze_da_go_koristi]
//каде попустот е 0% ако купувачот е стандарден, вредноста на основниот попуст, ако е лојален или збир од основниот и дополнителниот попуст, ако купувачот е вип. (5 поени)
//
//Да се креира класа за онлајн книжара FINKI-bookstore во која се чува низа од регистрирани купувачи (динамички алоцирана низа) и број на купувачи. (5 поени) За класата да се обезбедат:
//
//operator+= (10 поени) за додавање купувач во листата од купувачи, но само ако веќе не е дел од неа (ако во листата нема купувач со иста електронска адреса). Ако во листата постои корисник со иста електронска адреса, треба да се генерира исклучок UserExistsException. Потребно е да се обезбеди справување со исклучокот во функцијата main на означеното место. Во ваква ситуација се печати порака "The user already exists in the list" (5 поени).
//Функција update со која сите стандардни купувачи со купени повеќе од 5 производи стануваат лојални, а сите лојални купувачи со над 10 купени производи, стануваат вип (5 поени).
//Оператор << за печатење на информациите за сите регистрирани купувачи (5 поени).
//Да се обезбедат сите дополнителни методи потребни за правилно функционирање на програмата. (10 поени)
//
//For example:
//
//Input Result
//6
//5
//Albert Einstein
//einstein@finki.mk
//1
//2
//Isaac Newton
//newton@finki.mk
//2
//10
//Galileo Galilei
//galilei@finki.mk
//1
//12
//Stephen Hawking
//hawking@finki.mk
//0
//17
//Nikola Tesla
//tesla@finki.mk
//0
//7
//===== Test Case - update method ======
//
//Update:
//Albert Einstein
//einstein@finki.mk
//2
//loyal 10
//Isaac Newton
//newton@finki.mk
//10
//vip 30
//Galileo Galilei
//galilei@finki.mk
//12
//vip 30
//Stephen Hawking
//hawking@finki.mk
//17
//loyal 10
//Nikola Tesla
//tesla@finki.mk
//7
//loyal 10
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstring>
#define MAX 51
using namespace std;
class UserExistsException {
public:
void print() {
cout << "The user already exists in the list!" << endl;
}
};
enum typeC {
standard, loyal, vip
};
class Customer {
protected:
char name[MAX];
char email[MAX];
typeC type;
static int discount1;
static const int discount2;
int numProducts;
public:
Customer(char *name = (char *)"w", char *email = (char *)"w", typeC type = standard, int numProducts = 0) {
strcpy(this->name, name);
strcpy(this->email, email);
this->type = type;
this->numProducts = numProducts;
}
static void setDiscount1(int discount) {
Customer::discount1 = discount;
}
const char *getEmail() const {
return email;
}
void promote() {
if (type == standard && numProducts > 5) {
type = loyal;
}
else if (type == loyal && numProducts > 10) {
type = vip;
}
}
friend ostream &operator<<(ostream &os, Customer &c) {
os << c.name << endl;
os << c.email << endl;
os << c.numProducts << endl;
if (c.type == standard) {
os << "standard 0" << endl;
} else if (c.type == loyal) {
os << "loyal " << Customer::discount1 << endl;
} else if (c.type == vip) {
os << "vip " << (Customer::discount1 + Customer::discount2) << endl;
}
return os;
}
};
int Customer::discount1 = 10;
const int Customer::discount2 = 20;
class FINKI_bookstore {
private:
Customer *customers;
int n;
void copyFINKI_bookstore(const FINKI_bookstore &f) {
this->n = f.n;
customers = new Customer[n];
for (int i = 0; i < n; ++i) {
customers[i] = f.customers[i];
}
}
public:
FINKI_bookstore(Customer *customers = nullptr, int n = 0) {
this->n = n;
this->customers = new Customer[n];
for (int i = 0; i < n; ++i) {
this->customers[i] = customers[i];
}
}
void setCustomers(Customer *customers, int n) {
delete[] this->customers;
this->n = n;
this->customers = new Customer[n];
for (int i = 0; i < n; ++i) {
this->customers[i] = customers[i];
}
}
FINKI_bookstore(const FINKI_bookstore &f) {
copyFINKI_bookstore(f);
}
FINKI_bookstore &operator=(const FINKI_bookstore &other) {
if (this != &other) {
delete[] customers;
copyFINKI_bookstore(other);
}
return *this;
}
~FINKI_bookstore() {
delete[] customers;
}
FINKI_bookstore &operator+=(const Customer &c) {
for (int i = 0; i < n; ++i) {
if (strcmp(customers[i].getEmail(), c.getEmail()) == 0) {
throw UserExistsException();
}
}
Customer *tmp = new Customer[n + 1];
for (int i = 0; i < n; ++i) {
tmp[i] = customers[i];
}
tmp[n] = c;
delete[] customers;
customers = tmp;
n++;
return *this;
}
void update() {
for (int i = 0; i < n; ++i) {
customers[i].promote();
}
}
friend ostream &operator<<(ostream &os, FINKI_bookstore &f) {
for (int i = 0; i < f.n; ++i) {
os << f.customers[i];
}
return os;
}
};
int main() {
int testCase;
cin >> testCase;
char name[MAX];
char email[MAX];
int tC;
int discount;
int numProducts;
if (testCase == 1) {
cout << "===== Test Case - Customer Class ======" << endl;
cin.get();
cin.getline(name, MAX);
cin.getline(email, MAX);
cin >> tC;
cin >> numProducts;
cout << "===== CONSTRUCTOR ======" << endl;
Customer c(name, email, (typeC) tC, numProducts);
cout << c;
}
if (testCase == 2) {
cout << "===== Test Case - Static Members ======" << endl;
cin.get();
cin.getline(name, MAX);
cin.getline(email, MAX);
cin >> tC;
cin >> numProducts;
cout << "===== CONSTRUCTOR ======" << endl;
Customer c(name, email, (typeC) tC, numProducts);
cout << c;
c.setDiscount1(5);
cout << c;
}
if (testCase == 3) {
cout << "===== Test Case - FINKI-bookstore ======" << endl;
FINKI_bookstore fc;
int n;
cin >> n;
Customer customers[MAX];
for (int i = 0; i < n; ++i) {
cin.get();
cin.getline(name, MAX);
cin.getline(email, MAX);
cin >> tC;
cin >> numProducts;
Customer c(name, email, (typeC) tC, numProducts);
customers[i] = c;
}
fc.setCustomers(customers, n);
cout << fc << endl;
}
if (testCase == 4) {
cout << "===== Test Case - operator+= ======" << endl;
FINKI_bookstore fc;
int n;
cin >> n;
Customer customers[MAX];
for (int i = 0; i < n; ++i) {
cin.get();
cin.getline(name, MAX);
cin.getline(email, MAX);
cin >> tC;
cin >> numProducts;
Customer c(name, email, (typeC) tC, numProducts);
customers[i] = c;
}
fc.setCustomers(customers, n);
cout << "OPERATOR +=" << endl;
cin.get();
cin.getline(name, MAX);
cin.getline(email, MAX);
cin >> tC;
cin >> numProducts;
Customer c(name, email, (typeC) tC, numProducts);
fc += c;
cout << fc;
}
if (testCase == 5) {
cout << "===== Test Case - operator+= (exception) ======" << endl;
FINKI_bookstore fc;
int n;
cin >> n;
Customer customers[MAX];
for (int i = 0; i < n; ++i) {
cin.get();
cin.getline(name, MAX);
cin.getline(email, MAX);
cin >> tC;
cin >> numProducts;
Customer c(name, email, (typeC) tC, numProducts);
customers[i] = c;
}
fc.setCustomers(customers, n);
cout << "OPERATOR +=" << endl;
cin.get();
cin.getline(name, MAX);
cin.getline(email, MAX);
cin >> tC;
cin >> numProducts;
Customer c(name, email, (typeC) tC, numProducts);
try {
fc += c;
} catch (UserExistsException &e) {
e.print();
}
cout << fc;
}
if (testCase == 6) {
cout << "===== Test Case - update method ======" << endl << endl;
FINKI_bookstore fc;
int n;
cin >> n;
Customer customers[MAX];
for (int i = 0; i < n; ++i) {
cin.get();
cin.getline(name, MAX);
cin.getline(email, MAX);
cin >> tC;
cin >> numProducts;
Customer c(name, email, (typeC) tC, numProducts);
customers[i] = c;
}
fc.setCustomers(customers, n);
cout << "Update:" << endl;
fc.update();
cout << fc;
}
return 0;
}