#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Movie{
private:
string movie_name;
int seats_avaiable;
double ticket_price;
public:
//declaring static variables
static const string DEFAULT_MOVIE_NAME;
static const int DEFAULT_SEATS_AVAILABLE;
static const double DEFAULT_TICKET_PRICE;
public:
//declaring constructors and deconstructor
Movie();
Movie(string movie_name, int seats_avaiable, double ticket_price);
~Movie();
//declaring accessors and mutators
string get_movie_name() const;
int get_seats_avaiable() const;
double get_ticket_price() const;
void set_movie_name(string new_movie_name);
void set_seats_avaiable(int new_seats_avaiable);
void set_ticket_price(double new_ticket_price);
//declaring behavior functions
double PurchaseTicket(int number_of_tickets);
void Display() const;
};
class MovieTicketMaster{
private:
//declaring class instance variables
string theater_name;
string theater_location;
int movie_count;
Movie* movie_list;
public:
//declaring static variables
static const int MOVIE_LIST_SIZE = 32;
static const string DEFAULT_THEATER_NAME;
static const string DEFAULT_THEATER_LOCATION;
static const int DEFAULT_MOVIE_COUNT;
public:
MovieTicketMaster();
MovieTicketMaster(string theater_name, string theater_location, int movie_count);
~MovieTicketMaster();
//declaring accessors and mutators
string get_theater_name() const;
string get_theater_location() const;
int get_movie_count() const;
void set_theater_name(string new_theater_name);
void set_theater_location(string new_theater_location);
void set_movie_count(int new_movie_count);
//declaring behavior functions
void Init();
void Run() ;
private:
//declaring helper functions
void Menu();
void ViewShowingMovies();
void SearchMovie();
void PurchaseTicket();
Movie* FindMovie(string movie);
void BubbleSort();
};
int main(){
//Dynamically allocating a MovieTicketMaster object
MovieTicketMaster* p_movie_ticket_master = new MovieTicketMaster("Oakridge Theaters", "San Jose", 8);
//Running Program
p_movie_ticket_master->Init();
p_movie_ticket_master->Run();
//Free memory for MovieTicketMaster object.
delete p_movie_ticket_master;
return 0;
}
//========================================= Movie Class DEFINTIONS ===========================================
//initializing static data
const string Movie::DEFAULT_MOVIE_NAME = "Mystery Movie";
const int Movie::DEFAULT_SEATS_AVAILABLE = 100;
const double Movie::DEFAULT_TICKET_PRICE = 14.99;
//initializing constructors and deconstructor
Movie::Movie(): movie_name(DEFAULT_MOVIE_NAME), seats_avaiable(DEFAULT_SEATS_AVAILABLE), ticket_price(DEFAULT_TICKET_PRICE) {}
Movie::Movie(string movie_name, int seats_avaiable, double ticket_price){
this->movie_name = movie_name;
this->seats_avaiable = seats_avaiable;
this->ticket_price = ticket_price;
}
Movie::~Movie(){
cout << "Movie " << movie_name << " is no longer showing ..." << endl;
}
//initializing accessors and mutators
string Movie::get_movie_name() const { return movie_name; }
int Movie::get_seats_avaiable() const { return seats_avaiable; }
double Movie::get_ticket_price() const { return ticket_price; }
void Movie::set_movie_name(string new_movie_name) {movie_name = new_movie_name;}
void Movie::set_seats_avaiable(int new_seats_available) {seats_avaiable = new_seats_available;}
void Movie::set_ticket_price(double new_ticket_price) {ticket_price = new_ticket_price;}
//initializing behavior functions
double Movie::PurchaseTicket(int number_of_tickets){
if(number_of_tickets > seats_avaiable) { return -1; }
seats_avaiable -= number_of_tickets;
return (number_of_tickets * ticket_price);
}
void Movie::Display() const{
setfill("=");
cout << setw(30) << " " << endl;
cout << "Movie Name: " << movie_name << endl;
cout << "Amount of Seats Available: " << seats_avaiable << endl;
cout << "Ticket Price: " << ticket_price << endl;
cout << setw(30) << " " << endl;
}
// ========================================= MovieTicketMaster Class DEFINTIONS ===========================================
//initializing static data
const string MovieTicketMaster::DEFAULT_THEATER_NAME = "AMC";
const string MovieTicketMaster::DEFAULT_THEATER_LOCATION = "My City";
const int MovieTicketMaster::DEFAULT_MOVIE_COUNT = 0;
//initializing constructors and deconstructor
MovieTicketMaster::MovieTicketMaster(): theater_name(DEFAULT_THEATER_NAME), theater_location(DEFAULT_THEATER_LOCATION), movie_count(DEFAULT_MOVIE_COUNT) {movie_list = new Movie[MOVIE_LIST_SIZE];};
MovieTicketMaster::MovieTicketMaster(string theater_name, string theater_location, int movie_count): theater_name(theater_name), theater_location(theater_location), movie_count(movie_count) {movie_list = new Movie[MOVIE_LIST_SIZE];};
MovieTicketMaster::~MovieTicketMaster(){
cout << "Theater " << theater_name << " is currently closed" << endl;
delete[] movie_list;
}
//initializing accessors and mutators
string MovieTicketMaster::get_theater_name() const{ return theater_name; }
string MovieTicketMaster::get_theater_location() const { return theater_location; }
int MovieTicketMaster::get_movie_count() const { return movie_count; }
void MovieTicketMaster::set_theater_name(string new_theater_name) { theater_name = new_theater_name; }
void MovieTicketMaster::set_theater_location(string new_theater_location) { theater_location = new_theater_location;}
void MovieTicketMaster::set_movie_count(int new_movie_count) { movie_count = new_movie_count; }
//initializing behavior functions
void MovieTicketMaster::Init(){
const int kSize = 8;
string movie_name_list[kSize] =
{
"Kung Fu Panda 4",
"The First Omen",
"Monkey Man",
"Migration",
"Arthur The King",
"Godzilla X Kong: The New Empire",
"Imaginary",
"Epic Tails"
};
int seat_available_list[kSize] = {4, 6, 13, 19, 2, 7, 12, 9};
double price_list[kSize] = {12.5, 11.3, 9.5, 4.0, 3.8, 10.0, 11.9, 14.5};
Movie* p_movie_object = &movie_list[0];
string* p_movie_name_list = &movie_name_list[0];
int* p_seat_available_list = &seat_available_list[0];
double* p_price_list = &price_list[0];
for(int i = 0;i<kSize; i++){
p_movie_object->set_movie_name(*p_movie_name_list);
p_movie_object->set_seats_avaiable(*p_seat_available_list);
p_movie_object->set_ticket_price(*p_price_list);
movie_list[i] = *p_movie_object;
//Move on to next value in list
++p_movie_name_list;
++p_seat_available_list;
++p_price_list;
//Move to next Movie object
++p_movie_object;
}
for(int i = kSize;i<MOVIE_LIST_SIZE; i++){
//setting the rest of the indicies to empty objects
p_movie_object->set_movie_name("");
p_movie_object->set_seats_avaiable(0);
p_movie_object->set_ticket_price(0.0);
movie_list[i] = *p_movie_object;
}
movie_count = 8;
//sorting the movies
BubbleSort();
}
void MovieTicketMaster::Run(){
bool flag = true;
int user_input;
do{
Menu();
cout<< "What action will you like to take (choose 1,2,3, or 4)? ";
cin >> user_input;
cin.ignore();
switch(user_input){
case 1:
ViewShowingMovies();
break;
case 2:
SearchMovie();
break;
case 3:
PurchaseTicket();
break;
case 4:
flag = false;
break;
default:
break;
}
}while(flag);
}
//intializing helper functions
void MovieTicketMaster::Menu(){
//initial welcome message
cout << "MOVIE TICKET MASTER" << endl;
cout << "Theater: " << theater_name << " at " << theater_location << endl;
cout << "Experience the true cinema experience!\nBuy tickets before its too late!" << endl;
//Print Options
cout << "1.View all showing movies" << endl;
cout << "2. Search a movie" << endl;
cout << "3.Purchase a ticket" << endl;
cout << "4.Quit" << endl;
}
void MovieTicketMaster::ViewShowingMovies(){
Movie* p_movie = &movie_list[0];
for(int i = 0; i < movie_count; i++){
p_movie->Display();
++p_movie;
}
}
void MovieTicketMaster::SearchMovie(){
string user_input;
Movie* returned_value;
cout << "Input a movie name to search for: ";
getline(cin, user_input);
returned_value = FindMovie(user_input);
if(returned_value == nullptr) { cout << "ERROR, movie not found!" << endl; } // fix this
else { returned_value->Display(); }
}
Movie* MovieTicketMaster::FindMovie(string target_movie){
Movie* p_movie = nullptr;
int top_index = DEFAULT_MOVIE_COUNT -1;
int bot_index= 0;
int middle_index = (top_index-bot_index)/2;
while(bot_index <= top_index){
middle_index = (top_index-bot_index)/2;
if(movie_list[middle_index].get_movie_name() == target_movie){
p_movie = &movie_list[middle_index];
break;
}
if(movie_list[middle_index].get_movie_name().compare(target_movie) > 0){
top_index = middle_index -1;
}
if(movie_list[middle_index].get_movie_name().compare(target_movie) < 0){
bot_index = middle_index +1;
}
}
return p_movie;
}
void MovieTicketMaster::BubbleSort(){
const int kSize = movie_count;
Movie temp = movie_list[0];
for(int x = 0; x<kSize-1; x++){
for(int y = 0; y<kSize-x-1;y++){
if((movie_list[y].get_movie_name().compare(movie_list[y+1].get_movie_name())) > 0)
{
temp = movie_list[y];
movie_list[y] = movie_list[y+1];
movie_list[y+1] = temp;
}
}
}
}
void MovieTicketMaster::PurchaseTicket(){
string user_input;
double total_cost = 0.0;
int num_tickets = 0;
cout << "Enter the name of the Movie you want to buy: ";
getline(cin, user_input);
Movie* p_movie = FindMovie(user_input);
if(p_movie == nullptr) { cout << "The movie you entered doesn't exist, sorry!" << endl; }
else{
cout << "How many tickets would you like to purchase? ";
cin >> num_tickets;
cin.ignore();
total_cost = p_movie->PurchaseTicket(num_tickets);
if(total_cost == -1){
cout << "The show has been sold out or not enough tickets available per your request!" << endl;
}
else{
cout << "Thats costed you $" << total_cost << endl;
}
}
}