C++ - Theatre Seat Reservation

How to not sell one seat to two people.

Header File

This header file contains the SeatNode structure and the TheatreList class. These will hold all data related to the seat.

#include <string>
#include <iomanip>
#include <iostream>
#include <cassert>
using namespace std;

//STEVEN BARTSCH
//COMP 333
//ASSIGNMENT 3

struct SeatNode{
    string name;
    int seatNumb;
    SeatNode*link;
};

class TheatreList{
    public:
        TheatreList(int /*numb of seats*/, string /*concert title*/);
        ~TheatreList();
        void showReservations(ostream& /*ouput*/)const;
        bool searchSeat(int/*seat number*/);
        void reserveSeat();
        void removeSeat(int/*seat number*/);
        int getNoSold()const{return soldSeats;}
        int isSoldOut()const{return soldSeats==maxSeats;}
        string getConcertTitle()const{return concertTitle;}
        friend ostream & operator << (ostream& /*output*/, const TheatreList& /*class*/);
    private:
        SeatNode *firstPtr;
        string concertTitle;
        int maxSeats, soldSeats;
};

Concert Registration

Register the number of seats and concert title.

TheatreList::TheatreList(int newMaxSeats, string newConcertTitle){
    firstPtr = NULL;
    soldSeats = 0;
    while(newMaxSeats < 0){
        cout << "Seats can't be less than zero. Re-enter: ";
        cin >> newMaxSeats;
    }
    maxSeats = newMaxSeats;

    while(newConcertTitle.empty()){
        cout << "Concert Title cannot be blank. Re-enter: ";
        getline(cin,newConcertTitle);
    }

    cout << endl << "Concert " << newConcertTitle << " was created succesfully... " << endl;
    concertTitle = newConcertTitle;
}

Pointer Management

Check the first and empty pointers.

TheatreList::~TheatreList(){
    SeatNode*walker=firstPtr;
    while(walker!=NULL){
        firstPtr=walker->link;
        delete walker;
        walker=firstPtr;
    }
}

Show Reservations

Show all of the reservations.

void TheatreList::showReservations(ostream&out)const{
    //Display Title
    cout << endl << endl << "Reservations: " << endl;

    SeatNode *walker = firstPtr;
    out << left << "Concert Title: "<< concertTitle << endl << setw(20) << "Name" << setw(10) << "Seat" << endl;
    while (walker!=NULL){
        out << left << setw(20) << walker->name << right << setw(10) << walker->seatNumb << endl;
        walker=walker->link;
    }
}

Search Seats

Check to see if the seat is taken or not.

bool TheatreList::searchSeat(int newSeatNumber){
    //Display Title
    cout << endl << endl << "Search Seat: " << endl;

    bool isTaken = false;
    SeatNode *walker=firstPtr, *stalker=NULL;
    while(walker!=NULL){
        if(newSeatNumber==walker->seatNumb){
            isTaken = true;
            break;
        }
        stalker=walker;
        walker=walker->link;
    }
    cout << "Seat #" << newSeatNumber << " ";
    if(!isTaken)
        cout << "is available";
    else
        cout << "not available";
    cout << endl;

    delete walker, stalker;
    return isTaken;
}

Reserve Seats

Reserve a seat number and the name of the person.

void TheatreList::reserveSeat(){
    //Display Title
    cout << endl << endl << "Reserve Seat: " << endl;

    string newName;
    int newSeat;
    SeatNode *stalker=NULL, *walker=firstPtr;
    SeatNode *builder=new SeatNode;

    //user input
    cout << "Enter name: ";
    getline(cin,newName);
    cout << "Enter seat: ";
    cin >> newSeat;
    cin.clear();
    cin.ignore(80,'\n');

    //while loop to validate
    while(newSeat < 0 && newSeat > maxSeats){
        cout << "Invalid seat number. Must be greater than zero and less than " << maxSeats << endl;
        cout << "Enter seat: ";
        cin >> newSeat;
        cin.clear();
        cin.ignore(80,'\n');
    }

    assert(builder!=0);
    builder->link=NULL;
    builder->name=newName;
    builder->seatNumb=newSeat;
    soldSeats++;

    if(firstPtr==NULL){
        firstPtr=builder;
    }else{
        while(walker!=NULL){
            stalker=walker;
            walker=walker->link;
        }
        stalker->link=builder;
    }
}

Cancel Reservation

When a seat needs to be cancelled.

void TheatreList::removeSeat(int newSeatNumber){
    //Display Title
    cout << endl << endl << "Remove Seat: ";

    SeatNode *walker=firstPtr, *stalker=NULL;
    while(walker!=NULL){
        if(newSeatNumber==walker->seatNumb)
            break;
        stalker=walker;
        walker=walker->link;
    }
    if(walker==NULL)
        cout << "Seat cannot be removed since it hasn't been taken" << endl;
    else{
        if(stalker==NULL)
            firstPtr=walker->link;
        else{
            stalker->link=walker->link;
            delete walker;
        }
        cout << "Seat found and has been removed" << endl;
    }
}

Operator

ostream& operator<<(ostream&out,const TheatreList&object){
    out << "Concert Title: " << object.getConcertTitle() << "\t\tSold Seats: " << object.getNoSold() << endl;
    return out;
}

Client Code

Here is a sample of the client's end. Call the TheatreList class with the pointer and start the function calls.

#include "theatreList.h"

//STEVEN BARTSCH
//COMP 333
//ASSIGNMENT 3

int main(){
    TheatreList * ptr = new TheatreList(2,"Daughtry");
    assert(ptr!=NULL);

    for(int ctr=0;ctr<2;ctr++){
        ptr->showReservations(cout);
        ptr->searchSeat(2);
        ptr->reserveSeat();
        ptr->removeSeat(1);

        cout << "Number of sold seats: " << ptr->getNoSold() << endl;

        cout << "Concert sold out?: ";
        if(ptr->isSoldOut()==0)
            cout << "no";
        else
            cout << "yes";
        cout << endl;

        cout << "Concert title: " << ptr->getConcertTitle() << endl;
    }
    delete ptr;
    system("pause");
}