Professional Documents
Culture Documents
MOVIE TICKET
MOVIE TICKET
#include <fstream>
#include <sstream>
#include <vector>
#include <iomanip>
#include <limits>
#include <string>
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
class Movie {
public:
int id;
string name;
string genre;
void inputMovieDetails() {
cout << endl;
cout << "Enter movie ID: ";
while (!(cin >> id)) {
cout << "Invalid input. Please enter a valid movie ID: ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
cout << "Enter movie name: ";
cin.ignore();
getline(cin, name);
cout << "Enter movie genre: ";
getline(cin, genre);
}
void editMovieDetails() {
cout << "Enter new movie name: ";
cin.ignore();
getline(cin, name);
cout << "Enter new movie genre: ";
getline(cin, genre);
}
};
class CustomerTicket {
public:
string customerName;
int movieId;
int seatNumber;
string showTime;
double ticketPrice;
void writeTicketToFile() {
ofstream ticketFile("customertickets.txt", ios::app);
ticketFile << customerName << " " << movieId << " " << seatNumber << " " <<
showTime << " " << ticketPrice << endl;
ticketFile.close();
}
};
bool login() {
string username, password;
cout << "LOGIN PAGE" << endl;
cout << "Enter username: ";
cin >> username;
cout << "Enter password: ";
cin >> password;
return (username == ADMIN_USERNAME && password == ADMIN_PASSWORD);
}
void addMovie() {
clearScreen();
ofstream movieFile("moviedetails.txt", ios::app);
Movie movie;
cout << "ADD A MOVIE" << endl;
movie.inputMovieDetails();
movieFile << movie.id << " " << movie.name << " " << movie.genre << endl;
movieFile.close();
void viewMovies() {
clearScreen();
cout << "LIST OF MOVIES" << endl;
ifstream movieFile("moviedetails.txt");
Movie movie;
string line;
cout << left << setw(10) << "ID" << setw(30) << "Name" << setw(20) << "Genre"
<< endl;
while (getline(movieFile, line)) {
stringstream ss(line);
ss >> movie.id >> movie.name >> movie.genre;
movie.displayMovie();
}
movieFile.close();
}
void editMovie() {
clearScreen();
viewMovies();
int id;
cout << "Enter the ID of the movie to edit: ";
cin >> id;
ifstream movieFile("moviedetails.txt");
ofstream tempFile("temp.txt");
Movie movie;
string line;
bool found = false;
movieFile.close();
tempFile.close();
if (!found) {
cout << "Movie not found!" << endl;
}
else {
remove("moviedetails.txt");
rename("temp.txt", "moviedetails.txt");
cout << "Movie details updated successfully!" << endl;
}
}
void deleteMovie() {
clearScreen();
int id;
cout << "Enter the ID of the movie to delete: ";
cin >> id;
ifstream movieFile("moviedetails.txt");
ofstream tempFile("temp.txt");
Movie movie;
string line;
bool found = false;
movieFile.close();
tempFile.close();
if (!found) {
cout << "Movie not found!" << endl;
}
else {
remove("moviedetails.txt");
rename("temp.txt", "moviedetails.txt");
cout << "Movie deleted successfully!" << endl;
}
}
void bookTicket() {
clearScreen();
viewMovies();
CustomerTicket ticket;
cout << "Enter your name: ";
cin.ignore();
getline(cin, ticket.customerName);
if (choice == '1') {
ticket.writeTicketToFile();
cout << "Ticket booked successfully!" << endl;
}
else {
cout << "Booking cancelled." << endl;
}
}
void viewCustomerTickets() {
clearScreen();
cout << "CUSTOMER TICKETS" << endl;
ifstream ticketFile("customertickets.txt");
CustomerTicket ticket;
string line;
while (getline(ticketFile, line)) {
stringstream ss(line);
ss >> ticket.customerName >> ticket.movieId >> ticket.seatNumber >>
ticket.showTime >> ticket.ticketPrice;
ticket.displayTicket();
cout << "-------------------" << endl;
}
ticketFile.close();
}
int main() {
bool loggedIn = false;
do {
if (!login()) {
clearScreen();
cout << "Login failed. Incorrect username or password." << endl;
cout << "Press 1 to try again or any other key to exit: ";
char choice;
cin >> choice;
if (choice != '1') {
cout << "Exiting program." << endl;
return 1;
}
clearScreen();
}
else {
loggedIn = true;
}
} while (!loggedIn);
int choice;
do {
clearScreen();
cout << "Movie ticket Booking " << endl;
cout << "1. Add Movie" << endl;
cout << "2. View Movies" << endl;
cout << "3. Edit Movie" << endl;
cout << "4. Delete Movie" << endl;
cout << "5. Book Ticket" << endl;
cout << "6. View Customer Tickets" << endl;
cout << "7. Exit" << endl;
cout << "Enter your choice: ";
while (!(cin >> choice)) {
cout << "Invalid input. Please enter a valid choice: ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
switch (choice) {
case 1:
addMovie();
cout << "Press Enter to continue...";
cin.ignore();
cin.get();
break;
case 2:
viewMovies();
cout << "Press Enter to continue...";
cin.ignore();
cin.get();
break;
case 3:
editMovie();
cout << "Press Enter to continue...";
cin.ignore();
cin.get();
break;
case 4:
deleteMovie();
cout << "Press Enter to continue...";
cin.ignore();
cin.get();
break;
case 5:
bookTicket();
cout << "Press Enter to continue...";
cin.ignore();
cin.get();
break;
case 6:
viewCustomerTickets();
cout << "Press Enter to continue...";
cin.ignore();
cin.get();
break;
case 7:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
cout << "Press Enter to continue...";
cin.ignore();
cin.get();
break;
}
} while (choice != 7);
return 0;
}