Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

#include <iostream>

#include <fstream>
#include <sstream>
#include <vector>
#include <iomanip>
#include <limits>
#include <string>

using namespace std;

const string ADMIN_USERNAME = "movie_admin";


const string ADMIN_PASSWORD = "moviepassword123";
const double BASE_PRICE = 200.0;

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 displayMovie() const {


cout << left << setw(10) << id << setw(30) << name << setw(20) << genre <<
endl;
}

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 displayTicket() const {


cout << "Customer Name: " << customerName << endl;
cout << "Movie ID: " << movieId << endl;
cout << "Seat Number: " << seatNumber << endl;
cout << "Show Time: " << showTime << endl;
cout << "Ticket Price: Rs." << fixed << setprecision(2) << ticketPrice <<
endl;
}

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();

cout << "Movie added successfully!" << endl;


}

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;

while (getline(movieFile, line)) {


stringstream ss(line);
ss >> movie.id >> movie.name >> movie.genre;
if (movie.id == id) {
found = true;
movie.displayMovie();
movie.editMovieDetails();
}
tempFile << movie.id << " " << movie.name << " " << movie.genre << endl;
}

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;

while (getline(movieFile, line)) {


stringstream ss(line);
ss >> movie.id >> movie.name >> movie.genre;
if (movie.id == id) {
found = true;
}
else {
tempFile << movie.id << " " << movie.name << " " << movie.genre <<
endl;
}
}

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);

cout << "Enter the movie ID to book: ";


while (!(cin >> ticket.movieId)) {
cout << "Invalid input. Please enter a valid movie ID: ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

cout << "Enter the seat number (1-100): ";


while (!(cin >> ticket.seatNumber) || ticket.seatNumber < 1 ||
ticket.seatNumber > 100) {
cout << "Invalid input. Please enter a valid seat number (1-100): ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}

cout << "Enter show time (day/night): ";


cin.ignore();
getline(cin, ticket.showTime);

// Calculate ticket price


ticket.ticketPrice = BASE_PRICE;
if (ticket.seatNumber > 50) {
ticket.ticketPrice += 50;
}
if (ticket.showTime == "night") {
ticket.ticketPrice += 100;
}
clearScreen();
cout << "Ticket Details: " << endl;
ticket.displayTicket();
cout << "Press 1 to confirm booking or any other key to cancel: ";
char choice;
cin >> choice;

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;
}

You might also like