-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibrary Management System.cpp
More file actions
584 lines (504 loc) · 15.7 KB
/
Library Management System.cpp
File metadata and controls
584 lines (504 loc) · 15.7 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
#include <iostream>
#include <vector>
#include <string>
#include <cctype>
#include <fstream>
#include <sstream>
#include "Book.h" //include the Book class
//declaring operations for definition later
void addBook(std::vector<Book>& Library, int& nextID);
void listAllBooks(std::vector<Book>& Library);
void searchBooks(std::vector<Book>& Library);
void updateBooks(std::vector<Book>& Library);
void deleteBooks(std::vector<Book>& Library);
void saveFile(std::vector<Book>& Library);
void loadFile(std::vector<Book>& Library, int& nextID);
int main()
{
int nextID = 1;
std::vector<Book> Library; //making a vector of Book objects called a library to store our books
int running = 0;
while (running == 0)
{
int userChoice;
std::cout << "WELCOME TO OPEN LIBRARY!!!\n\n\n";
std::cout << "Select an option to choose operations: \n";
std::cout << "1)ADD BOOK - ENTER 1.\n";
std::cout << "2)LIST ALL BOOKS - ENTER 2.\n";
std::cout << "3)SEARCH BOOKS - ENTER 3.\n";
std::cout << "4)UPDATE BOOK - ENTER 4.\n";
std::cout << "5)DELETE BOOK - ENTER 5.\n";
std::cout << "6)SAVE FILE - ENTER 6.\n";
std::cout << "7)LOAD FILE - ENTER 7.\n";
std::cout << "8)EXIT PROGRAM - ENTER 8.\n";
std::cin >> userChoice;
std::cin.ignore();
switch (userChoice)
{
case 1:
addBook(Library, nextID);
break;
case 2:
listAllBooks(Library);
break;
case 3:
searchBooks(Library);
break;
case 4:
updateBooks(Library);
break;
case 5:
deleteBooks(Library);
break;
case 6:
saveFile(Library);
break;
case 7:
loadFile(Library, nextID);
break;
case 8:
std::cout << "Thanks for your time!\n";
return 0;
default:
std::cout << "Error in operation selection.\n";
}
}
return 0;
}
void addBook(std::vector<Book>& Library, int& nextID)
{
//declaring variables locally
int availableQuantity;
std::string title;
std::string author;
int releaseYear;
//getting input
std::cout << "Enter title: ";
std::getline(std::cin, title);
std::cout << "Enter author: ";
std::getline(std::cin, author);
std::cout << "Enter release year: ";
std::cin >> releaseYear;
std::cin.ignore(); //std::sin.ignore() used here because cin leaves a \n after it's done which would be picked up by a getline()
if (std::cin.fail()) //error handling for std::cin so it does not accept fail cases
{
std::cin.clear(); //clears the current broken state
std::cin.ignore(); //ignores leftover input caused to the error
std::cout << "Invalid input!!!\n";
return;
}
std::cout << "Enter release available quantity: ";
std::cin >> availableQuantity;
std::cin.ignore(); //only use where in was used right after it
if (std::cin.fail()) //error handling for std::cin so it does not accept fail cases
{
std::cin.clear(); //clears the current broken state
std::cin.ignore(); //ignores leftover input caused to the error
std::cout << "Invalid input!!!\n";
return;
}
if (title == "" || author == "" || releaseYear < 0 || releaseYear < 1800 || releaseYear > 2050 || availableQuantity < 0 || availableQuantity > 250)
{
std::cout << "Error invalid values.\n";
return;
}
//making the actual book object then sending it back to the vector with a temporary object name called newBook
int id = nextID++;; //id being generated as nextID++
Book newBook(id, availableQuantity, title, author, releaseYear);
Library.push_back(newBook); //sending the object made currently to the vector to save it
std::cout << "Book added!\n\n"; //indication to the user that the book was added
}
void listAllBooks(std::vector<Book>& Library)
{
if (Library.size() == 0) //to print incase the library is empty
{
std::cout << "Library currently empty!\n\n";
}
for (int i = 0; i < Library.size();i++)
{
std::cout << "Entry " << i + 1 << ": id = " << Library[i].getID() << " " << Library[i].getTitle() << " by " << Library[i].getAuthor() << " released on " << Library[i].getReleaseYear() << " Available Quantity = " << Library[i].getAvailableQuantity() << std::endl << '\n';
}
}
void searchBooks(std::vector<Book>& Library)
{
if (Library.size() == 0) //to print incase the library is empty
{
std::cout << "Library currently empty!\n\n";
}
int choice;
std::cout << "Search By: \n";
std::cout << "1. Title\n2. Author\n3. ID\n4. Release Year Range(e.g., 2020 - 2025)\n5. List All Books(sorted by year)\n\n";
std::cin >> choice;
std::cin.ignore();
if (std::cin.fail())
{
std::cin.clear();
std::cin.ignore(); //error handling
std::cout << "Error in choice input\n\n";
return;
}
switch (choice)
{
case 1: //title search
{
std::string searchTerm;
std::cout << "Enter your search term for the Title: ";
std::getline(std::cin, searchTerm);
std::cout << "\nSearch Term: '" << searchTerm << "'\n";
bool foundAny = false;
//converting each char in searchTerm as a ref in var c which then given to tolower saving back to itself
for (char& c : searchTerm)
{
c = tolower(c);
}
//making a ref variable called book of type Book from Library in a loop
for (const Book& book : Library)
{
std::string lowerCaseTitle = book.getTitle();
// Convert title to lowercase
for (char& c : lowerCaseTitle)
{
c = tolower(c);
}
//checking if the searchTerm was found in
if (lowerCaseTitle.find(searchTerm) != std::string::npos) //!= std::string::string::npos if a fancy way of saying not found
{ //when we do title.search() with any string it will give the index the string is found at
// Print book details using getters //as in if the given string to find isn't found std::string::npos will be returned it just means it was not found
foundAny = true;
std::cout << "ID: " << book.getID()
<< " | Title: " << book.getTitle()
<< " | Author: " << book.getAuthor()
<< " | Year: " << book.getReleaseYear()
<< " | Qty: " << book.getAvailableQuantity() << "\n";
}
}
if (!foundAny)
{
std::cout << "No books found matching '" << searchTerm << "'\n\n";
}
break;
}
case 2: //author search
{
std::string searchTerm;
std::cout << "Enter your search term for the Author: ";
std::getline(std::cin, searchTerm);
std::cout << "\nSearch Term: '" << searchTerm << "'\n";
bool foundAny = false;
//lower case conversion
for (char& c : searchTerm)
{
c = tolower(c);
}
//lowercase Author
for (const Book& book : Library)
{
std::string lowerCaseAuthor = book.getAuthor();
for (char& c : lowerCaseAuthor)
{
c = tolower(c);
}
if (lowerCaseAuthor.find(searchTerm) != std::string::npos)
{
foundAny = true;
std::cout << "\n\nID: " << book.getID()
<< " | Title: " << book.getTitle()
<< " | Author: " << book.getAuthor()
<< " | Year: " << book.getReleaseYear()
<< " | Qty: " << book.getAvailableQuantity() << "\n\n";
}
}
if (!foundAny)
{
std::cout << "No books found matching '" << searchTerm << "'\n\n";
}
break;
}
case 3: // ID Search
{
std::string searchTerm;
std::cout << "Enter ID search term: ";
// Clear input buffer first
std::getline(std::cin, searchTerm);
std::cout << "\nSearch Term: '" << searchTerm << "'\n";
bool foundAny = false;
//int booksChecked = 0;
for (const Book& book : Library)
{
std::string bookID = std::to_string(book.getID());
//booksChecked++;
//std::cout << "Checking Book ID: " << bookID << "\n";
if (bookID.find(searchTerm) != std::string::npos)
{
foundAny = true;
std::cout << "\nMATCH FOUND:\n"
<< "ID: " << book.getID()
<< " | Title: " << book.getTitle()
<< " | Author: " << book.getAuthor()
<< " | Year: " << book.getReleaseYear()
<< " | Qty: " << book.getAvailableQuantity() << "\n";
}
}
/*std::cout << "\nSearch Complete:\n"
<< "- Books in library: " << Library.size() << "\n"
<< "- Books checked: " << booksChecked << "\n"
<< "- Matches found: " << (foundAny ? "Yes" : "None") << "\n\n";*/
if (!foundAny)
{
std::cout << "No books found matching ID pattern '" << searchTerm << "'\n\n";
}
break;
}
case 4: //release year search
{
int userSearchYear1;
int userSearchYear2;
std::cout << "Enter starting year: ";
std::cin >> userSearchYear1;
std::cout << "Enter ending year: ";
std::cin >> userSearchYear2;
bool foundAny = false;
for (Book& book : Library)
{
if (book.getReleaseYear() >= userSearchYear1 &&
book.getReleaseYear() <= userSearchYear2)
{
foundAny = true;
std::cout << "\nMATCH FOUND:\n"
<< "ID: " << book.getID()
<< " | Title: " << book.getTitle()
<< " | Author: " << book.getAuthor()
<< " | Year: " << book.getReleaseYear()
<< " | Qty: " << book.getAvailableQuantity() << "\n";
}
}
if (!foundAny)
{
std::cout << "\n\nNo books found between "
<< userSearchYear1 << " and " << userSearchYear2 << "\n\n";
}
break;
}
case 5:
{
std::vector<Book> sortedBooks = Library; //making a copy of the library to sort them so we don't change the original Library which will mess up the other functionalities
//perforiming bubble sort to sort all Books in the Library by year
for (size_t i = 0;i < sortedBooks.size();i++) //using size_t to match the return type of sortedBooks.size()
{
for (size_t j = 0; j < sortedBooks.size() - i - 1;j++)
{
if (sortedBooks[j].getReleaseYear() > sortedBooks[j + 1].getReleaseYear())
{
std::swap(sortedBooks[j], sortedBooks[j + 1]);
}
}
}
//display sorted results to the user
std::cout << "All books sorted by release year (oldest first):\n";
for (const Book& book : sortedBooks)
{
std::cout << "ID: " << book.getID()
<< " | Title: " << book.getTitle()
<< " | Author: " << book.getAuthor()
<< " | Year: " << book.getReleaseYear()
<< " | Qty: " << book.getAvailableQuantity() << "\n";
}
std::cout << "\n\n";
break;
}
default:
{
std::cout << "Error in selection choice\n\n";
}
}
}
void updateBooks(std::vector<Book>& Library)
{
if (Library.size() == 0) //to print incase the library is empty
{
std::cout << "Library currently empty!\n\n";
}
int choice;
std::cout << "Enter the Id of the Book you want to Update: ";
std::cin >> choice;
std::cin.ignore();
bool found = false;
for (Book& book : Library)
{
if (book.getID() == choice)
{
found = true;
std::cout << "\n\nCurrent Book infortmation:\n\n";
std::cout << "ID: " << book.getID()
<< " | Title: " << book.getTitle()
<< " | Author: " << book.getAuthor()
<< " | Year: " << book.getReleaseYear()
<< " | Qty: " << book.getAvailableQuantity() << "\n\n";
std::string yesNo;
////ID
//std::cout << "Do you want to change the ID? Please answer in yes or no: ";
//std::getline(std::cin, yesNo);
//if (yesNo == "Yes" || yesNo == "yes")
//{
// int tempID; //section removed
// std::cout << "Enter the new id: ";
// std::cin >> tempID;
// std::cin.ignore();
// book.setID(tempID);
// std::cout << "\nID successfuly updated!\n\n";
//}
//
//TITLE
std::cout << "Do you want to change the Title? Please answer in yes or no: ";
std::getline(std::cin, yesNo);
if (yesNo == "Yes" || yesNo == "yes")
{
std::string newTitle;
std::cout << "Enter the new Title: ";
std::getline(std::cin, newTitle);
book.setTitle(newTitle);
book.setTitle(newTitle);
std::cout << "\nTitle successfuly updated!\n\n";
}
//AUTHOR
std::cout << "Do you want to change the Author? Please answer in yes or no: ";
std::getline(std::cin, yesNo);
if (yesNo == "Yes" || yesNo == "yes")
{
std::string newAuthor;
std::cout << "Enter the new Author: ";
std::getline(std::cin, newAuthor);
book.setAuthor(newAuthor);
std::cout << "\nAuthor successfuly updated!\n\n";
}
//YEAR
std::cout << "Do you want to change the Year? Please answer in yes or no: ";
std::getline(std::cin, yesNo);
if (yesNo == "Yes" || yesNo == "yes")
{
int tempYear;
std::cout << "Enter the new Year: ";
std::cin >> tempYear;
std::cin.ignore();
book.setReleaseYear(tempYear);
std::cout << "\nYear successfuly updated!\n\n";
}
//QUANTITY
std::cout << "Do you want to change the Quantity? Please answer in yes or no: ";
std::getline(std::cin, yesNo);
if (yesNo == "Yes" || yesNo == "yes")
{
int tempQuantity;
std::cout << "Enter the new Quantity: ";
std::cin >> tempQuantity;
std::cin.ignore();
book.setAvailableQuantity(tempQuantity);
std::cout << "\nQuantity successfuly updated!\n\n";
}
}
}
if (found == false)
{
std::cout << "\n\nNo books found matching ID: " << choice << "\n\n";
}
}
void deleteBooks(std::vector<Book>& Library)
{
if (Library.size() == 0) //to print incase the library is empty
{
std::cout << "Library currently empty!\n\n";
}
int choice;
std::cout << "Enter the Id of the Book you want to Delete: ";
std::cin >> choice;
std::cin.ignore();
bool found = false;
for (size_t i = 0;i < Library.size();i++)
{
if (Library[i].getID() == choice)
{
Library.erase(Library.begin() + i); //Library.erase(library.begin()+i being used to remove the desired Book from Library
found = true; //so erase just deletes anything thats given to it and begin starts at index 0
break; //we add the index i to make sure the desired Book is removed
}
}
std::cout << "\n\nBook removed!\n\n";
if (found)
{
std::cout << "\n\nBook removed!\n\n";
}
else
{
std::cout << "\n\nBook not found!\n\n";
}
}
void saveFile(std::vector<Book>& Library)
{
std::ofstream outFile("Library Save.csv"); //saving the file as "Library save.csv" a csv file that can be read by other programs as well
if (!outFile)
{
std::cout << "\n\nError: Can't create file!\n\n"; //error check incase the file was not created
return;
}
else
{
outFile << "ID,Title,Author,Year,Qty\n";
for (Book& book : Library)
{
outFile << book.getID() << ',' << book.getTitle() << ',' << book.getAuthor() << ',' << book.getReleaseYear() << ',' << book.getAvailableQuantity() << '\n'; //adding text in the csv file
}
std::cout << "\n\nFile saved Successfully!\n\n"; //letting the user know the file was created successfully
}
}
void loadFile(std::vector<Book>& Library, int& nextID)
{
Library.clear(); //clear current Library to prepare to load file
std::ifstream file("Library Save.csv");
if (!file.is_open())
{
std::cout << "\n\nError: Can't open file!\n\n";
return;
}
std::string header;
std::getline(file, header); //skip the header line
std::string line;
while (std::getline(file, line))
{
std::stringstream ss(line);
std::string token;
std::vector<std::string> tokens;
//definin a string stream which acts like the input and adding token to then tokens vector
while (std::getline(ss, token, ','))
{
tokens.push_back(token);
}
//checking if we have all the five tokens so the book has all the valid data if not skipping it
if (tokens.size() != 5)
{
std::cout << "Skipping invalid line: " << line << "\n";
continue;
}
try
{
//converting all the string values to their types using string to int
int id = std::stoi(tokens[0]);
std::string title = tokens[1];
std::string author = tokens[2];
int year = std::stoi(tokens[3]);
int quantity = std::stoi(tokens[4]);
//adding the values to the back of the Library using emplace_back saving it
Library.emplace_back(id, quantity, title, author, year);
}
catch (const std::exception& e) //looking for erros that happen while conversion
{
std::cout << "Error parsing line: " << line << "\n";
std::cout << "Reason: " << e.what() << "\n"; //giving a proper reasing why the parsing error happened
}
}
std::cout << "\nLoaded " << Library.size() << " books from file\n\n"; //informing the user how many books have been loaded
int maxID = 0;
for (const Book& book : Library) {
if (book.getID() > maxID) maxID = book.getID();
}
nextID = maxID + 1; // Update global counter
}