Class Operations & Throw Exceptions

I am a little hung on on completing this program

The assessment asks us Following the information provided below, implement a RockConcert and TicketHolder class. The class TicketHolder represents someone who paid for admission a particular concert. This TicketHolder class should keep track of the what it cost and where the seat located.

I have constructed most of the code, given the Driver that was provided, but I am pretty stumped with how to go about coding methods RockConcert::sellTicket and, RockConcert::getTicket

Below is the code,

Thanks a ton!



**TicketHolder.h**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 #ifndef TicketHolder_h
    #define TicketHolder_h
    #include <string>
    #include <iostream>
    
    using namespace std;
    
    class TicketHolder {
    
    public:
        TicketHolder();
        TicketHolder( std::string seat, double cost );
        
        std::string getSeat() const;
        double getCost() const;
        
        friend std::ostream& operator<< (std::ostream& os, const TicketHolder & t);
        
    private:
        std::string my_Seat;
        double my_Cost;
    };
    
    #endif 


**TicketHolder.cpp**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "TicketHolder.h"
    
    
    TicketHolder::TicketHolder(){
        
    }
    TicketHolder::TicketHolder( std::string seat, double cost ){
        my_Seat = seat;
        my_Cost = cost;
    }
    
    std::string TicketHolder::getSeat() const{
        return (my_Seat);
    }
    
    double TicketHolder::getCost() const{
        return (my_Cost);
        
    }
    
    std::ostream& operator<< (std::ostream& os, const TicketHolder & t){
        return os;
    }


**RockConcert.h**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
 #ifndef RockConcert_h
    #define RockConcert_h
    #include <string>
    #include <iostream>
    #include <stdexcept>
    #include "TicketHolder.h"
    #include "SeatAlreadySold.h"
    #include "SeatNotFound.h"
    
    class RockConcert {
    
    public:
        public:
        RockConcert( std::string date, std::string location );
            
        void sellTicket( std::string seat, double cost ) throw (SeatAlreadySold);
        TicketHolder getTicket( std::string seat ) throw (SeatNotFound);
        double getProceeds( );
            
        friend std::ostream& operator<< ( std::ostream& out, const RockConcert & r );
    
    
    private:
            TicketHolder myTickets[ 20 ];
            int myNumberOfTicketsSeenSoFar;
            std::string my_When;
            std::string my_Where;
    
        };
    
    #endif 



**RockConcert.cpp**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 #include "RockConcert.h"
        #include "TicketHolder.h"
        
        RockConcert::RockConcert( std::string date, std::string location ) {
            my_When = date;
            my_Where = location;
        }
        
        void RockConcert::sellTicket( std::string seat, double cost ) throw (SeatAlreadySold){
        
        }
        
        TicketHolder RockConcert::getTicket( std::string seat ) throw(SeatNotFound) {
        
            
        }
        
        double RockConcert::getProceeds( ){
            return (myNumberOfTicketsSeenSoFar);
        
        }
        
        std::ostream& operator<< ( std::ostream& out, const RockConcert & r ){
            return out;
        }



**Driver.CPP**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 // driver
    // function main
    #include "TicketHolder.h"
    #include "RockConcert.h"
    #include <iostream>
    #include <stdexcept>
    
    int main(int argc, const char * argv[])
    
    {
    RockConcert megaDeath( "Sept 1", "Staples Center" );
    megaDeath.sellTicket( "Row Q Seat 20", 120.00 );
    megaDeath.sellTicket( "Row Q Seat 21", 120.00 );
    megaDeath.sellTicket( "Row SSS Seat 12", 50.00 );
    TicketHolder ticket = megaDeath.getTicket( "Row Q Seat 21" );
    
    // Print out the concert and its tickets
    std::cout << megaDeath << std::endl;
        
    
    try {
        
        
        megaDeath.sellTicket( "Row T Seat 30", 65.00 );
        
    } catch( SeatAlreadySold sas ) {
        
        std::cout << "An error occurred here for the reason:";
        std::cout << sas.what(std::endl:endl;
    }
    
    // Print out the library and its book - notice the difference
    std::cout << megaDeath << std::endl;
    // Let's try to throw some errors...
    try {
        // can't sell a ticket that has already been sold
        megaDeath.sellTicket( "Row Q Seat 21", 120.00 );
        std::cout << "An error occurred here..." ;
    } catch( SeatAlreadySold sas ) {
        sas.what();
    }
    
    try {
        // can't get a ticket that has not been sold
        megaDeath.getTicket( "Row A Seat 1" );
        std::cout << "An error occurred here..." ;
    } catch( SeatNotFound snf ) {
        snf.what();
    }
    
   }
how to go about coding methods RockConcert::sellTicket and, RockConcert::getTicket
In both cases you need a loop that goes to myNumberOfTicketsSeenSoFar. You can use the getSeat() member function of the TicketHolder in your array (like myTickets[i].getSeat() == seat) to determine whether the seat is used or not and throw accordingly
Topic archived. No new replies allowed.