26
Ticket Booth Base Level 3 1

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

Embed Size (px)

Citation preview

Page 1: 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

Ticket Booth Base Level 3

1

Page 2: 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

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

Page 3: 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

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

Page 4: 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

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

Page 5: 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

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<string, Booking*> bookings_for_selected_venue;

We can use objects of the key class like index values for an array. Strings in this example.

5

Page 6: 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

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

Page 7: 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

Download

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

Page 8: 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

End of Run

8

Page 9: 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

Ticket_Booth.h

9

Page 10: 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

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

Page 11: 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

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

Page 12: 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

Using an STL map

12

Comment out everything that doesn't compile.

Add #include

Page 13: 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

Booking.h

We need an accessor method for the show.

Add to public section:const Show* Get_Show() const {return show;};

13

Page 14: 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

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;

}

14String used like an array index!

Page 15: 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

Accessor Method for Bookings

Add to Ticket_Booth.h:

const std::map<string, Booking*>* Get_Bookings() const

{

return &bookings;

};

15

Page 16: 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

Display the Bookings

Add to main

// Display bookings for first venue

const map<string, Booking*>* bookings =

Ticket_Booth::Instance()->Get_Bookings();

map<string, Booking*>::const_iterator begin = bookings->begin();

map<string, Booking*>::const_iterator end = bookings->end();

map<string, Booking*>::const_iterator i;

for (i = begin; i != end; ++i)

{

cout << endl;

pair<string, Booking*> p = *i;

Booking* b = p.second;

b->Display();

}

16

Page 17: 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

Bookings for The Little Theater

17

Page 18: 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

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

Page 19: 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

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

Page 20: 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

Ticket_Booth.h

In protected section:

//std::map<string, Booking*> bookings;

std::map<string, std::map<string, Booking*> > bookings;

...

In public section:

const std::map<string, std::map<string, Booking*> >* Get_Bookings() const

{

return &bookings;

};

...

Comment out everything that causes a compile error.

20

Page 21: 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

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

Page 22: 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

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

Page 23: 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

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

Page 24: 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

main.cpp

Modify section after Bookings_from_XML::Get_Bookings() as shown on the next slide

24

Page 25: 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

25

// Display bookings for all venues.

const map<string, map<string, Booking*> >* all_bookings =

Ticket_Booth::Instance()->Get_Bookings();

map<string, map<string, Booking*> >::const_iterator begin = all_bookings->begin();

map<string, map<string, Booking*> >::const_iterator end = all_bookings->end();

map<string, map<string, Booking*> >::const_iterator j;

for (j = begin; j != end; ++j)

{

pair<string, map<string, Booking*> > p2 = *j;

map<string, Booking*>* bookings = &p2.second;

// Display bookings for current venue.

map<string, Booking*>::const_iterator begin = bookings->begin();

map<string, Booking*>::const_iterator end = bookings->end();

map<string, Booking*>::const_iterator i;

for (i = begin; i != end; ++i)

{

cout << endl;

pair<string, Booking*> p = *i;

Booking* b = p.second;

b->Display();

}

}

Page 26: 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

End of Run

26