Jul 6, 2022 at 4:59am UTC
Hello This is the code that I've addressed last time and I made a new one and made a new code and currently working on it. This is my concern When you input 6 in the movie menu, to check Movie Availability, you will proceed to Check Movie code. However, when you input a Movie code which is unavailable, instead of telling the program, Movie is unavailable(line 217), it will tell the system it will just exit the program instead of going back to Movie Menu. Can someone help me fix the code based on the link below. I just put a link there since the code is too long.
Last edited on Jul 7, 2022 at 10:23am UTC
Jul 6, 2022 at 12:08pm UTC
As a first refactor, consider something like (NOT tried) [posted in 3 posts due to code size limitations]
Part 1:
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
#include <string>
#include <deque>
#include <stack>
#include <sstream>
#include <fstream>
#include <iostream>
using namespace std;
struct Movie {
int movCode {};
std::string movTitle;
std::string movGenre;
std::string movproduction;
int movCopies {};
};
struct Customer {
int custCode {};
std::string custName;
std::string custAddress;
stack < int > custRentmov;
};
struct RentByCustomer {
int rentMovieCode {};
std::string rentMovName;
int movCopies2 {};
int rentCustomerID {};
std::string rentCustName;
};
class movieList {
private :
Customer custlq;
bool cQcheck {};
deque < Customer > custQueue;
RentByCustomer rentMovbyC;
stack < RentByCustomer > custMova;
stack < RentByCustomer > custMovb;
stack < int > custMovc;
struct movieListNode {
Movie movl;
struct movieListNode* Mnext {};
};
movieListNode* Moviehead {};
public :
movieList() {}
~movieList();
movieList(const movieList&) = delete ;
movieList& operator =(const movieList&) = delete ;
void addMovie();
void rentMovie(int movCode, int rentCustomerID);
void returnMovie(int rentByCustomer, int returnedMovie);
bool isCustomerEmpty() const ;
void SaveCustRentMov();
void rentedByCustomer();
void showMovieDetails(int movCode) const ;
void displayMovies() const ;
void movieAvailability(int movCode) const ;
void OpenMovieFile();
void SaveMovieFile();
void addCustomer();
void custDets(int ) const ;
void rentListCustomer(int );
void OpenCustomerFile();
void SaveCustomerFile();
};
movieList::~movieList() {
for (; Moviehead; ) {
const auto ptr { Moviehead->Mnext };
delete Moviehead;
Moviehead = ptr;
}
}
void movieList::displayMovies() const {
cout << "Display All Movies\n" ;
cout << "Movies Available:\n\n" ;
for (auto MovieFile { Moviehead }; MovieFile; MovieFile = MovieFile->Mnext) {
cout << "--------------------------------\n" ;
cout << "Movie Code: " << MovieFile->movl.movCode << '\n' ;
cout << "Movie Title: " << MovieFile->movl.movTitle << '\n' ;
cout << "Movie Genre: " << MovieFile->movl.movGenre << '\n' ;
cout << "Movie Producer: " << MovieFile->movl.movproduction << '\n' ;
cout << "Number of Copies: " << MovieFile->movl.movCopies << "\n\n" ;
cout << "--------------------------------\n" ;
}
}
void movieList::addCustomer() {
cQcheck = true ;
cout << "Enter the Customer Code: " ;
cin >> custlq.custCode;
cin.ignore();
cout << "Enter the Customer Name: " ;
getline(cin, custlq.custName);
cout << "Enter the Customer Address: " ;
getline(cin, custlq.custAddress);
custQueue.push_back(custlq);
cout << "\nGood Job! You have added a customer!\n" ;
//cout << "Press any key.\n";
}
void movieList::custDets(int forShowCustDetails) const {
if (!custQueue.empty()) {
for (auto & cust : custQueue)
if (cust.custCode == forShowCustDetails) {
cout << "Customer Name: " << cust.custName << '\n' ;
cout << "Address: " << cust.custAddress << '\n' ;
break ;
}
} else
cout << "Customer is empty!\n" ;
//cout << "Press any key.\n";
}
void movieList::addMovie() {
auto movnewNode { new movieListNode };
cout << "Enter the Movie Code " ;
cin >> movnewNode->movl.movCode;
cin.ignore();
cout << "Enter the Movie title: " ;
getline(cin, movnewNode->movl.movTitle);
cout << "Enter the Movie genre: " ;
getline(cin, movnewNode->movl.movGenre);
cout << "Enter the Movie producer: " ;
getline(cin, movnewNode->movl.movproduction);
cout << "Enter the number of copies: " ;
cin >> movnewNode->movl.movCopies;
cin.ignore();
cout << '\n' ;
if (!Moviehead)
Moviehead = movnewNode;
else {
auto movnodePtr { Moviehead };
for (; movnodePtr->Mnext; movnodePtr = movnodePtr->Mnext);
movnodePtr->Mnext = movnewNode;
}
ofstream smf("Movies.txt" , ios::app);
if (smf.is_open()) {
smf << movnewNode->movl.movCode << '\n' ;
smf << movnewNode->movl.movTitle << '\n' ;
smf << movnewNode->movl.movGenre << '\n' ;
smf << movnewNode->movl.movproduction << '\n' ;
smf << movnewNode->movl.movCopies << '\n' ;
}
cout << "\nYou have successfully added the movie!\n" ;
//cout << endl << "Press any key.\n";
}
void movieList::rentMovie(int movCode, int rentCustomerID) {
auto itr { custQueue.begin() };
for (; itr != custQueue.end(); ++itr)
if (itr->custCode == rentCustomerID)
break ;
if (itr == custQueue.end()) {
cout << "Customer id not found\n" ;
return ;
}
for (auto movnodePtr { Moviehead }; movnodePtr; movnodePtr = movnodePtr->Mnext)
if (movnodePtr->movl.movCode == movCode) {
movnodePtr->movl.movCopies -= 1;
rentMovbyC.movCopies2 = 0;
rentMovbyC.rentMovieCode = movnodePtr->movl.movCode;
rentMovbyC.rentMovName = movnodePtr->movl.movTitle;
rentMovbyC.rentCustomerID = itr->custCode;
rentMovbyC.rentCustName = itr->custName;
++rentMovbyC.movCopies2;
custMova.push(rentMovbyC);
cout << "You have successfully rented the movie\n" ;
//cout << "Press any key.\n";
return ;
}
cout << "Movie code not found\n" ;
}
void movieList::movieAvailability(int forRentCustMov) const {
auto foundMovie {Moviehead};
for (; foundMovie && foundMovie->movl.movCode != forRentCustMov; foundMovie = foundMovie->Mnext);
if (foundMovie && foundMovie->movl.movCode == forRentCustMov)
if (foundMovie->movl.movCopies != 0) {
cout << "Movie is available!\n" ;
cout << "Movie Title: " << foundMovie->movl.movTitle << '\n' ;
cout << "Genre: " << foundMovie->movl.movGenre << '\n' ;
cout << "Production: " << foundMovie->movl.movproduction << '\n' ;
cout << "Number of Copies: " << foundMovie->movl.movCopies << "\n\n" ;
} else
cout << "Movie is unavailable - No copies\n" ;
else
cout << "Movie not found\n " ;
}
void movieList::showMovieDetails(int forRentCustMov) const {
bool found {};
for (auto movDetailshow { Moviehead }; movDetailshow; movDetailshow = movDetailshow->Mnext)
if (movDetailshow->movl.movCode == forRentCustMov) {
found = true ;
cout << "Movie Title: " << movDetailshow->movl.movTitle << '\n' ;
cout << "Genre: " << movDetailshow->movl.movGenre << '\n' ;
cout << "Production: " << movDetailshow->movl.movproduction << '\n' ;
cout << "Number of Copies: " << movDetailshow->movl.movCopies << "\n\n" ;
}
if (!found)
cout << "Movie Code is not on the list!\n" ;
}
void movieList::rentListCustomer(int forListRentByCustomer) {
movieListNode* movnodePtr { Moviehead };
for (auto itr { custQueue.begin() }; itr != custQueue.end(); ++itr) {
if (itr->custCode == forListRentByCustomer) {
cout << "Customer Name: " << itr->custName << '\n' ;
cout << "Customer Address: " << itr->custAddress << '\n' ;
break ;
}
}
if (custMova.empty())
cout << endl << "\nThe Customer's list is empty!\n" ;
while (!custMova.empty()) {
rentMovbyC = custMova.top();
if (rentMovbyC.rentCustomerID == forListRentByCustomer)
if (rentMovbyC.movCopies2 > 0) {
cout << "Movies Rented by the customer:\n\n" ;
cout << "Movie Code: " << rentMovbyC.rentMovieCode << '\n' ;
while (movnodePtr)
if (movnodePtr->movl.movCode == rentMovbyC.rentMovieCode) {
rentMovbyC.rentMovName = movnodePtr->movl.movTitle;
break ;
} else
movnodePtr = movnodePtr->Mnext;
cout << "Movie Title: " << rentMovbyC.rentMovName << "\n\n" ;
custMovb.push(rentMovbyC);
custMova.pop();
} else
cout << "List of Movies Rented is empty!\n" ;
else {
custMovb.push(rentMovbyC);
custMova.pop();
}
}
while (!custMovb.empty()) {
rentMovbyC = custMovb.top();
custMovb.pop();
custMova.push(rentMovbyC);
}
}
Last edited on Jul 6, 2022 at 12:11pm UTC