-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassesMain.cpp
More file actions
256 lines (230 loc) · 7.75 KB
/
Copy pathclassesMain.cpp
File metadata and controls
256 lines (230 loc) · 7.75 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
#include <iostream>
#include <cstring>
#include <vector>
#include "classesParent.h"
#include "videoGames.h"
#include "music.h"
#include "movie.h"
using namespace std;
/*
* Name | Sophia You
* Date | November 8 2023
* Description |
* The main method contains the parent class. The user can add, search, or
* delete items in the media library.
*/
// funaction prototypes
void addMedia(vector<Parent*> &mediaList);
void searchMedia(vector<Parent*> &mediaList);
void deleteMedia(vector<Parent*> &mediaList);
int main()
{
// This vector will hold all types of media
vector<Parent*> mediaList;
int editing = 1; // checks to see if the user wants to quit
int max = 80;
char input[max]; // choose type of media
cout << "Welcome to the media library. Please enter a command." << endl;
while (strcmp(input, "quit") != 0)
{
cout << "Type 'add' to add a type of media." << endl;
cout << "Type 'search' to search for media in a database." << endl;
cout << "Type 'delete' to delete an item from the library." << endl;
cout << "Type 'quit' to quit." << endl;
cin.getline(input, max);
if (strcmp(input, "add") == 0)
{
addMedia(mediaList);
}
else if (strcmp(input, "search") == 0)
{
searchMedia(mediaList);
}
else if (strcmp(input, "delete") == 0)
{
deleteMedia(mediaList);
}
// if the user enters "quit," doing do anything
// when it goes back up to the while loop, it'll terminate
}
return 0;
}
// this function takes in user input and puts the media into a vector
void addMedia(vector<Parent*> &mediaList)
{
int max = 100;
char input[max];
cout << "First, enter some basic information." << endl;
cout << "Title: " << endl;
char* title = new char[100];
cin.getline(input, max); // reads in info
strcpy(title, input);
cout << "Year Released: " << endl;
int year = 0;
cin >> year;
cin.ignore(max, '\n');
// reads in info
// specify the media type
cout << "" << endl;
cout << "Enter one of the following media types. Only use lower case lett\
ers." << endl;
cout << "video games" << endl;
cout << "movies" << endl;
cout << "music" << endl;
cin.getline(input, max);
// we need specific info depending on the type of media
if (strcmp(input, "video games") == 0)
{
cout << "Publisher: " << endl;
char* publisher = new char[100];
cin.getline(input, max); // reads in info
strcpy(publisher, input);
cout << "Rating: " << endl;
float rating = 0;
cin >> rating; // reads in info
cin.ignore(max, '\n');
VideoGames* videoGame = new VideoGames(title, year, publisher, rating); // this goes in the vector
mediaList.push_back(videoGame); // adds to vector
cout << "You have now added a new game." << endl;
videoGame->print(); // prints out entered info
}
else if (strcmp(input, "music") == 0)
{
// asks for user to input fields
cout << "artist: " << endl;
char* artist = new char[100];
cin.getline(input, max); // reads in info
strcpy(artist, input);
cout << "Duration (minutes): " << endl;
float duration = 0;
cin >> duration; // reads in info
cin.ignore(max, '\n');
cout << "Publisher: " << endl;
char* publisher = new char[100];
cin.getline(input, max); // reads in info
strcpy(publisher, input);
Music* music = new Music(title, year, artist, duration, publisher); // this goes in the vector
mediaList.push_back(music); // adds to vector
cout << "You have now added new music." << endl;
music->print(); // prints out entered info
}
else if (strcmp(input, "movies") == 0)
{
// enter fields
cout << "Director: " << endl;
char* director = new char[100];
cin.getline(input, max);
strcpy(director, input);
cout << "Duration (minutes): " << endl;
float duration = 0;
cin >> duration; // reads in info
cin.ignore(max, '\n');
cout << "Rating: " << endl;
float rating = 0;
cin >> rating; // reads in info
cin.ignore(max, '\n');
Movie* movie = new Movie(title, year, director, duration, rating); //this goes in the vector
mediaList.push_back(movie); // adds to vector
cout << "You have now added a new movie." << endl;
movie->print(); // prints out entered info
}
}
// this function allows the user to search for media by title or year.
void searchMedia(vector<Parent*> &mediaList)
{
cout << "If searching by title, enter 'title'. If searching by year, enter 'year.' Lower case letters only!" << endl;
int max = 80;
char input[max];
cin.getline(input, max);
if (strcmp(input, "title") == 0) // search by title
{
cout << "Enter title: " << endl;
cin.getline(input, max);
// walk through the vector and find the occurences
for (vector<Parent*>::iterator it = mediaList.begin(); it !=mediaList.end(); it++)
{
if(strcmp(input, (*it)->getTitle()) == 0)
{
(*it)->print(); // print out the media information
}
}
}
else if (strcmp(input, "year") == 0) // search by year
{
int year = 0;
cout << "Enter year: " << endl;
cin >> year;
cin.ignore(max, '\n');
// walk through the vector and find the occurrences
for (vector<Parent*>::iterator it = mediaList.begin(); it !=mediaList.end\
(); it++)
{
if(year == (*it)->getYear()) // if the year matches the mediaList year
{
(*it)->print();
}
}
}
}
/* this function asks for the title or year, asks for user confirmation
* and deletes the media upon said user confirmation. If the user does not
* want to delete that piece of media, the program moves onto the next item in
* the list
*/
void deleteMedia(vector<Parent*> &mediaList)
{
cout << "You are now deleting items from the media library. You will need to enter either the TITLE or the YEAR. To delete by title, enter 'title', and to delete by year, enter 'year.'" << endl;
int max = 80;
int year = 0;
char input[max];
char confirm[max]; // stores if the user wants to delete the item
cin.getline(input, max);
if (strcmp(input, "title") == 0) // search by title
{
cout << "Enter title: " << endl;
cin.getline(input, max);
// walk through the vector and find the occurances
for (vector<Parent*>::iterator it = mediaList.begin(); it !=mediaList.end\
(); it++)
{
if(strcmp(input, (*it)->getTitle()) == 0)
{
(*it)->print(); // print the media first
cout << "Delete this item? Enter 'yes' or 'no.' Lower case letters only." << endl;
cin.getline(confirm, max);
if (strcmp(confirm, "yes") == 0) // if they confirm, delete, otherwise move on
{
cout << "Item deleted." << endl;
delete *it; // delete the actual media
mediaList.erase(it); // delete the pointer in memory
break;
}
}
}
}
else if (strcmp(input, "year") == 0) // searching by year
{
cout << "Enter year: " << endl;
cin >> year;
cin.ignore(max, '\n');
for (vector<Parent*>::iterator it = mediaList.begin(); it !=mediaList.end\
\
(); it++)
{
if(year == (*it)->getYear())
{
(*it)->print(); // print the media first
cout << "Delete this item? Enter 'yes' or 'no.' Lower case letter\
s only." << endl;
cin.getline(confirm, max);
if (strcmp(confirm, "yes") == 0) // if they confirm, delete
{
cout << "Item deleted." << endl;
delete *it; // delete the actual media
mediaList.erase(it); // delete the pointer in memory
break;
}
}
}
}
}