Design a set of classes that work together to simulate a customer buying tickets
ID: 3555311 • Letter: D
Question
Design a set of classes that work together to simulate a customer buying tickets for a music event The classes you will design are:
CUSTOMER - this class will have: the name of the customer and the amount of cash on hand
TICKETMASTER - this class will have: a service charge = $8.00 per ticket, tax = .085 current amount of all tickets sold. Its responsibilities are printing a list of events for sale, looking up an event for a customer, and selling a ticket to the event.
EVENT - this class has name of musical group, time of event, place of event number of tickets available price of single ticket Use the events file for data for all events
TICKET - this class will have: name of customer who purchased the ticket, the band name, the time and place of the concert, and the number tickets purchased along with total price of tickets (cost + tax + service charge)
When purchasing a ticket, a customer will need to have to have enough money and there will have to be enough available for that particular event.
Demonstrate your classes by creating:
1) A 1st customer with $100.00 cash who wants 1 ticket to Bob Dylan
2) A 2nd customer with 100.00 cash wants 2 tickets to R Kelly
3) A 3rd customer with $300 wants 2 tickets to Muse
4) A 4th customer with $50 wants 1 ticket to Bassnectar
Since I can't upload the events.dat file, I will copy and paste it below...
Muse
Oracle Arena
5
75.00
8:00
Foster the People
Greek Theater
50
35.00
8:00
Bassnectar
Bill Graham
20
50.00
8:00
Rihanna
HP Pavillion
20
45.00
7:30
Bob Dylan
Bill Graham
0
100.00
7:30
R Kelly
Paramount Theater
15
35.00
7:30
Jane's Addiction
Mondavi Center
40
30.00
8:30
Of Monsters and Men
Shoreline
25
40.00
8:00
Explanation / Answer
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
/********************************
* Class Event
*******************************/
class Event{
private:
string bandName;
string time;
string place;
int ticketAvailable;
float price;
public:
Event(string bn, string plc, int ticAvail, float prc, string tm){
bandName = bn;
time = tm;
place = plc;
ticketAvailable = ticAvail;
price = prc;
}
/*
* Getter Methods of all variables
*/
const string& getBandName() const {
return bandName;
}
const string& getPlace() const {
return place;
}
float getPrice() const {
return price;
}
int getTicketAvailable() const {
return ticketAvailable;
}
const string& getTime() const {
return time;
}
/**
* This fucntion decrement the number of tickets available. It is used by TicketMaster when a
* a ticket is bought
*/
void decrementTickets(int count){
ticketAvailable-=count;
}
void printEvent(){
cout << "Band Name : " << bandName << endl;
cout << "Place : " << place << endl;
cout << "Time : " << time << endl;
cout << "Tickets Available : " << ticketAvailable << endl;
cout << "Price : "<< price << endl;
}
};
/********************************
* Class Ticket
*******************************/
class Ticket{
private:
string custName;
string bandName;
string time;
string place;
int ticketCount;
float price;
public:
Ticket(string custN, const Event& ev, int ticketCnt, float prc){
custName = custN;
bandName = ev.getBandName();
time = ev.getTime();
place = ev.getPlace();
ticketCount = ticketCnt;
price = prc;
}
void printTicket(){
cout << "______ Ticket Details ______" << endl ;
cout << "Customer Name : " << custName << endl ;
cout << "Band Name : " << bandName << endl ;
cout << "Place : " << place << endl ;
cout << "Time : " << time << endl ;
cout << "Count : " << ticketCount << endl ;
cout << "Total Price : " << std::setprecision(4) << price << endl ;
}
};
/********************************
* Class TicketMaster
*******************************/
class TicketMaster{
private:
static const float SVC_CHARGE;
static const float TAX;
float amountCollected;
string error;
Event *events[100];
int eventCount;
public:
TicketMaster(char* eventsFileName){
amountCollected = 0.0;
error = "";
eventCount=0;
//Open the eventsFile and load all the event Details
ifstream eventsFile;
eventsFile.open(eventsFileName);
if( eventsFile.bad() ){
error = "Cannot open the file";
eventsFile.close();
return;
}
while( eventsFile.good() ){
char bandName[40];
char place [40];
char time[10];
float price;
int ticketsAvail;
eventsFile.getline(bandName, 40, ' ');
eventsFile.getline(place, 40, ' ');
eventsFile>>ticketsAvail;
eventsFile>> price;
eventsFile.get();
eventsFile.getline(time, 40, ' ');
events[eventCount++] = new Event( bandName, place, ticketsAvail, price, time);
}
}
/*
* Looks up for an event using bandName, returns the event if exist in list or NULL it it doesn't
* exist
*/
Event* lookupEvent(const string& bandName){
for( int i=0; i<eventCount; i++){
if( bandName == events[i]->getBandName())
return events[i];
}
return NULL;
}
/*
* Get the total price of the ticket
* Price = (ticketPrice + servicecharge) * count + 8.5% tax on it
*/
float getPrice(const string& bandName, int count){
const Event* ev = this-> lookupEvent(bandName);
if( ev == NULL )
return 0.0;
else{
float totalPrice = (SVC_CHARGE + ev->getPrice() )*count;
return totalPrice*(1+TAX);
}
}
/*
* Method for buy a ticket. Returns Ticket if successful else NULL and sets error accordingly
*/
Ticket* buyTicket(const string& bandName, const string& custName, int count){
Ticket* tic=NULL;
Event* ev = lookupEvent(bandName);
if( ev->getTicketAvailable() >= count ){
float totalPrice = getPrice(bandName, count);
amountCollected += totalPrice;
tic = new Ticket(custName, *ev, count, totalPrice);
ev->decrementTickets(count);
} else {
error = "Sufficient tickets not available";
}
return tic;
}
/*
* Prints the list of events
*/
void printEvents(){
cout << "_____________ Event List _______________" << endl;
for (int i=0; i<eventCount; i++){
cout << "Event # " << i+1 <<endl;
events[i]->printEvent();
cout << endl;
}
cout << "_________ End of Event list ____________" << endl;
}
const string& getError(){
return error;
}
float getAmountCollected() const {
return amountCollected;
}
};
const float TicketMaster::SVC_CHARGE = 8.00;
const float TicketMaster::TAX = 0.085;
class Customer{
private:
string name;
float cash;
Ticket *ticket;
public:
Customer(){
name = "";
cash = 0.0;
ticket = NULL;
}
Customer(string name, float cash){
this->name = name;
this->cash = cash;
ticket = NULL;
}
const string& getName(){
return name;
}
bool buy(string bandName, int count, TicketMaster& tm){
cout << endl <<"" << name << " trying to buy ticket for " << bandName << endl;
//Look up an event
const Event* ev = tm.lookupEvent(bandName);
if(ev == NULL ){
cout << "TicketMaster says: No such event" << endl;
return false;
}
//Check if sufficient tickets are available
if( ev->getTicketAvailable() < count ){
cout << "Sufficient tickets not available" << endl;
return false;
}
//Check if sufficient cash is present;
float totalPrice = tm.getPrice(bandName, count);
if( totalPrice >= cash){
cout << name << " don't have sufficient cash in hand, need $"<< (totalPrice - cash) <<
" more" << endl;
return false;
}
//Buy the ticket
cash -= totalPrice;
ticket = tm.buyTicket(bandName, name, count);
if( ticket == NULL){
cout << "TicketMaster says: " + tm.getError();
return false;
}
//Print the ticket details
cout << name << " got the ticket" << endl;
ticket->printTicket();
return true;
}
void printDetails(){
cout << "Customer Details of " << name << endl;
cout << "" << name << " got $" << cash << " in hand";
if( ticket != NULL){
cout << " and got ticket for " << endl;
ticket->printTicket();
} else {
cout << " and no ticket" << endl << endl;
}
}
};
int main() {
TicketMaster tm("events.txt");
Customer cust1 = Customer("Cust1", 100.00);
Customer cust2 = Customer("Cust2", 100.00);
Customer cust3 = Customer("Cust3", 300.00);
Customer cust4 = Customer("Cust4", 50.00);
Customer cust5 = Customer("Cust5", 50.00);
tm.printEvents();
cust1.printDetails();
cust1.buy("Bob Dylan", 1, tm);
cust2.buy("R Kelly", 1, tm);
cust3.buy("Muse", 1, tm);
cust4.buy("Bassnectar", 1, tm);
cout << endl;
cout << "Ticket Master collected total of "<< tm.getAmountCollected();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.