-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBooksCollection.cpp
More file actions
128 lines (97 loc) · 3.48 KB
/
BooksCollection.cpp
File metadata and controls
128 lines (97 loc) · 3.48 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
/*
File:
BooksCollection.cpp
*/
#include "BooksCollection.h"
#include <iostream>
using namespace std;
//Adds a new book to the collection by calling the addBook() method from the Book class
void BooksCollection::addBooksCollection()
{
Book newBook; //Call the Book’s method to input details
newBook.addItem();
books.push_back(newBook); //Add the new patron to the vector by pushing the new book into the book’s vector
// Calls LibraryItemsCollectionItemsCollection
shared_ptr<LibraryItem> item = make_shared<Book>(newBook);
libraryItemsCollection.addLibraryItem(item); //Add to LibraryItemsCollection
cout << "Book added successfully." << endl;
}
//Edits an existing book in the collection
void BooksCollection::editBooksCollection()
{
int id;
cout << "\nEnter Library ID Number of book to edit: "; // Prompt user for Book ID
cin >> id;
//Search for the book in the LibraryItemsCollection using its ID
shared_ptr<LibraryItem> item = libraryItemsCollection.getLibraryItemByID(id);
//If item is found, cast it to Book and edit
if (item)
{
dynamic_pointer_cast<Book>(item)->editItem();
cout << "Book has been updated." << endl;
}
else
{
cout << "Item not found!" << endl;
}
}
//Deletes a book from the collection by its Library ID.
void BooksCollection::deleteBooksCollection()
{
int id;
cout << "Enter Library ID Number of book to delete: "; // Prompt user for Book ID
cin >> id;
string idStr = to_string(id);
//Search and delete in the LibraryItemsCollection
shared_ptr<LibraryItem> item = libraryItemsCollection.getLibraryItemByID(id);
if (item) //If item is found, delete it
{
dynamic_pointer_cast<Book>(item)->deleteItem();
libraryItemsCollection.deleteLibraryItem(idStr, 1); //Remove from LibraryItemsCollection
}
else
{
cout << "Item not found!" << endl;
}
}
//Searches for a book by its Library ID
void BooksCollection::findBooksCollection() const
{
int id;
cout << "Enter Library ID Number of book to Search or Find: "; //Prompt user for Book ID Number to search
cin >> id;
//Search by Book ID and use getLibraryID to find the Book by ID Number
for (const auto &book : books)
{
if (book.getLibraryID() == id)
{
book.printItemDetails(); //If found, the book's details are printed using printBookDetails();
return;
}
}
cout << "Library ID Number does not exist." << endl; //If the Book ID Number does not match, it does not exist
}
//Prompt user for Book ID Number to print all Book collection
void BooksCollection::printAllBooksCollection() const
{
if (books.empty())
{
cout << "No books found." << endl;
return;
}
for (const auto &book : books) { //Loops through all the books in the collection and prints the details of each book by calling printBookDetails() for each book in the 'books' vector.
book.printItemDetails(); //Output the relevant details
}
}
//Prompt user for Book ID Number to print single Book’s collection
void BooksCollection::printSingleBook(int libraryID) const
{
if (books.empty())
{
cout << "No books found." << endl;
return;
}
for (const auto &book : books) { //Loops through all the books in the collection and prints the details of each book by calling printBookDetails() for each book in the 'books' vector.
book.printItemDetails();
}
}