-
Notifications
You must be signed in to change notification settings - Fork 1
/
Chang.cpp
634 lines (521 loc) · 11.5 KB
/
Chang.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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#include <iostream>
using namespace std;
class FBTNode{
private:
int bookId;
int fbtValue;
public:
FBTNode *next;
FBTNode(){
bookId = 0;
fbtValue = 0;
next = NULL;
}
FBTNode(int book, int value){
bookId = book;
fbtValue = value;
next = NULL;
}
void incValue(){
fbtValue++;
}
void setBookId(int input){
bookId = input;
}
int getBookId(){
return bookId;
}
int getValue(){
return fbtValue;
}
};
class BookNode{
protected:
char title[1024];
int id;
float price;
FBTNode *fbt;
public:
BookNode *next;
BookNode(){
id = 0;
price = 0;
fbt = NULL;
}
BookNode(char c[1024], int i, float f){
strcpy_s(title,c);
id = i;
price = f;
fbt = NULL;
}
virtual void display(){
cout << "The title of the book is: " << title << endl;
cout << "The ID of the book is: " << id << endl;
cout << "The price of the book is: " << price << "\n" << endl;
}
void setTitle(char *c){
strcpy_s(title,c);
}
void setId(int i){
id = i;
}
void setPrice(float f){
price = f;
}
void setFbt(FBTNode *p){
if(fbt != NULL){
FBTNode *head,*tail;
head = fbt;
tail = fbt->next;
if(p->getValue() > head->getValue()){
p->next = head;
fbt = p;
return;
}
while(tail != NULL){
if(p->getValue() > tail->getValue()){
p->next = tail;
head->next = p;
return;
}
head = head->next;
tail = tail->next;
}
head->next = p;
}
else{
p->next = NULL;
fbt = p;
}
}
FBTNode* getFbt(){
return fbt;
}
int getId(){
return id;
}
char *getTitle(){
return title;
}
float getPrice(){
return price;
}
void quit(FBTNode *node){
FBTNode *p,*k;
if(node != NULL){
p = node;
k = node->next;
while(p != NULL){
delete p;
p = k;
if(k != NULL)
k = k->next;
}
}
}
~BookNode(){
quit(fbt);
}
};BookNode *Catalog=NULL,*Cart=NULL;
class FictionNode : public BookNode{
private:
char author[1024];
public:
FictionNode(char title[1024], int id, float price, char c[1024]):BookNode(title, id, price){
strcpy_s(author,c);
}
void display(){
cout << "The title of the fiction book is: " << title << endl;
cout << "The ID of the fiction book is: " << id << endl;
cout << "The price of the fiction book is: " << price << endl;
cout << "The author of the fiction book is: " << author << "\n" << endl;
}
};
class MagnazineNode : public BookNode{
private:
int issue;
public:
MagnazineNode(char title[1024], int id, float price, int i):BookNode(title, id, price){
issue = i;
}
void display(){
cout << "The title of the magnazine is: " << title << endl;
cout << "The ID of the magnazine is: " << id << endl;
cout << "The price of the magnazine is: " << price << endl;
cout << "The issue of the magnazine is: " << issue << "\n" << endl;
}
};
class TextbookNode : public BookNode{
private:
char author[1024];
int edition;
char isbn[1024];
public:
TextbookNode(char title[1024], int id, float price, char tauthor[1024], int i, char tisbn[1024]):BookNode(title, id, price){
strcpy_s(author,tauthor);
edition = i;
strcpy_s(isbn,tisbn);
}
void display(){
cout << "The title of the textbook is: " << title << endl;
cout << "The ID of the textbook is: " << id << endl;
cout << "The price of the textbook is: " << price << endl;
cout << "The author of the textbook is: " << author << endl;
cout << "The edition of the textbook is: " << edition << endl;
cout << "The ISBN of the textbook is: " << isbn << "\n" << endl;
}
};
// forward declaration //
void menu();
void loadCatalog();
void branching(char option);
void displayCatalog();
void displayCart();
void checkOut();
void addToCart();
void loadFbt();
void promptFbt(FBTNode* List);
void updateFbt();
void quit(BookNode *option);
BookNode *searchId(int i);
BookNode *searchTitle(char *c);
const float afterTax = 1.09;
int main(){
char ch;
cout << "\n\nWelcome to CSE240: Bookstore\n";
loadCatalog();
loadFbt();
do {
menu();
ch = tolower(getchar()); // read a char, convert to lower case
cin.ignore();
branching(ch);
} while (ch != 'q');
return 0;
}
void menu()
{
cout << "\nMenu Options\n";
cout << "------------------------------------------------------\n";
cout << "a: Add a book to cart\n";
cout << "c: Check out\n";
cout << "m: Display Cart\n";
cout << "d: Display Catalog\n";
cout << "q: Quit\n";
cout << "\n\nPlease enter a choice (a,c,m,d or q) ---> ";
}
void branching(char option)
{
switch(option)
{
case 'a':
addToCart();
break;
case 'c':
checkOut();
break;
case 'm':
displayCart();
break;
case 'd':
displayCatalog();
break;
case 'q':
quit(Catalog);
if(Cart != NULL){
quit(Cart);
}
Catalog = NULL;
Cart = NULL;
exit(0);
break;
default:
cout << "\nError: Invalid Input. Please try again...";
break;
}
}
void loadCatalog()
{
FILE *fileName;
char title[1024];
int id,type;
float price;
fileName = fopen("iBookCatalog.txt","r");
if(fileName != NULL){
while(fread(&type, sizeof(int), 1, fileName) == 1){
fread(title,1024,1,fileName);
fread(&id, sizeof(int), 1, fileName);
fread(&price, sizeof(float), 1, fileName);
BookNode *node;
switch(type){
case 0:
char fictionAuthor[1024];
fread(fictionAuthor, 1024, 1, fileName);
node = new FictionNode(title, id, price, fictionAuthor);
node->next = Catalog;
Catalog = node;
break;
case 1:
int issue;
fread(&issue, sizeof(int), 1, fileName);
node = new MagnazineNode(title, id, price, issue);
node->next = Catalog;
Catalog = node;
break;
case 2:
char textbookAuthor[1024];
int edition;
char isbn[1024];
fread(textbookAuthor, 1024, 1, fileName);
fread(&edition, sizeof(int), 1, fileName);
fread(isbn, 1024, 1, fileName);
node = new TextbookNode(title, id, price, textbookAuthor,edition,isbn);
node->next = Catalog;
Catalog = node;
break;
case 3:
node = new BookNode();
node->setTitle(title);
node->setId(id);
node->setPrice(price);
node->next = Catalog;
Catalog = node;
break;
}
}
}
fclose(fileName);
}
void displayCatalog(){
BookNode *node;
node = Catalog;
while(node != NULL){
node->display();
node = node->next;
}
}
void displayCart(){
BookNode *node;
node = Cart;
while(node != NULL){
node->display();
node = node->next;
}
}
void checkOut(){
cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
cout.precision(2);
BookNode *node;
node = Cart;
char input;
int count = 1;
cout << "So far you have the following items in your cart:\n" << endl;
while(node != NULL){
cout << count << ") " << node->getTitle() << " X 1" << endl;
node = node->next;
count++;
}
cout << "\nAre you sure you have all that you need?\nPlease enter \"y\" or \"n\"" << endl;
cin >> input;
cin.ignore();
if(input == 'y'){
FBTNode *fbt;
BookNode *head,*phead;
bool flag = false;
int id;
head = Cart;
phead = Cart;
node = Cart;
float total=0;
while(node != NULL){
total += node->getPrice();
node = node->next;
}
total = total * afterTax;
cout << "Your grand total after taxes is : $" << total << endl;
if(Cart->next != NULL && Cart != NULL){
while(head != NULL) {
phead = Cart;
while(phead != NULL){
fbt = searchId(head->getId())->getFbt();
if(phead != head){
id = phead->getId();
flag = false;
while(fbt != NULL){
if(id == fbt->getBookId()){
fbt->incValue();
flag = true;
}
fbt = fbt->next;
}
if(flag == false){
FBTNode *p = new FBTNode(id,1);
searchId(head->getId())->setFbt(p);
}
}
phead = phead->next;
}
head = head->next;
}
updateFbt();
}
quit(Catalog);
if(Cart != NULL){
quit(Cart);
}
Catalog = NULL;
Cart = NULL;
exit(0);
}
}
void addToCart(){
BookNode *node;
node = NULL;
char c;
cout << "Please choose \"i\" if you are going to enter by ID and \"t\" by title" << endl;
c = tolower(getchar());
cin.ignore();
BookNode *p;
switch(c){
case 'i':
int input;
cout << "Please enter the ID of the book you wish to add to your cart" << endl;
cin >> input;
cin.ignore();
if(input <= 17 && input >= 0){
node = searchId(input);
p = new BookNode(node->getTitle(),node->getId(),node->getPrice());
p->next = Cart;
Cart = p;
promptFbt(node->getFbt());
break;
}
else{
cout << "Invalid input" << endl;
break;
}
case 't':
char title[1024];
cout << "Please enter the title of the book you wish to add to your cart" << endl;
cin.getline(title,1024,'\n');
node = searchTitle(title);
if(node != NULL){
p = new BookNode(node->getTitle(),node->getId(),node->getPrice());
p->next = Cart;
Cart = p;
promptFbt(node->getFbt());
break;
}
else{
cout << "Invalid input" << endl;
break;
}
}
}
void loadFbt(){
FILE *fileName;
int parent, book, value;
BookNode *node;
fileName = fopen("FBT.txt","r");
if(fileName != NULL){
FBTNode *fbtNode;
while(fread(&parent, sizeof(int), 1, fileName) == 1){
fread(&book, sizeof(int), 1, fileName);
fread(&value, sizeof(int), 1, fileName);
node = searchId(parent);
fbtNode = new FBTNode(book,value);
node->setFbt(fbtNode);
}
}
fclose(fileName);
}
BookNode *searchId(int i){
BookNode *node;
node = Catalog;
while(node != NULL){
if(node->getId() == i){
return node;
}
node = node->next;
}
return NULL;
}
BookNode *searchTitle(char *c){
BookNode *node;
node = Catalog;
while(node != NULL){
if(stricmp(node->getTitle(),c) == 0){
return node;
}
node = node-> next;
}
return NULL;
}
void promptFbt(FBTNode* List){
int temp;
BookNode *node;
cout << "You have successfully added \"" << Cart->getTitle() << "\" to your cart\n" << endl;
temp = Cart->getId();
node = searchId(temp);
if (node->getFbt() != NULL)
{
FBTNode *fbt;
BookNode *book;
fbt = node->getFbt();
int i = 1, id;
char input;
cout << "Please consider the following books most frequently bought with \"" << Cart->getTitle() << "\":" << endl;
while(fbt != NULL && i<4){
id = fbt->getBookId();
book = searchId(id);
cout << i << ") " << book->getTitle() << "\t\tBook ID: " << book->getId() << endl;
fbt= fbt->next;
i++;
}
cout << "\nWould you like to buy any of the books listed above?\t(y/n)" << endl;
input = tolower(getchar());
cin.ignore();
if(input == 'y')
addToCart();
}
else{
cout << "Sorry, no suggestions available..." << endl;
}
}
void updateFbt(){
FILE *fileName;
BookNode *book;
FBTNode *fbt;
book = Catalog;
fileName = fopen("FBT.txt", "w");
if(fileName != NULL){
int temp;
while(book != NULL){
if(book->getFbt() != NULL){
fbt = book->getFbt();
while(fbt != NULL){
temp = book->getId();
fwrite(&temp, sizeof(int), 1, fileName);
temp = fbt->getBookId();
fwrite(&temp, sizeof(int), 1, fileName);
temp = fbt->getValue();
fwrite(&temp, sizeof(int), 1, fileName);
fbt = fbt->next;
}
}
book = book->next;
}
}
fclose(fileName);
}
void quit(BookNode *option){
BookNode *p,*k;
p = option;
k = option->next;
while(p != NULL){
delete p;
p = k;
if(k != NULL)
k = k->next;
}
}