-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
731 lines (633 loc) · 19.9 KB
/
main.cpp
File metadata and controls
731 lines (633 loc) · 19.9 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
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
/*
Author: Kishaan G.
Description: Library system getting and altering books from a file.
Made on replit.
Last modified: June 22, 2022.
*/
// ================= Pre-processor Commands =================
#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
#include <fstream>
using namespace std;
// ======================= Structures =======================
struct Book
{
string title = "";
string authorFullName = "";
int releaseYear = 0;
string genre = "";
bool inLibrary = false;
};
// ==================== Global constant ====================
const string FILE_NAME = "libraryBooks.txt";
// ====================== Prototypes ======================
void keyToContinue();
int menu(string, string[], int, string);
// ======== Getting and displaying books
void pasteBookInFile(ostream &, Book);
void getIntFromFile(int &);
void getBookFromFile(istream &, Book &);
void getAllBooksFromFile(Book[], int);
void putAllInfoInFile(Book[], int);
void allBooksMenu(Book[], int, string, string, int &);
// ======== Main interfaces
void userOrLibrarianMenu(Book[], int &);
void libraryUserInterface(Book[], int &);
Book* librarianInterface(Book[], int &);
// ================================== User Interface Options
void examineLibraryContents(Book[], int);
void searchForBookInterface(Book[], int);
void borrowOrReturnBookInterface(Book[], int, int);
// ================= Borrow / Return Books
int borrowBookMenu(Book[], int);
int returnBookMenu(Book[], int);
// ================= Searching for books
void searchBasedOnString(string, Book[], int, string, string);
void searchBasedOnInt(int, Book[], int, string);
void searchBasedOnAvailability(Book[], int);
// ============================== Librarian Interface Options
Book* addBookToLibrary(Book[], int &);
void removeBookFromLibrary(Book [], int &);
void modifyBook(Book [], int);
// ===================== Definitions =====================
ostream & operator << (ostream & _stream, Book _book)
{ // This provides a description of the book.
_stream << _book.title << " written by " << _book.authorFullName << " released in " << _book.releaseYear << ".\nGenre: " << _book.genre << endl;
if(_book.inLibrary)
{
_stream << "It is currently available in the library.";
}
else
{
_stream << "It is currently taken out of the library.";
}
return _stream;
}
// ===================================
void keyToContinue()
{
cout << "Type any key to continue..." << endl;
cin.ignore();
getchar();
return;
}
// ===================================
int menu(string prompt, string options[], int numberOfOptions, string errorMsg)
{
int response;
do
{
cout << prompt << endl;
for(int i = 0; i < numberOfOptions; i++)
{
cout << i+1 << ") " << options[i] << endl;
}
cin >> response;
if(response < 1 or response > numberOfOptions)
{
cout << errorMsg << endl;
keyToContinue();
system("clear");
}
}while (response < 1 or response > numberOfOptions);
return response;
}
// ===================================
void pasteBookInFile(ostream & _stream, Book _book)
{ // This will write a book into a file in a format that can be read.
_stream << _book.title << endl << _book.authorFullName << endl << _book.genre << endl;
if(_book.inLibrary)
{
_stream << "Available" << endl;
}
else
{
_stream << "Unavailable" << endl;
}
_stream << _book.releaseYear << endl;
return;
}
// =================================== Handling the file
void getIntFromFile(int & intResponse)
{ // This will read an integer from a file.
ifstream inputFile;
inputFile.open (FILE_NAME);
if(inputFile.is_open())
{
inputFile >> intResponse;
inputFile.close();
}
else
{
cout << "Could not open the file." << endl;
}
return;
}
// ===================================
void getBookFromFile(istream & _stream, Book & _book)
{ // Reads the information of a book from the file.
string inLibrary;
getline(_stream, _book.title);
getline(_stream, _book.authorFullName);
getline(_stream, _book.genre);
_stream >> inLibrary;
if(inLibrary == "Available")
{
_book.inLibrary = true;
}
else
{
_book.inLibrary = false;
}
_stream >> _book.releaseYear;
return;
}
// ===================================
void getAllBooksFromFile(Book _allBooks[], int _numOfBooks)
{
ifstream inputFile;
inputFile.open(FILE_NAME);
int digitsInNumOfBooks = (log10(_numOfBooks) + 1); // Determines the amount of digits in the variable _numOfBooks.
inputFile.ignore(digitsInNumOfBooks - 1);
if(inputFile.is_open())
{
inputFile.ignore();
for(int i = 0; i < _numOfBooks; i++)
{ // Gets the information of each book in the file.
inputFile.ignore();
getBookFromFile(inputFile, _allBooks[i]);
}
inputFile.close();
}
else
{
cout << "Could not open the file." << endl;
}
return;
}
// ===================================
void putAllInfoInFile(Book _allBooks[], int numOfBooks)
{
ofstream outputFile;
outputFile.open(FILE_NAME);
if(outputFile.is_open())
{
outputFile << numOfBooks << endl;
for(int i = 0; i < numOfBooks; i++)
{ // This will paste all the information of the books into the file.
pasteBookInFile(outputFile, _allBooks[i]);
}
outputFile.close();
}
else
{
cout << "Could not open the file." << endl;
}
return;
}
// ===================================
void allBooksMenu(Book _allBooks[], int numOfBooks, string prompt, string errorMsg, int & userChoice)
{ // This is a menu for all the books with a return option.
do
{
cout << prompt << endl << endl;
for(int i = 0; i < numOfBooks; i++)
{ // Displays all the books
cout << i+1 << ") " << _allBooks[i] << endl << endl;
}
cout << numOfBooks+1 << ") Return." << endl; // Displays a return option
cin >> userChoice;
if(userChoice < 1 or userChoice > numOfBooks+1)
{
system("clear");
cout << errorMsg << endl;
keyToContinue();
system("clear");
}
}while(userChoice < 1 or userChoice > numOfBooks + 1);
return;
}
// =================================== Main Interface
void userOrLibrarianMenu(Book allBooks[], int & numOfBooks)
{ // Checks if the user is a regular user or librarian.
int userMenuChoice = 0;
string menuOptions[3] = {"User", "Librarian", "Quit"};
string menuPrompt = "Are you a:";
string errorMsg = "Please type either 1, 2, or 3.";
Book* newAllBooks = allBooks;
do
{
userMenuChoice = menu(menuPrompt, menuOptions, 3, errorMsg);
switch(userMenuChoice)
{
case 1:
system("clear");
libraryUserInterface(allBooks, numOfBooks);
break;
case 2:
system("clear");
newAllBooks = librarianInterface(allBooks, numOfBooks);
allBooks = NULL;
allBooks = newAllBooks;
break;
case 3: // Quit option.
system("clear");
putAllInfoInFile(allBooks, numOfBooks);
break;
}
allBooks = newAllBooks;
}while(userMenuChoice != 3);
return;
}
// =================================== User Interface
void libraryUserInterface(Book allBooks[], int & numOfBooks)
{ // Provides options for a library user.
int userMenuChoice = 0;
string menuOptions[5] = {"Examine the library's books", "Search for a book", "Borrow a book", "Return a book", "Return"};
string menuPrompt = "What would you like to do?";
string errorMsg = "Please type either 1, 2, 3 4, or 5.";
do
{
userMenuChoice = menu(menuPrompt, menuOptions, 5, errorMsg);
switch(userMenuChoice)
{
case 1: // Examine the library's books
system("clear");
examineLibraryContents(allBooks, numOfBooks);
break;
case 2: // Search for a book based on of the properties
system("clear");
searchForBookInterface(allBooks, numOfBooks);
break;
case 3: // Borrow a book
system("clear");
borrowOrReturnBookInterface(allBooks, numOfBooks, 1);
break;
case 4: // Return a book
system("clear");
borrowOrReturnBookInterface(allBooks, numOfBooks, -1);
break;
case 5: // Return option
system("clear");
break;
}
}while(userMenuChoice != 5);
return;
}
// =================================== Librarian Interface
Book* librarianInterface(Book allBooks[], int & numOfBooks)
{ // Provides options for a librarian.
int userMenuChoice = 0;
string menuOptions[5] = {"Add a book to the library", "Remove a book from the library", "Modify a book", "Search for a book", "Return"};
string menuPrompt = "What would you like to do?";
string errorMsg = "Please type either 1, 2, 3, 4, or 5.";
Book* newAllBooks = allBooks;
do
{
userMenuChoice = menu(menuPrompt, menuOptions, 5, errorMsg);
switch(userMenuChoice)
{
case 1: // Add books to the library
system("clear");
newAllBooks = addBookToLibrary(allBooks, numOfBooks);
allBooks = NULL;
allBooks = newAllBooks;
break;
case 2: // Remove a book from the library
system("clear");
removeBookFromLibrary(allBooks, numOfBooks);
break;
case 3: // Modify a book
system("clear");
modifyBook(allBooks, numOfBooks);
break;
case 4: // Search for a book
system("clear");
searchForBookInterface(allBooks, numOfBooks);
break;
case 5: // Return option
system("clear");
break;
}
}while(userMenuChoice != 5);
return newAllBooks;
}
// =================================== Interface options
void examineLibraryContents(Book allBooks[], int numOfBooks)
{ // Provides descriptions for all the books in the library.
for(int i = 0; i < numOfBooks; i++)
{
cout << allBooks[i] << endl << endl;
}
keyToContinue();
system("clear");
return;
}
// ===================================
void searchForBookInterface(Book allBooks[], int numOfBooks)
{ // Provides the user with options to search for a book.
int userMenuChoice = 0;
string menuOptions[6] = {"Title", "Author", "Release Year", "Genre", "Library Availability", "Return"};
string menuPrompt = "What would you like to search for a book based on?";
string errorMsg = "Please enter a valid number.";
string stringSearch = "";
int intSearch = 0;
do
{
userMenuChoice = menu(menuPrompt, menuOptions, 6, errorMsg);
switch(userMenuChoice)
{
case 1: // Title
system("clear");
cout << "What is the name of the book?" << endl;
cin.ignore();
getline(cin, stringSearch);
searchBasedOnString(stringSearch, allBooks, numOfBooks, "Title", "Books with the title ");
break;
case 2: // Author
system("clear");
cout << "What is the name of the author?" << endl;
cin.ignore();
getline(cin, stringSearch);
searchBasedOnString(stringSearch, allBooks, numOfBooks, "Author", "Books with the author ");
break;
case 3: // Release year
system("clear");
cout << "What is the release year?" << endl;
cin >> intSearch;
searchBasedOnInt(intSearch, allBooks, numOfBooks, "Books released near ");
break;
case 4: // Genre
system("clear");
cout << "What is the genre?" << endl;
cin.ignore();
getline(cin, stringSearch);
searchBasedOnString(stringSearch, allBooks, numOfBooks, "Genre", "Books in the genre ");
break;
case 5: // Availability
system("clear");
searchBasedOnAvailability(allBooks, numOfBooks);
break;
case 6: // Return option
system("clear");
break;
}
}while(userMenuChoice != 6);
return;
}
// =================================== Borrow a Book
void borrowOrReturnBookInterface(Book _allBooks[], int numOfBooks, int borrowOrReturn)
{
int userChoice = 0;
if(borrowOrReturn == 1)
{
userChoice = borrowBookMenu(_allBooks, numOfBooks);
}
else if(borrowOrReturn == -1)
{
userChoice = returnBookMenu(_allBooks, numOfBooks);
}
if(userChoice != numOfBooks+1)
{
system("clear");
if(borrowOrReturn == 1)
{ // If the user is borrowing a book, the book is no longer available in the library.
_allBooks[userChoice - 1].inLibrary = false;
cout << "You succesfully borrowed " << _allBooks[userChoice - 1].title << "." << endl;
}
else if(borrowOrReturn == -1)
{ // If the user is returning a book, the book is now available in the library.
_allBooks[userChoice - 1].inLibrary = true;
cout << "You succesfully returned " << _allBooks[userChoice - 1].title << "." << endl;
}
keyToContinue();
}
system("clear");
return;
}
// ===========================
int borrowBookMenu(Book _allBooks[], int numOfBooks)
{
int userChoice = 0;
do
{
allBooksMenu(_allBooks, numOfBooks, "What book would you like to borrow?", "Please enter a valid input.", userChoice); // Gets what book the user wants to return.
if(_allBooks[userChoice - 1].inLibrary == false and userChoice != numOfBooks+1)
{ // If the user selected a book that is not available in the library, they can't borrow the book.
system("clear");
cout << "Please enter a book that is available in the library." << endl;
keyToContinue();
system("clear");
}
}while(_allBooks[userChoice - 1].inLibrary == false and userChoice != numOfBooks+1);
return userChoice;
}
// ===========================
int returnBookMenu(Book _allBooks[], int numOfBooks)
{
int userChoice = 0;
do
{
allBooksMenu(_allBooks, numOfBooks, "What book would you like to return?", "Please enter a valid input.", userChoice);
if(_allBooks[userChoice - 1].inLibrary == true and userChoice != numOfBooks+1)
{ // If the user selected a book that is available in the library, they can't return the book.
system("clear");
cout << "Please enter a book that is taken out of the library." << endl;
keyToContinue();
system("clear");
}
}while(_allBooks[userChoice - 1].inLibrary == true and userChoice != numOfBooks+1);
return userChoice;
}
// =========================== Search for books
void searchBasedOnString(string search, Book _allBooks[], int numOfBooks, string comparison, string prompt)
{
string capitalizedSearch = search;
transform(capitalizedSearch.begin(), capitalizedSearch.end(), capitalizedSearch.begin(), ::toupper); // Transforms the characters that the user searched into capital letters.
string capitalizedComparison;
cout << endl << prompt << search << " include:" << endl << endl;
for(int i = 0; i < numOfBooks; i++)
{
if(comparison == "Title")
{
capitalizedComparison = _allBooks[i].title;
}
else if(comparison == "Author")
{
capitalizedComparison = _allBooks[i].authorFullName;
}
else if(comparison == "Genre")
{
capitalizedComparison = _allBooks[i].genre;
}
transform(capitalizedComparison.begin(), capitalizedComparison.end(), capitalizedComparison.begin(), ::toupper); // Transforms the characters of what the user is searching for.
if(capitalizedSearch == capitalizedComparison)
{ // IF the user searched the name of a book, display it.
cout << _allBooks[i] << endl << endl;
}
else if(capitalizedComparison.find(capitalizedSearch) != string::npos) // Checks if the user typed in any similar string of chracters as what they're searching based on.
{
cout << _allBooks[i] << endl << endl;
}
}
keyToContinue();
system("clear");
return;
}
// ===========================
void searchBasedOnInt(int search, Book _allBooks[], int numOfBooks, string prompt)
{
cout << endl << prompt << search << " include:" << endl << endl;
for(int i = 0; i < numOfBooks; i++)
{
if(abs(search - _allBooks[i].releaseYear) < 4)
{ // Checks if the user's search is within 3 years to the release year of any book.
cout << _allBooks[i] << endl << endl;
}
}
keyToContinue();
system("clear");
return;
}
// ===========================
void searchBasedOnAvailability(Book _allBooks[], int numOfBooks)
{
string menuOptions[3] = {"Available", "Unavailable", "Return"};
int userMenuChoice = menu("What would you like to see books that are:", menuOptions, 3, "Please type either 1, 2 or 3.");
if(userMenuChoice == 1)
{
cout << endl << "All books available in the library include:" << endl << endl;
for(int i = 0; i < numOfBooks; i++)
{
if(_allBooks[i].inLibrary)
{ // Displays all available books.
cout << _allBooks[i] << endl << endl;
}
}
keyToContinue();
}
else if(userMenuChoice == 2)
{
cout << endl << "All unavailable books in the library include:" << endl << endl;
for(int i = 0; i < numOfBooks; i++)
{
if(_allBooks[i].inLibrary == false)
{ // Displays all unavailable books.
cout << _allBooks[i] << endl << endl;
}
}
keyToContinue();
}
system("clear");
return;
}
// ======================================= Add / Remove Books
Book* addBookToLibrary(Book _allBooks[], int & numOfBooks)
{
Book *newAllBooks = new Book[numOfBooks+1]; // Creates a pointer to a new array.
copy(_allBooks, _allBooks + min(numOfBooks, numOfBooks+1), newAllBooks); // Copies over all the books from the original array to the new pointer.
numOfBooks++;
string menuOptions[2] = {"Available", "Unavailable"};
string menuPrompt = "Is the book available?";
string errorMsg = "Please enter either 1 or 2.";
cin.ignore();
// Gets the information for the new book.
cout << "What is the title of the new book?" << endl;
getline(cin, newAllBooks[numOfBooks-1].title);
system("clear");
cout << "What is the author of the new book?" << endl;
getline(cin, newAllBooks[numOfBooks-1].authorFullName);
system("clear");
cout << "What is the release year of the new book?" << endl;
cin >> newAllBooks[numOfBooks-1].releaseYear;
cin.ignore();
system("clear");
cout << "What is the genre of the new book?" << endl;
getline(cin, newAllBooks[numOfBooks-1].genre);
system("clear");
int availabilityChoice = menu(menuPrompt, menuOptions, 2, errorMsg);
if(availabilityChoice == 1)
{
newAllBooks[numOfBooks-1].inLibrary = true;
}
else newAllBooks[numOfBooks-1].inLibrary = false;
system("clear");
cout << newAllBooks[numOfBooks-1] << endl << "has been added." << endl;
system("clear");
return newAllBooks; // Returns the pointer to the new array of books.
}
// ===================================
void removeBookFromLibrary(Book _allBooks[], int & numOfBooks)
{
int removeBook = 0;
allBooksMenu(_allBooks, numOfBooks, "What book would you like to remove?", "Please enter a valid input.", removeBook); // Gets which book the user wants to remove.
for(int i = removeBook-1; i < numOfBooks-1; i++)
{ // Moves all books back one position in the array from the position of the removed book.
_allBooks[i].title = _allBooks[i+1].title;
_allBooks[i].authorFullName = _allBooks[i+1].authorFullName;
_allBooks[i].releaseYear = _allBooks[i+1].releaseYear;
_allBooks[i].genre = _allBooks[i+1].genre;
_allBooks[i].inLibrary = _allBooks[i+1].inLibrary;
}
numOfBooks--;
system("clear");
return;
}
// ===================================
void modifyBook(Book _allBooks[], int numOfBooks)
{
int modifyBook = 0;
int modifyAttribute = 0;
string menuOptions[6] = {"Title", "Author", "Release Year", "Genre", "Library Availability", "Return"};
allBooksMenu(_allBooks, numOfBooks, "What book would you like to modify?", "Please enter a valid input.", modifyBook);
system("clear");
string availabilityOptions[2] = {"Available", "Unavailable"};
if(modifyBook != 5)
{ // Determines what book they want to modify.
modifyAttribute = menu("What would you like to modify?", menuOptions, 6, "Please enter a valid input.");
system("clear");
}
cin.ignore();
if(modifyAttribute == 1)
{ // Changes the contents of what the user chose to change.
cout << "What is the new title?" << endl;
getline(cin, _allBooks[modifyBook-1].title);
}
else if(modifyAttribute == 2)
{
cout << "Who is the new author?" << endl;
getline(cin, _allBooks[modifyBook-1].authorFullName);
}
else if(modifyAttribute == 3)
{
cout << "What is the new release year?" << endl;
cin >> _allBooks[modifyBook-1].releaseYear;
}
else if(modifyAttribute == 4)
{
cout << "What is the new genre?" << endl;
getline(cin, _allBooks[modifyBook-1].genre);
}
else if(modifyAttribute == 5)
{
cout << "What is the new availability?" << endl;
if(menu("Is the book now:", availabilityOptions, 2, "Please type either 1 or 2.") == 1)
{
_allBooks[modifyBook-1].inLibrary = true;
}
else _allBooks[modifyBook-1].inLibrary = false;
}
system("clear");
return;
}
// ===================================
int main()
{
int numOfBooks = 0;
getIntFromFile(numOfBooks);
Book allBooks[numOfBooks];
getAllBooksFromFile(allBooks, numOfBooks);
userOrLibrarianMenu(allBooks, numOfBooks);
return 0;
}