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
|
#include "OnlineLibrary.h"
#include "LibraryBanking.h"
#include "Transaction.h"
#include <iostream>
// Игнорираме всички букви до края на реда вкл.
void ignoreLine() {
while (std::cin.get() != '\n');
}
// Тази функция ни позволява да приемем от входа низ с неограничена дължина
String getlineEndless() {
String bufferString;
for (char buffer = std::cin.get(); buffer != '\n'; buffer = std::cin.get()) {
bufferString.AppendChar(buffer);
}
return bufferString;
}
void RegisterConsole(OnlineLibrary& lib, LibraryBanking& libBank) {
char c;
std::cin >> c;
switch (c) {
case 'u': {
User newUser;
std::cout << "Username: ";
ignoreLine();
newUser.SetEmail(getlineEndless());
std::cout << "Password: ";
newUser.SetPassword(getlineEndless());
std::cout << "Bank card: ";
unsigned bankCard;
std::cin >> bankCard;
newUser.SetBankCard(bankCard);
lib.RegisterUser(newUser);
}
break;
case 'l': {
std::cout << "Librarian Number: ";
unsigned number;
std::cin >> number;
Librarian newLib(number);
std::cout << "Password: ";
ignoreLine();
newLib.SetPassword(getlineEndless());
lib.RegisterLibrarian(newLib);
}
break;
case 'c': {
std::cout << "Clerk Number: ";
unsigned number;
std::cin >> number;
BankClerk newClerk(number);
std::cout << "Static password: ";
ignoreLine();
newClerk.SetPassword(getlineEndless());
libBank.RegisterClerk(newClerk);
}
break;
}
}
void UserConsole(OnlineLibrary& lib, LibraryBanking& libBank) {
std::cout << "Username: ";
ignoreLine();
String bufferUsername = getlineEndless();
std::cout << "Password: ";
String bufferPassword = getlineEndless();
User* loggedIn = lib.LoginUser(bufferUsername, bufferPassword);
if (loggedIn == nullptr) {
std::cout << "Invalid username or password!" << std::endl;
return;
}
while (true) {
char c;
std::cin >> c;
switch (c) {
case 'e':
return;
case 'l':
loggedIn->ListLibrary();
break;
case 'r': {
std::cout << "User library index: ";
unsigned i;
std::cin >> i;
const Book* book = loggedIn->GetBook(i);
if (book == nullptr) {
std::cout << "Invalid index!" << std::endl;
}
else {
std::cout << book->GetText().GetStr() << std::endl;
}
}
break;
case 's':
lib.ListBooks();
break;
case 'p': {
std::cout << "Book index: ";
unsigned i;
std::cin >> i;
const Book* book = lib.GetBook(i);
if (book == nullptr) {
std::cout << "Invalid index!" << std::endl;
}
else if (loggedIn->GetBankCard() == 0 && book->GetPrice() != 0.0) {
std::cout << "Cannot purchase book with invalid card!" << std::endl;
}
else {
lib.PurchaseBook(*book, *loggedIn, libBank);
loggedIn->AddBookToLibrary(book);
}
}
break;
case 't':
lib.ListTransactions(*loggedIn);
break;
case 'x': {
std::cout << "Output file: ";
ignoreLine();
String fileName = getlineEndless();
lib.ExportTransactions(*loggedIn, fileName);
}
break;
case 'P': {
std::cout << "New password: ";
ignoreLine();
String newPassword = getlineEndless();
loggedIn->SetPassword(newPassword);
}
break;
case 'C': {
std::cout << "New bank card: ";
unsigned newBankCard;
std::cin >> newBankCard;
loggedIn->SetBankCard(newBankCard);
}
break;
}
}
}
void LibrarianConsole(OnlineLibrary& lib) {
std::cout << "Librarian number: ";
unsigned bufferLibrarianNo;
std::cin >> bufferLibrarianNo;
std::cout << "Password: ";
ignoreLine();
String bufferPassword = getlineEndless();
Librarian* loggedIn = lib.LoginLibrarian(bufferLibrarianNo, bufferPassword);
if (loggedIn == nullptr) {
std::cout << "Invalid librarian number or password!" << std::endl;
return;
}
while (true) {
char c;
std::cin >> c;
switch (c) {
case 'e':
return;
// Не се изисква по условие, но е удобно
case 's':
lib.ListBooks();
break;
case 'a': {
Book newBook;
newBook.SetAvailableForPurchase(true);
std::cout << "Title: ";
ignoreLine();
newBook.SetTitle(getlineEndless());
std::cout << "Author: ";
newBook.SetAuthor(getlineEndless());
std::cout << "Year: ";
short year;
std::cin >> year;
newBook.SetYear(year);
std::cout << "Price: ";
double price;
std::cin >> price;
newBook.SetPrice(price);
ignoreLine();
String text;
char prevprev = std::cin.get(), prev = std::cin.get(), curr = std::cin.get();
while (prevprev != '\n' || prev != '.' || curr != '\n') {
text.AppendChar(prevprev);
std::cout << '[' << (int)prevprev << ',' << (int)prev << ',' << (int)curr << ']' << std::endl;
prevprev = prev;
prev = curr;
curr = std::cin.get();
}
text.AppendChar(prevprev);
newBook.SetText(text);
lib.AddBook(newBook);
}
break;
case 'i': {
Book newBook;
newBook.SetAvailableForPurchase(true);
std::cout << "File name: ";
ignoreLine();
if (newBook.Import(getlineEndless())) {
double price;
std::cout << "Price: ";
std::cin >> price;
newBook.SetPrice(price);
lib.AddBook(newBook);
}
}
break;
case 'x': {
std::cout << "Book index: ";
unsigned i;
std::cin >> i;
Book* book = lib.GetBook(i);
if (book == nullptr) {
std::cout << "Invalid index!" << std::endl;
}
else {
std::cout << "File name: ";
ignoreLine();
book->Export(getlineEndless());
}
}
break;
case 'E': {
std::cout << "Book index: ";
unsigned i;
std::cin >> i;
Book* book = lib.GetBook(i);
if (book == nullptr) {
std::cout << "Invalid index!" << std::endl;
}
else {
ignoreLine();
std::cout << "Title: ";
book->SetTitle(getlineEndless());
std::cout << "Author: ";
book->SetAuthor(getlineEndless());
std::cout << "Price: ";
double price;
std::cin >> price;
book->SetPrice(price);
}
}
break;
case 'A': {
std::cout << "Book index: ";
unsigned i;
std::cin >> i;
Book* book = lib.GetBook(i);
if (book == nullptr) {
std::cout << "Invalid index!" << std::endl;
}
else {
book->SetAvailableForPurchase(! book->GetAvailableForPurchase());
if (book->GetAvailableForPurchase()) {
std::cout << "Book is now available!" << std::endl;
}
else {
std::cout << "Book is now not available!" << std::endl;
}
}
}
break;
}
}
}
void ClerkConsole(LibraryBanking& libBank) {
std::cout << "Clerk number: ";
unsigned bufferClerkNo;
std::cin >> bufferClerkNo;
std::cout << "Dynamic password: ";
unsigned dynamicPassword;
std::cin >> dynamicPassword;
ignoreLine();
std::cout << "Static password: ";
String bufferPassword = getlineEndless();
BankClerk* loggedIn = libBank.LoginClerk(bufferClerkNo, dynamicPassword, bufferPassword);
if (loggedIn == nullptr) {
std::cout << "Invalid clerk number or password!" << std::endl;
return;
}
while (true) {
char c;
std::cin >> c;
switch (c) {
case 'e':
return;
case 'l':
libBank.ListPendingTransactions();
break;
case 'A':
case 'R': {
std::cout << "Transaction index: ";
unsigned i;
std::cin >> i;
Transaction* transaction = libBank.GetTransaction(i);
if (transaction == nullptr) {
std::cout << "Invalid index!" << std::endl;
}
else if (transaction->GetStatus() != STATUS_PENDING) {
std::cout << "Transaction not pending!" << std::endl;
}
else {
transaction->SetStatus(loggedIn->GetClerkNumber(), c == 'A' ? STATUS_ACCEPTED : STATUS_REJECTED);
}
}
break;
case 'x': {
std::cout << "File name: ";
ignoreLine();
libBank.ExportNonPendingTransactions(getlineEndless());
}
break;
}
}
}
// Поискано ни е да имплементираме система,
// това да направим конзолен интерфейс се подразбира
int main() {
OnlineLibrary lib;
LibraryBanking libBank;
while (true) {
char c;
std::cin >> c;
switch (c) {
case 'r': RegisterConsole(lib, libBank); break;
case 'u': UserConsole(lib, libBank); break;
case 'l': LibrarianConsole(lib); break;
case 'c': ClerkConsole(libBank); break;
case 'e': return 0;
default: std::cout << "Invalid!" << std::endl;
}
}
return 0;
}
|