-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmovie.cpp
More file actions
250 lines (220 loc) · 6.56 KB
/
movie.cpp
File metadata and controls
250 lines (220 loc) · 6.56 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
#include "movie.h"
Movie::Movie() : duration(0) {
}
Movie::Movie(const string& title, const string& genre, int duration, const string& releaseDate, const int& ageRating, int count)
: title(title), genre(genre), duration(duration), releaseDate(releaseDate), ageRating(ageRating) {}
void Movie::setTitle(const string& title) {
this->title = title;
}
string Movie::getTitle() const {
return title;
}
void Movie::setGenre(const string& genre) {
this->genre = genre;
}
string Movie::getGenre() const {
return genre;
}
void Movie::setDuration(int duration) {
this->duration = duration;
}
int Movie::getDuration() const {
return duration;
}
void Movie::setReleaseDate(const string& releaseDate) {
this->releaseDate = releaseDate;
}
string Movie::getReleaseDate() const {
return releaseDate;
}
void Movie::setAgeRating(const int& ageRating) {
this->ageRating = ageRating;
}
int Movie::getAgeRating() const {
return ageRating;
}
void Movie::setCount(int count) {
this->count = count;
}
int Movie::getCount() const {
return count;
}
void Movie::addMovie() {
cout<<"Enter the movie title: \n";
getline(cin,title);
cout<<"Enter the movie genre: \n";
getline(cin,genre);
cout<<"Enter its duration in minutes: \n";
cin>>duration;
cin.get();
cout<<"Enter the release date: \n";
getline(cin,releaseDate);
cout<<"Enter its age rating: \n";
cin>>ageRating;
cin.get();
count++;
if (saveToFile()){
cout<<"Movie added to record successfully. "<<endl;
}
}
void Movie::viewMovie(const vector<Movie>& movies) {
if (movies.empty()) {
cout << "No movies to display." << std::endl;
return;
}
cout << "List of Movies:" << endl;
cout<<setw(20)<<left<<"Title"<<setw(20)<<left<<"Genre"<<setw(20)<<left<<"Duration"<<setw(20)<<left<<"Release Date"<<setw(20)<<left<<"Age Rating"<<endl;
cout<<"----------------------------------------------------------------------------------------------------"<<endl;
for (int i = 0; i < movies.size(); i++) {
cout << movies[i]<<endl;
}
}
Movie Movie::searchMovie(const vector<Movie>& movies) {
bool found = false;
cout<<"Enter the name of movie you want to search: ";
string searchTerm;
getline(cin,searchTerm);
cout << "Search Results:" << endl;
cout<<setw(20)<<left<<"Title"<<setw(20)<<left<<"Genre"<<setw(20)<<left<<"Duration"<<setw(20)<<left<<"Release Date"<<setw(20)<<left<<"Age Rating"<<endl;
cout<<"----------------------------------------------------------------------------------------------------"<<endl;
for (int i=0; i< movies.size(); i++) {
string title = movies[i].getTitle();
for (int j=0; j<title.length(); j++){
title[j]=tolower(title[j]);
}
for (int j=0; j<searchTerm.length(); j++){
searchTerm[j]=tolower(searchTerm[j]);
}
if (searchTerm==title) {
cout << movies[i];
found = true;
break;
}
}
if (!found) {
cout << "No movies found matching the search term \"" << searchTerm << "\"." << endl;
}
}
Movie Movie::selectMovie(){
vector<Movie> movies;
Movie::readFile(movies);
Movie::viewMovie(movies);
cout<<"Which movie do you want to add a screening for?\n";
string pick;
getline(cin,pick);
for (int i=0; i< movies.size(); i++) {
string title = movies[i].getTitle();
for (int j=0; j<title.length(); j++){
title[j]=tolower(title[j]);
}
for (int j=0; j<pick.length(); j++){
pick[j]=tolower(pick[j]);
}
if (pick==title) {
return movies[i];
}
}
cout << "No movies with title \"" << pick<< "\"." << endl;
Movie empty;
return empty;
}
void Movie::deleteMovie(vector<Movie>& movies) {
cout<<"\nEnter the name of the movie you want to delete: "<<endl;
bool deleted=false;
string searchTerm;
getline(cin,searchTerm);
for (int i=0; i< movies.size(); i++) {
string title = movies[i].getTitle();
for (int j=0; j<title.length(); j++){
title[j]=tolower(title[j]);
}
for (int j=0; j<searchTerm.length(); j++){
searchTerm[j]=tolower(searchTerm[j]);
}
if (searchTerm==title) {
movies.erase(movies.begin()+i);
deleted= true;
cout << "Movie titled '" << searchTerm << "' has been deleted." << endl;
overwriteFile(movies);
count--;
break;
}
}
if(!deleted) {
cout << "No movie found with the title '" << searchTerm << "'." << endl;
}
}
void Movie::overwriteFile(vector<Movie>& movies){
ofstream fout;
try{
fout.open("movies.txt");
if (fout.fail()){
throw runtime_error("Error opening movies.txt for writing");
}
}
catch(runtime_error &e){
cout<<e.what()<<endl;
return;
}
for (int i = 0; i < movies.size(); i++) {
fout<<movies[i].title<<endl;
fout<<movies[i].genre<<endl;
fout<<movies[i].duration<<endl;
fout<<movies[i].releaseDate<<endl;
fout<<movies[i].ageRating<<endl;
}
fout.close();
}
bool Movie::saveToFile() {
ofstream fout;
try{
fout.open("movies.txt",ios::app);
if (fout.fail()){
throw runtime_error("Error opening movies.txt for writing");
}
}
catch(runtime_error &e){
cout<<e.what()<<endl;
return false;
}
fout<<title<<endl;
fout<<genre<<endl;
fout<<duration<<endl;
fout<<releaseDate<<endl;
fout<<ageRating<<endl;
fout.close();
return true;
}
void Movie::readFile(vector<Movie>& movies) {
ifstream fin;
Movie m;
try{
fin.open("movies.txt");
if (fin.fail()){
throw runtime_error("Error opening movies.txt for reading");
}
}
catch(runtime_error &e){
cout<<e.what()<<endl;
return;
}
while( getline(fin,m.title)){
getline(fin,m.genre);
fin>>m.duration;
fin.get();
getline(fin,m.releaseDate);
fin>>m.ageRating;
fin.get();
movies.push_back(m);
count++;
}
}
ostream& operator<<(ostream& output, const Movie& movie) {
output<<setw(20)<<left<<movie.title<<setw(20)<<left<<movie.genre<<setw(20)<<left<<movie.duration<<setw(20)<<left<<movie.releaseDate<<movie.ageRating;
return output;
}
string Movie:: getMovie(){
string line="";
line+=title+"\n"+genre+"\n"+to_string(duration)+"\n"+releaseDate+"\n"+to_string(ageRating);
return line;
}