ticket booth base level 3 1. in the completed program, the ticket seller will: select a venue from a...
Post on 13-Dec-2015
216 Views
Preview:
TRANSCRIPT
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 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
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
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
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
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
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
End of Run
8
Ticket_Booth.h
9
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
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
Using an STL map
12
Comment out everything that doesn't compile.
Add #include
Booking.h
We need an accessor method for the show.
Add to public section:const Show* Get_Show() const {return show;};
13
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!
Accessor Method for Bookings
Add to Ticket_Booth.h:
const std::map<string, Booking*>* Get_Bookings() const
{
return &bookings;
};
15
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
Bookings for The Little Theater
17
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
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
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
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
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
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
main.cpp
Modify section after Bookings_from_XML::Get_Bookings() as shown on the next slide
24
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();
}
}
End of Run
26
top related