-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.cpp
1722 lines (1625 loc) · 52.8 KB
/
index.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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <fstream>
#include <algorithm>
#include <typeinfo>
#include <ctime>
#include <memory>
#include <stdexcept>
using namespace std;
int current_no_users = 0;
int current_no_books = 0;
int current_no_issued_books = 0;
class Issued_books
{
string issue_id;
string book_name;
string id;
string issue_date;
string return_date;
string generate_issued_id(string name, string idd)
{
time_t timestamp;
cout << "timestamp =" << timestamp << endl;
time(×tamp);
cout << "timestamp =" << timestamp << endl;
string pass = to_string(timestamp) + name[0] + to_string(idd[0]);
cout << "Generated issued id = " << pass << "|" << endl;
// pass[pass.size() - 1] = '\0';
return pass;
}
public:
Issued_books(string name, string idd)
{
time_t timestamp;
time(×tamp);
string temp = ctime(×tamp);
if (temp[temp.length() - 1] == '\n')
{
cout << "If called" << endl;
temp[temp.length() - 1] = '\0';
}
this->issue_id = this->generate_issued_id(name, idd);
this->book_name = name;
this->id = idd;
this->issue_date = temp;
this->return_date = "-";
cout << "this->issue_date + this->return_date = " << this->issue_date + this->return_date << endl;
}
Issued_books(string arr[])
{
this->issue_id = arr[0];
this->book_name = arr[1];
this->id = arr[2];
this->issue_date = arr[3];
this->return_date = arr[4];
}
string get_issued_book_data()
{
string s = "";
s = this->issue_id + "," + this->book_name + "," + this->id + "," + this->issue_date + "," + this->return_date;
return s;
}
string get_issued_id()
{
return this->issue_id;
}
vector<string> get_all_data_in_vector()
{
vector<string> temp;
temp.push_back(this->issue_id);
temp.push_back(this->book_name);
temp.push_back(this->id);
temp.push_back(this->issue_date);
temp.push_back(this->return_date);
return temp;
}
void book_submitted()
{
time_t timestamp;
time(×tamp);
string temp = ctime(×tamp);
if (temp[temp.length() - 1] == '\n')
{
temp[temp.length() - 1] = '\0';
}
this->return_date = temp;
}
};
class Users
{
private:
string name;
string phone;
string email;
int current_books_no;
int all_books_no;
vector<string> current_books_issue_id;
vector<string> all_books_issue_id;
public:
string id;
Users()
{
cout << "Users default constructor called" << endl;
}
Users(string name, string id, string email, string phone)
{
this->name = name;
this->phone = phone;
this->id = id;
this->email = email;
this->current_books_no = 0;
this->all_books_no = 0;
}
Users(string name, string id, string email, string phone, int current_books_no, int all_books_no, int current_books_issue_id, int all_books_issue_id)
{
this->name = name;
this->phone = phone;
this->id = id;
this->email = email;
this->current_books_no = current_books_no;
this->all_books_no = all_books_no;
}
Users(string arr[])
{
cout << "Users array contructor called" << endl;
this->name = arr[0];
this->id = arr[1];
this->phone = arr[2];
this->email = arr[3];
this->current_books_no = stoi(arr[4]);
this->all_books_no = stoi(arr[5]);
this->add_current_issued_books(arr[6]);
this->add_all_issued_books(arr[7]);
}
void add_new_users(string name, string id, string phone, string email)
{
cout << endl
<< "add_new_users called" << endl;
this->name = name;
this->id = id;
this->phone = phone;
this->email = email;
this->current_books_no = 0;
this->all_books_no = 0;
}
void add_current_issued_books(string arr)
{
if (arr == "NULL" || arr == "")
{
return;
}
cout << "add_current_issued_books called" << endl;
cout << "arr = " << arr << endl;
stringstream s(arr);
char del = '|';
string word;
while (!s.eof())
{
getline(s, word, del);
cout << "word = " << word << endl;
this->current_books_issue_id.push_back(word);
}
}
void add_all_issued_books(string arr)
{
if (arr == "NULL" || arr == "")
{
return;
}
cout << "add_all_issued_books called" << endl;
cout << "arr = " << arr << endl;
stringstream s(arr);
char del = '|';
string word;
while (!s.eof())
{
getline(s, word, del);
cout << "word = " << word << endl;
this->all_books_issue_id.push_back(word);
}
}
string get_current_issued_book_ids_in_string()
{
string s = "";
int i = 0;
while (i < current_books_issue_id.size())
{
s = s + current_books_issue_id[i];
i++;
if (i < current_books_issue_id.size())
{
s = s + "|";
}
}
return s;
}
string get_all_issued_book_ids_in_string()
{
string s = "";
int j = all_books_issue_id.size(), i = 0;
while (i < j)
{
s = s + all_books_issue_id[i];
i++;
if (i < j)
{
s = s + "|";
}
}
return s;
}
void display_user_data()
{
cout << endl;
// << "display_user_data called" << endl;
cout << "Name = " << this->name << endl
<< "Id = " << this->id << endl
<< "Phone = " << this->phone << endl
<< "Email = " << this->email << endl
<< "Current_books_no = " << this->current_books_no << endl
<< "All_books_no = " << this->all_books_no << endl
<< "Current_books_issue_id = " << this->get_current_issued_book_ids_in_string() << endl
<< "All_books_issue_id = " << this->get_all_issued_book_ids_in_string() << endl
<< endl;
}
string get_data_in_string()
{
// cout << "get_data_in_string called" << endl;
string s = "";
s = this->name + "," + this->id + "," + this->phone + "," + this->email + "," + to_string(this->current_books_no) + "," + to_string(this->all_books_no) + "," + this->get_current_issued_book_ids_in_string() + "," + this->get_all_issued_book_ids_in_string();
return s;
}
void add_new_issued_books(string book_name)
{
add_current_issued_books(book_name);
add_all_issued_books(book_name);
}
void show_all_issued_books_id()
{
cout << endl
<< "show_all_issued_books_id called" << endl
<< endl;
int i = 0;
while (i < this->all_books_issue_id.size())
{
cout << "-> " << this->all_books_issue_id[i] << endl;
i++;
}
cout << endl
<< endl;
}
void show_current_issued_books_id()
{
cout << endl
<< "show_current_issued_books_id called" << endl
<< endl;
int i = 0;
while (i < this->current_books_issue_id.size())
{
cout << i << " -> " << this->current_books_issue_id[i] << endl;
i++;
}
cout << endl
<< endl;
}
void deallocate_book_id(string book_id)
{
cout << "deallocate_book_id called" << endl;
int i = 0;
while (i < this->current_books_issue_id.size())
{
if (current_books_issue_id[i] == book_id)
{
cout << "Issue_id found with i = " << i << endl;
this->current_books_issue_id.erase(this->current_books_issue_id.begin() + i);
}
i++;
}
}
};
class Books
{
protected:
string author;
string genre;
string subject;
int total_no_books;
int available_no_books;
int overall_allocate_users;
int ongoing_allocate_users;
public:
string name;
virtual ~Books() = default; // Virtual destructor
virtual void display_book_info() const = 0;
Books()
{
cout << "Books default constructor called" << endl;
}
// Parameterized constructor
Books(string name, string author, string genre, string subject, int overall_allocate_users, int ongoing_allocate_users)
: name(name), author(author), genre(genre), subject(subject),
overall_allocate_users(overall_allocate_users), ongoing_allocate_users(ongoing_allocate_users) {}
// Another parameterized constructor
Books(string name, string author, string genre, string subject, int no)
: name(name), author(author), genre(genre), subject(subject),
total_no_books(no), available_no_books(no), overall_allocate_users(0), ongoing_allocate_users(0) {}
// Constructor with exception handling
Books(string arr[])
{
try
{
this->name = arr[0];
this->author = arr[1];
this->genre = arr[2];
this->subject = arr[3];
this->available_no_books = stoi(arr[4]);
this->total_no_books = stoi(arr[5]);
this->overall_allocate_users = stoi(arr[6]);
this->ongoing_allocate_users = stoi(arr[7]);
}
catch (const exception &e)
{
throw runtime_error("Error in Books constructor: " + string(e.what()));
}
}
// Encapsulation: Public method to modify private members
void add_ongoing_allocate_users()
{
try
{
if (available_no_books <= 0)
throw runtime_error("No available books to allocate.");
this->ongoing_allocate_users++;
this->overall_allocate_users++;
this->available_no_books--;
}
catch (const exception &e)
{
throw runtime_error("Error in add_ongoing_allocate_users: " + string(e.what()));
}
}
// Exception handling example
void remove_ongoing_allocate_users()
{
try
{
if (ongoing_allocate_users <= 0)
throw runtime_error("No ongoing allocations to remove.");
this->available_no_books++;
this->ongoing_allocate_users--;
}
catch (const exception &e)
{
throw runtime_error("Error in remove_ongoing_allocate_users: " + string(e.what()));
}
}
// Polymorphism: Overridable function display_book_info
virtual void display_book_data() const
{
cout << "Name = " << this->name << endl
<< "Author = " << this->author << endl
<< "Genre = " << this->genre << endl
<< "Subject = " << this->subject << endl
<< "Available_no_books = " << this->available_no_books << endl
<< "Total_no_books = " << this->total_no_books << endl
<< "Overall_allocate_users = " << this->overall_allocate_users << endl
<< "Ongoing_allocate_users = " << this->ongoing_allocate_users << endl
<< endl;
}
// Getter functions for protected members
string getGenre() const { return genre; }
int getTotalNoBooks() const { return total_no_books; }
string get_data_in_string() const
{
return name + "," + author + "," + genre + "," + subject + "," +
to_string(available_no_books) + "," + to_string(total_no_books) + "," +
to_string(overall_allocate_users) + "," + to_string(ongoing_allocate_users);
}
int get_available_no_books() const { return available_no_books; }
};
// Derived class from Books - Inheritance
class Educational_book : public Books
{
string book_name;
public:
static int no_of_educational_books;
Educational_book(string name, string author, string subject, int total_no_books)
: Books(name, author, "Educational book", subject, total_no_books), book_name(name)
{
no_of_educational_books++;
}
void display_book_info() const override
{
cout << "Educational Book Info:" << endl;
display_book_data();
}
};
// int Educational_book::no_of_educational_books = 0;
class Horror_book : public Books
{
string horror_book_name;
public:
static int no_of_horror_books;
Horror_book(string name, string author, string subject, int total_no_books)
: Books(name, author, "Horror book", subject, total_no_books), horror_book_name(name)
{
no_of_horror_books++;
}
void display_book_info() const override
{
cout << "Horror Book Info:" << endl;
display_book_data();
}
};
// int Horror_book::no_of_horror_books = 0;
class Adventure_book : public Books
{
string adventure_book_name;
public:
static int no_of_adventure_books;
Adventure_book(string name, string author, string subject, int total_no_books)
: Books(name, author, "Adventure book", subject, total_no_books), adventure_book_name(name)
{
no_of_adventure_books++;
}
void display_book_info() const override
{
cout << "Adventure Book Info:" << endl;
display_book_data();
}
void display_book_info(string additional_info) const
{
cout << additional_info << endl;
display_book_info();
}
};
// int Adventure_book::no_of_adventure_books = 0;
class Fantasy_book : public Books
{
string book_name;
public:
static int no_of_fantasy_books;
Fantasy_book(string name, string author, string subject, int total_no_books)
: Books(name, author, "Fantasy book", subject, total_no_books), book_name(name)
{
no_of_fantasy_books++;
}
void display_book_info() const override
{
cout << "Fantasy Book Info:" << endl;
display_book_data();
}
};
// int Fantasy_book::no_of_fantasy_books = 0;
class Science_Fiction_book : public Books
{
string book_name;
public:
static int no_of_sci_fiction_books;
Science_Fiction_book(string name, string author, string subject, int total_no_books)
: Books(name, author, "Science Fiction book", subject, total_no_books), book_name(name)
{
no_of_sci_fiction_books++;
}
void display_book_info() const override
{
cout << "Science Fiction Book Info:" << endl;
display_book_data();
}
};
// int Science_Fiction_book::no_of_sci_fiction_books = 0;
class Mystery_book : public Books
{
string book_name;
public:
static int no_of_mystery_books;
Mystery_book(string name, string author, string subject, int total_no_books)
: Books(name, author, "Mystery book", subject, total_no_books), book_name(name)
{
no_of_mystery_books++;
}
void display_book_info() const override
{
cout << "Mystery Book Info:" << endl;
display_book_data();
}
};
// int Mystery_book::no_of_mystery_books = 0;
class Thriller_Suspense_book : public Books
{
string book_name;
public:
static int no_of_thriller_books;
Thriller_Suspense_book(string name, string author, string subject, int total_no_books)
: Books(name, author, "Thriller & Suspense book", subject, total_no_books), book_name(name)
{
no_of_thriller_books++;
}
void display_book_info() const override
{
cout << "Thriller & Suspense Book Info:" << endl;
display_book_data();
}
};
class Other_book : public Books
{
string book_name;
public:
static int no_of_other_books;
Other_book(string name, string author, string genre, string subject, int total_no_books)
: Books(name, author, genre, subject, total_no_books), book_name(name)
{
no_of_other_books++;
}
void display_book_info() const override
{
cout << "Other Book Info:" << endl;
display_book_data();
}
};
// int Thriller_Suspense_book::no_of_thriller_books = 0;
// Template function to compare total number of books based on genre
template <typename GenreType>
bool compareTotalBooks(const Books &book, GenreType genre)
{
// Check if the book's genre matches the provided genre type
if (book.getGenre() == genre)
{
cout << "Total number of books in genre '" << genre << "' is: " << book.getTotalNoBooks() << endl;
return true;
}
else
{
cout << "The book's genre does not match the given genre type '" << genre << "'." << endl;
return false;
}
}
int Educational_book::no_of_educational_books = 0;
int Horror_book::no_of_horror_books = 0;
int Adventure_book::no_of_adventure_books = 0;
int Fantasy_book::no_of_fantasy_books = 0;
int Science_Fiction_book::no_of_sci_fiction_books = 0;
int Mystery_book::no_of_mystery_books = 0;
int Thriller_Suspense_book::no_of_thriller_books = 0;
int Other_book::no_of_other_books = 0;
// Vector declarations using pointers
vector<Users> users;
vector<Books *> books;
vector<Issued_books> issued_books;
vector<Educational_book *> educational_books;
vector<Horror_book *> horror_books;
vector<Adventure_book *> adventure_books;
vector<Fantasy_book *> fantasy_books;
vector<Science_Fiction_book *> science_fiction_books;
vector<Mystery_book *> mystery_books;
vector<Thriller_Suspense_book *> thriller_suspense_books;
vector<Other_book *> other_books;
// vector<Historical_Fiction_book*> historical_fiction_books;
// vector<Romance_Book*> romance_books;
// vector<Graphic_Novel*> graphic_novels;
string input_check(string input);
void Add_Book(vector<Books *> &books);
void Remove_Book(vector<Books *> &books);
void Add_User(vector<Users> &users);
void Remove_User(vector<Users> &users);
void Allocate_Book_to_User(vector<Users> &users, vector<Books *> &books, vector<Issued_books> &issued_books);
void Deallocate_Book_from_User(vector<Users> &users, vector<Books *> &books, vector<Issued_books> &issued_books);
void History_of_User(vector<Users> &users, string id);
int get_no_of_lines(ifstream &User_data);
void Update_users(vector<Users> &users);
void Update_Books(vector<Books *> &books);
void Update_Issued_Books(vector<Issued_books> &issued_books);
int does_the_user_exists(string id);
int does_the_book_exists(string name);
void show_all_users(vector<Users> &users);
void show_all_books(vector<Books *> &books);
void show_all_issued_books(vector<Issued_books> &issued_books);
void Remove_user_from_csv(vector<Users> &users, string id);
void Remove_book_from_csv(vector<Books *> &books, string name);
void Update_csv_from_users(vector<Users> &users);
void Update_csv_from_books(vector<Books *> &books);
void add_issued_book_data(string data);
void Update_csv_from_issued_books(vector<Issued_books> &issued_books);
int get_index_of_issued_book(string id);
int does_the_issued_book_exists(string id);
int current_no = 0;
// Makes Books classes by taking data from csv Books file and stores the classses in books vector
void Update_Books(vector<Books *> &books)
{
cout << "Update_Books called" << endl;
ifstream Books_data("Book_data.csv");
string s = "";
string arr[8];
getline(Books_data, s); // Skip header
s = "";
while (getline(Books_data, s))
{
int i = 0;
cout << "i = " << i << endl;
char del = ',';
stringstream ss(s);
string word;
while (!ss.eof())
{
getline(ss, word, del);
cout << word << " - ";
arr[i] = word;
i++;
}
// Create appropriate derived class based on genre
Books *temp = nullptr;
string genre = arr[2];
if (genre == "Educational book")
{
temp = new Educational_book(arr[0], arr[1], arr[3], stoi(arr[4]));
}
else if (genre == "Horror book")
{
temp = new Horror_book(arr[0], arr[1], arr[3], stoi(arr[4]));
}
else if (genre == "Adventure book")
{
temp = new Adventure_book(arr[0], arr[1], arr[3], stoi(arr[4]));
}
else if (genre == "Fantasy book")
{
temp = new Fantasy_book(arr[0], arr[1], arr[3], stoi(arr[4]));
}
else if (genre == "Science Fiction book")
{
temp = new Science_Fiction_book(arr[0], arr[1], arr[3], stoi(arr[4]));
}
else if (genre == "Mystery book")
{
temp = new Mystery_book(arr[0], arr[1], arr[3], stoi(arr[4]));
}
else if (genre == "Thriller & Suspense book")
{
temp = new Thriller_Suspense_book(arr[0], arr[1], arr[3], stoi(arr[4]));
}
if (temp != nullptr)
{
books.push_back(temp);
current_no_books++;
}
else
{
cout << "Warning: Unknown book genre '" << genre << "'" << endl;
}
}
Books_data.close();
}
// Makes Users classes by taking data from csv Users file and stores the classes in users vector
void Update_users(vector<Users> &users)
{
cout << "Update_users called" << endl;
ifstream User_data("User_data.csv");
string s = "";
string arr[7];
getline(User_data, s);
s = "";
while (getline(User_data, s))
{
int i = 0;
cout << "i = " << i << endl;
char del = ',';
stringstream ss(s);
string word;
while (!ss.eof())
{
getline(ss, word, del);
cout << word << " - ";
arr[i] = word;
i++;
}
// int index = stoi(arr[0]);
// cout << "index = " << index << endl;
Users temp(arr);
// temp.display_user_data();
users.push_back(temp);
// users[current_no_users].display_user_data();
current_no_users++;
}
User_data.close();
}
// Makes Issued_book classes by taking data from csv Issued_book file and stores the classes in issued_book vector
void Update_Issued_Books(vector<Issued_books> &issued_books)
{
cout << "Update_Books called" << endl;
ifstream Issued_book("Issued_books.csv");
string s = "";
char del = ',';
int i = 0;
getline(Issued_book, s);
s = "";
string arr[5];
while (!Issued_book.eof())
{
getline(Issued_book, s);
cout << "s = " << s << endl;
if (s == "")
{
cout << "if statemenet s == called" << endl;
return;
}
stringstream ss(s);
int j = 0;
while (!ss.eof())
{
getline(ss, arr[j], del);
j++;
}
Issued_books temp(arr);
issued_books.push_back(temp);
i++;
}
}
// returns length of lines in particular file
int get_no_of_lines(ifstream &file_name)
{
cout << "get_no_of_lines called" << endl;
int no_of_lines = 0;
string s = "";
while (getline(file_name, s))
{
no_of_lines++;
}
return no_of_lines;
}
// Adds book in database as well as in vector
void Add_Book(vector<Books *> &books)
{
Books *book;
// ifstream Book_data("Book_data.csv");
int no, k;
string s = "", Name, Author = "", Subject = "", Genre = "", temp;
cout << "Book Name : ";
cin.ignore();
getline(cin, Name);
Name = input_check(Name);
cout << "Author : ";
getline(cin, Author);
Author = input_check(Author);
cout << "GENRE :" << endl;
while (k != 1 && k != 2 && k != 3 && k != 4 && k != 5 && k != 6 && k != 7 && k != 8 && k != 9 && k != 10 || k != 11)
{
cout << ">>> 1. Educational Book" << endl;
cout << ">>> 2. Action & Adventure Book" << endl;
cout << ">>> 3. Fantasy Book" << endl;
cout << ">>> 4. Science Fiction Book" << endl;
cout << ">>> 5. Mystery Book" << endl;
cout << ">>> 6. Horror Book" << endl;
cout << ">>> 7. Thriller & Suspense Book" << endl;
cout << ">>> 8. Other" << endl
<< endl;
cout << " --->>> Please enter your choice : ";
cin >> temp;
if (temp == "1" || temp == "2" || temp == "3" || temp == "4" || temp == "5" || temp == "6" || temp == "7" || temp == "8")
{
// cout << "You have entered a valid number." << endl;
k = stoi(temp);
break;
}
else
{
cout << "You have not entered a valid number" << endl;
continue;
}
}
if (k == 8)
{
cout << ">-- Genre = ";
cin.ignore();
getline(cin, Genre);
Genre = input_check(Genre);
}
cout << "SUBJECT : ";
cin.ignore();
getline(cin, Subject);
Subject = input_check(Subject);
cout << "No of books: ";
cin >> no;
int i = does_the_book_exists(Name);
if (i != -1)
{
cout << "A book with same name already exists" << endl;
return;
}
switch (k)
{
case 1:
book = new Educational_book(Name, Author, Subject, no);
if (book != nullptr)
{
cout << "Book data = " << book->get_data_in_string() << endl;
books.push_back(book);
Educational_book *tempp;
// Converting the base class pointer to derived class pointer
// to store in the specific vector
tempp = (Educational_book *)book;
educational_books.push_back(tempp);
cout << "Edu Book data = " << tempp->get_data_in_string() << endl;
}
break;
case 2:
book = new Adventure_book(Name, Author, Subject, no);
if (book != nullptr)
{
cout << "Book data = " << book->get_data_in_string() << endl;
books.push_back(book);
Adventure_book *tempp;
// Converting the base class pointer to derived class pointer
// to store in the specific vector
tempp = (Adventure_book *)book;
adventure_books.push_back(tempp);
cout << "Adventure Book data = " << tempp->get_data_in_string() << endl;
}
break;
case 3:
book = new Fantasy_book(Name, Author, Subject, no);
if (book != nullptr)
{
cout << "Book data = " << book->get_data_in_string() << endl;
books.push_back(book);
Fantasy_book *tempp;
// Converting the base class pointer to derived class pointer
// to store in the specific vector
tempp = (Fantasy_book *)book;
fantasy_books.push_back(tempp);
cout << "Fantasy Book data = " << tempp->get_data_in_string() << endl;
}
break;
case 4:
book = new Science_Fiction_book(Name, Author, Subject, no);
if (book != nullptr)
{
cout << "Book data = " << book->get_data_in_string() << endl;
books.push_back(book);
Science_Fiction_book *tempp;
// Converting the base class pointer to derived class pointer
// to store in the specific vector
tempp = (Science_Fiction_book *)book;
science_fiction_books.push_back(tempp);
cout << "Sci-Fi Book data = " << tempp->get_data_in_string() << endl;
}
break;
case 5:
book = new Mystery_book(Name, Author, Subject, no);
if (book != nullptr)
{
cout << "Book data = " << book->get_data_in_string() << endl;
books.push_back(book);
Mystery_book *tempp;
// Converting the base class pointer to derived class pointer
// to store in the specific vector
tempp = (Mystery_book *)book;
mystery_books.push_back(tempp);
cout << "Mystery Book data = " << tempp->get_data_in_string() << endl;
}
break;
case 6:
book = new Horror_book(Name, Author, Subject, no);
if (book != nullptr)
{
cout << "Book data = " << book->get_data_in_string() << endl;
books.push_back(book);
Horror_book *tempp;
// Converting the base class pointer to derived class pointer
// to store in the specific vector
tempp = (Horror_book *)book;
horror_books.push_back(tempp);
cout << "Horror Book data = " << tempp->get_data_in_string() << endl;
}
break;
case 7:
book = new Thriller_Suspense_book(Name, Author, Subject, no);
if (book != nullptr)
{
cout << "Book data = " << book->get_data_in_string() << endl;
books.push_back(book);
Thriller_Suspense_book *tempp;
// Converting the base class pointer to derived class pointer
// to store in the specific vector
tempp = (Thriller_Suspense_book *)book;
thriller_suspense_books.push_back(tempp);
cout << "Thriller & Suspense Book data = " << tempp->get_data_in_string() << endl;
}
break;
case 8:
book = new Other_book(Name, Author, Genre, Subject, no);
if (book != nullptr)
{
cout << "Book data = " << book->get_data_in_string() << endl;
books.push_back(book);
Other_book *tempp;
// Converting the base class pointer to derived class pointer
// to store in the specific vector
tempp = (Other_book *)book;
other_books.push_back(tempp);
cout << "Other Book data = " << tempp->get_data_in_string() << endl;
}
break;
default:
cout << "Please enter a valid number" << endl;
break;
}
ofstream Book_data_edit("Book_data.csv", ios::app);
Book_data_edit << books[current_no_books]->get_data_in_string() << endl;
current_no_books++;
Book_data_edit.close();
}
// Removes book from database as well as from vector
void Remove_Book(vector<Books *> &books)
{
cout << "Remove_Book called" << endl;
string name;
cout << "Name: ";
cin.ignore();
getline(cin, name);
name = input_check(name);
cout << "Entered name = " << name;
int i = 0;
while (i < books.size())
{
string lower_b_n = books[i]->name, lname = name;
transform(lower_b_n.begin(), lower_b_n.end(), lower_b_n.begin(), ::tolower);
transform(lname.begin(), lname.end(), lname.begin(), ::tolower);
if (lower_b_n == lname)
{
books.erase(books.begin() + i);
current_no_books--;
Remove_book_from_csv(books, name);
show_all_books(books);
break;