Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ticket Booth Base Level 3 1. In the completed program, the ticket seller will: Select a venue from a menu of all venues. Select a show from a menu of.

Similar presentations


Presentation on theme: "Ticket Booth Base Level 3 1. In the completed program, the ticket seller will: Select a venue from a menu of all venues. Select a show from a menu of."— Presentation transcript:

1 Ticket Booth Base Level 3 1

2 In the completed program, the ticket seller will: Select a venue from a menu of all venues. Select a show from a menu of shows booked for the selected venue. Select a performance from a menu of performances scheduled for the selected show. Display available seats for the selected performance, with ticket prices. Select an available seat to sell the ticket. 2

3 Bookings When a venue has been selected, we will need to iterate over Bookings for the selected Venue in order to display the shows available at that venue. Iterate over Performances of the selected show at the selected venue in order to display the available performances. How should we organize the Bookings? 3

4 Bookings Let's use an STL map. The STL map is an associative array. A collection of (key,value) pairs. Store and retrieve values identified by their keys. Functionally like a vector where the index does not have to be an integer. We can parameterize the map to use any type as the key. Use the key like an array index. 4

5 The STL map Template The map template takes two parameters: Class of key Class of the value Example: A map of Bookings indexed by show name: map bookings_for_selected_venue; We can use objects of the key class like index values for an array. Strings in this example. 5

6 Using a Map We could store all Bookings for a given Venue in a map indexed by show name. Retrieve the booking for a specified show by using the show name as an index. Booking* booking = bookings_for_selected_venue["The Music Man"]; OR Booking* booking = bookings_for_selected_venue[selected_show.Name()]; Let's try it! 6

7 Download http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/ Downloads/2011_04_27_Ticket_Booth_3/ http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/ Downloads/2011_04_27_Ticket_Booth_3/ File Ticket_Booth_3.zip This base level handles multiple venues, shows, and bookings. Extract, build, and run. 7

8 End of Run 8

9 Ticket_Booth.h 9

10 Class Ticket_Booth Ticket_Booth currently holds the Bookings as a vector of vectors. A vector of Booking* for each Venue. Retrieve by integer index. For each Venue, there is a vector of Bookings. One Booking for each Show that is currently booked for the venue. Retrieve by integer index. We will replace the vectors with maps. 10

11 Using an STL map Let's start with Bookings for a single venue. Create a map indexed by Show name. Will include the Bookings for all shows booked at that venue. 11

12 Using an STL map 12 Comment out everything that doesn't compile. Add #include

13 Booking.h We need an accessor method for the show. Add to public section: const Show* Get_Show() const {return show;}; 13

14 Ticket_Booth.cpp Update Add_Booking For now, only the first venue. Ignore the others. void Ticket_Booth::Add_Booking(int venue_index, Booking* b) { // bookings[venue_index]->push_back(b); if (venue_index > 0) { return; } string show_name = b->Get_Show()->Name(); bookings[show_name] = b; } 14 String used like an array index!

15 Accessor Method for Bookings Add to Ticket_Booth.h: const std::map * Get_Bookings() const { return &bookings; }; 15

16 Display the Bookings Add to main // Display bookings for first venue const map * bookings = Ticket_Booth::Instance()->Get_Bookings(); map ::const_iterator begin = bookings->begin(); map ::const_iterator end = bookings->end(); map ::const_iterator i; for (i = begin; i != end; ++i) { cout << endl; pair p = *i; Booking* b = p.second; b->Display(); } 16

17 Bookings for The Little Theater 17

18 Bookings for All Venues We have Bookings for the first venue in the Ticket_Booth singleton class. How should we store Bookings for all venues? Would like to use the name of the Venue to retrieve its Bookings. How about a map? 18

19 Bookings for All Venues We can make the current Bookings map the value member of a map indexed by Venue name. Using a Venue name as the index value retrieve a map containing all of the bookings for that Venue. 19

20 Ticket_Booth.h In protected section: //std::map bookings; std::map > bookings;... In public section: const std::map >* Get_Bookings() const { return &bookings; };... Comment out everything that causes a compile error. 20

21 Change Ticket_Booth::Add_Venue to specify venue by name rather than by index. Ticket_Booth.h //void Add_Booking(int venue_index, Booking* b); void Add_Booking(string venue_name, Booking* b); 21

22 Ticket_Booth.cpp void Ticket_Booth::Add_Booking(string venue_name, Booking* b) { string show_name = b->Get_Show()->Name(); bookings[venue_name][show_name] = b; } 22

23 Bookings_from_XML.cpp At end of Get_Bookings: while (booking_node != 0) { Booking* booking = Bookings_from_XML::Get_Booking(booking_node); const Venue* v = booking->Get_Venue(); string venue_name = v->Name(); Ticket_Booth::Instance()->Add_Booking(venue_name, booking); booking_node = booking_node->NextSibling(); } 23

24 main.cpp Modify section after Bookings_from_XML::Get_Bookings() as shown on the next slide 24

25 25 // Display bookings for all venues. const map >* all_bookings = Ticket_Booth::Instance()->Get_Bookings(); map >::const_iterator begin = all_bookings->begin(); map >::const_iterator end = all_bookings->end(); map >::const_iterator j; for (j = begin; j != end; ++j) { pair > p2 = *j; map * bookings = &p2.second; // Display bookings for current venue. map ::const_iterator begin = bookings->begin(); map ::const_iterator end = bookings->end(); map ::const_iterator i; for (i = begin; i != end; ++i) { cout << endl; pair p = *i; Booking* b = p.second; b->Display(); }

26 End of Run 26


Download ppt "Ticket Booth Base Level 3 1. In the completed program, the ticket seller will: Select a venue from a menu of all venues. Select a show from a menu of."

Similar presentations


Ads by Google