-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibraryItemsCollection.cpp
More file actions
209 lines (178 loc) · 5.99 KB
/
LibraryItemsCollection.cpp
File metadata and controls
209 lines (178 loc) · 5.99 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
/*
File:
LibraryItemsCollection.cpp
*/
#include "LibraryItemsCollection.h"
#include "Book.h"
#include "AudioCD.h"
#include "DVD.h"
#include <algorithm>
#include <memory>
using namespace std;
LibraryItemsCollection::LibraryItemsCollection() {}
LibraryItemsCollection::~LibraryItemsCollection() {}
//Create a new LibraryItem
void LibraryItemsCollection::addLibraryItem(shared_ptr<LibraryItem> item)
{
items.push_back(item); //Add the item to the collection (items.push_back(item))void editLibraryItem(int libraryID, int itemType)
}
shared_ptr<LibraryItem> LibraryItemsCollection::getLibraryItemByID(int libraryID)
{
for (auto& item : items)
{
if (item->getLibraryID() == libraryID)
{
return item; //Return the item if the ID matches
}
}
return nullptr; // Return nullptr if no matching item is found
}
vector<shared_ptr<LibraryItem>> LibraryItemsCollection::getItemsByType(const string& type) const
{
vector<shared_ptr<LibraryItem>> filteredItems;
for (const auto& item : items)
{
//If item.getLibraryID() == Library ID || Artist Name || DVD Title
if ((type == "Book" && dynamic_cast<Book*>(item.get())) || (type == "AudioCD" && dynamic_cast<AudioCD*>(item.get())) || (type == "DVD" && dynamic_cast<DVD*>(item.get())))
{
filteredItems.push_back(item);
}
}
return filteredItems;
}
void LibraryItemsCollection::editLibraryItem(int libraryID, int itemType)
{
for (auto& item : items)
{
if (item->getLibraryID() == libraryID)
{
//Depending on the item type, call the respective edit function
switch (itemType)
{
//Prompt user for details to edit (library item details)
case 1:
dynamic_pointer_cast<Book>(item)->editItem(); //Call editItem on Book
break;
case 2:
dynamic_pointer_cast<AudioCD>(item)->editItem(); //Call editItem on AudioCD
break;
case 3:
dynamic_pointer_cast<DVD>(item)->editItem(); //Call editItem on DVD
break;
default:
cout << "Invalid item type." << endl; //Wrong item type
return;
}
return; //Return (Operation complete)
}
}
//If not found, display "Library item not found
cout << "Item with Library ID " << libraryID << " not found!" << endl;
}
bool LibraryItemsCollection::deleteLibraryItem(const string& identifier, int deleteItemType)
{
for (auto it = items.begin(); it != items.end(); ++it)
{
//Check if the item matches the given identifier based on type
if (deleteItemType == 1) //Match by Library ID from Book
{
auto bookItem = dynamic_cast<Book*>(it->get());
if (bookItem && to_string(bookItem->getLibraryID()) == identifier)
{
items.erase(it); //Clear all items from the collection (items.erase(it))
return true;
}
}
else if (deleteItemType == 2)
{
auto audioCDItem = dynamic_cast<AudioCD*>(it->get());
if (audioCDItem && audioCDItem->getArtist() == identifier)
{
items.erase(it); //Clear all items from the collection (items.erase(it))
return true;
}
}
else if (deleteItemType == 3)
{
auto dvdItem = dynamic_cast<DVD*>(it->get());
if (dvdItem && dvdItem->getTitle() == identifier)
{
items.erase(it); //Clear all items from the collection (items.erase(it))
return true;
}
}
}
return false;
}
void LibraryItemsCollection::searchLibraryItemByLibraryID(int libraryID) const
{
bool found = false;
for (const auto& item : items)
{
//Check if the item is of type Book and the libraryID matches
if (auto book = dynamic_cast<Book*>(item.get()))
{
if (book->getLibraryID() == libraryID) //If item.getLibraryID() == Library ID:
{
book->printItemDetails(); //Print item details (item.printItemDetails())
found = true;
break;
}
}
//Add cases for other item types like AudioCD, DVD if needed
}
if (!found)
{
cout << "No Library Item found with ID: " << libraryID << endl; //If not found, display that the Library item with ID is not found.
}
}
void LibraryItemsCollection::searchLibraryItemByArtist(const string& artist) const
{
bool found = false;
for (const auto& item : items)
{
if (auto audioCD = dynamic_cast<AudioCD*>(item.get()))
{
if (audioCD->getArtist() == artist) //If item.getArtist() == Artist Name:
{
item->printItemDetails(); //Print item details (item.printItemDetails())
found = true; //Return (Found and printed details)
}
}
}
if (!found)
{
cout << "No AudioCD found with Artist: " << artist << endl; //If not found, display that the Library item with Artist Name is not found
}
}
void LibraryItemsCollection::searchLibraryItemByTitle(const string& title) const
{
bool found = false;
for (const auto& item : items)
{
if (auto dvd = dynamic_cast<DVD*>(item.get()))
{
if (dvd->getTitle() == title) //If item.getTitle() == DVD Title:
{
item->printItemDetails(); //Print item details (item.printItemDetails())
found = true; //Return (Found and printed details)
}
}
}
if (!found)
{
cout << "No DVD found with Title: " << title << endl; //If not found, display that the library item with DVD Title is not found
}
}
//Prints Item Details
void LibraryItemsCollection::printItemDetails() const
{
for (const auto& item : items)
{
item->printItemDetails();
}
}
vector<shared_ptr<LibraryItem>>& LibraryItemsCollection::getItems()
{
return items;
}