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.

Slides:



Advertisements
Similar presentations
Chapter 21 The STL (maps and algorithms) Bjarne Stroustrup
Advertisements

The TicketBooth System Use Case Diagram and Scenarios.
Brown Bag #2 Advanced C++. Topics  Templates  Standard Template Library (STL)  Pointers and Smart Pointers  Exceptions  Lambda Expressions  Tips.
. STL: C++ Standard Library (continued). STL Iterators u Iterators are allow to traverse sequences u Methods  operator*  operator->  operator++, and.
1 STL Map & Multimap Ford & Topp Chapter 11 Josuttis Sections: 6.5 & 6.6 CSE Lecture 15 – Maps.
C++ Templates. What is a template? Templates are type-generic versions of functions and/or classes Template functions and template classes can be used.
Beginning C++ Through Game Programming, Second Edition
STL. What is STL? Standard Templates Library Templates are best used for –Defining containers for storing data –Some kinds of algorithms that work the.
More on the STL vector list stack queue priority_queue.
CMSC 202 Lesson 24 Iterators and STL Containers. Warmup Write the class definition for the templated Bag class – A bag has: Random insertion Random removal.
Templates and the STL.
CSIS 123A Lecture 12 Templates. Introduction  C++ templates  Allow very ‘general’ definitions for functions and classes  Type names are ‘parameters’
C++ Exceptions STL Vector. Example int Quotient (int numer, int denom} { if (denom != 0) return (numer/denom); else //What to do?? }
1 Arrays Chapter 13 Especially read 13.1 – 13.2 Text introduces vectors, which we will not cover, in 13.3.
1 Chapter 3 Lists, Stacks, and Queues Abstract Data Types, Vectors Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Object Oriented Programming Elhanan Borenstein Lecture #11 copyrights © Elhanan Borenstein.
DATA STRUCTURES AND ALGORITHMS Lecture Notes 12 Prepared by İnanç TAHRALI.
CS 11 C++ track: lecture 7 Today: Templates!. Templates: motivation (1) Lots of code is generic over some type Container data types: List of integers,
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Introduction to STL and the vector container class CS342 Data Structures Based on Ford & Topp.
Main Index Contents 11 Main Index Contents Week 3 – The Vector Container.
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
1 Using XML. 2 The Ticket Booth System We need a way to retain information between program runs. In real life, we would probably use a database system.
1 Linked Stack Chapter 4. 2 Linked Stack We can implement a stack as a linked list. Same operations. No fixed maximum size. Stack can grow indefinitely.
STL multimap Container. STL multimaps multimaps are associative containers –Link a key to a value –AKA: Hashtables, Associative Arrays –A multimap allows.
11 Introduction to Object Oriented Programming (Continued) Cats.
CSC 107 – Programming For Science. Today’s Goal  Become familiar with simple arrays  Declaring an array variable  Assigning data to array entries 
Iterator for linked-list traversal, Template & STL COMP171 Fall 2005.
Lecture 11 Standard Template Library Lists Iterators Sets Maps.
1 Joe Meehean.  List of names  Set of names  Map names as keys phone #’s as values Phil Bill Will Phil Bill Will Phil Bill Will Phil: Bill:
Standard C++ Library Part II. Last Time b String abstraction b Containers - vector.
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes.
Project 3: Ticket Printer
1 Linked Lists II Doubly Linked Lists Chapter 3. 2 Objectives You will be able to: Describe, implement, and use a Doubly Linked List of integers.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
CS Class 19 Today  Practice with classes Announcements  Turn in algorithm for Project 5 in class today  Project 5 due 11/11 by midnight – .
1 Associative Containers Ordered Ordered Unordered UnorderedSets Maps as sets of pairs Set API Ex: Sieve of Eratosthenes Ex: Sieve of EratosthenesImplementation.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Learning Objective  Standard Template Library.
1 Huffman Codes Drozdek Chapter Encoding Next we will add the capability to encode a message entered as normal text. The Huffman Tree that we use.
1 Project 1: Tickets. 2 Class Ticket Write a definition for class Ticket. A Ticket object corresponds to a physical ticket for a stage show. Attributes.
11 Introduction to Object Oriented Programming (Continued) Cats.
1 Chapter 3 Lists, Stacks, and Queues Reading: Sections 3.1, 3.2, 3.3, 3.4 Abstract Data Types (ADT) Iterators Implementation of Vector.
Struct s (7.4) Used as data aggregates for an entity can be different types of data e.g. for student id, name, GPA, address,... Similar to classes, but.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
1 Derived Classes Chapter Objectives You will be able to: Create and use derived classes. Understand the meaning of polymorphism and how it works.
1 The Standard Template Library Drozdek Section 3.7.
1 Implementing Ticket Printer. Class Diagram 2 Dependencies A class that contains objects of another class, or has a reference to another class, depends.
Multi-dimensional Array 1 Multi-dimensional array refers to an array with more than one index. It is a logical representation. On physical storage, the.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
1 Lecture 4: Part1 Arrays Introduction Arrays  Structures of related data items  Static entity (same size throughout program)
Main Index Contents 11 Main Index Contents Sets Defined by a key along with other data Sets Defined by a key along with other data Key-Value Data Key-Value.
1 Implementing Ticket Printer. Download project from last class Downloads/2016_02_12_In_Class/
CPSC 252 Tables / Maps / Dictionaries Page 1 Tables, Maps and Dictionaries A table (or map or dictionary) is a collection of key/value pairs. In general.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Function Templates 16.2.
Unit VI.  C++ templates are a powerful mechanism for code reuse, as they enable the programmer to write code (classes as well as functions) that behaves.
Using XML. The Ticket Booth System We need a way to retain information between program runs. In real life, we would probably use a database system for.
1 Using an XML Parser. 2 Objective You will be able to use a publically available open source parser to convert an XML file into an internal data structure.
1 Project 7: Ticket_Booth_0 Using XML. 2 Ticket Booth 0 Base Level 0 of the real Ticket Booth program. Gets information for a single venue from an XML.
Regarding homework 9 Many low grades
C++ Templates.
Standard Template Library (STL)
Abstract Data Types Iterators Vector ADT Sections 3.1, 3.2, 3.3, 3.4
Associative Structures
Collections Not in our text.
Lists - I The List ADT.
Lists - I The List ADT.
Iterators and STL Containers
STL and Example.
Chapter 3 Lists, Stacks, and Queues
Presentation 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 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 Downloads/2011_04_27_Ticket_Booth_3/ 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; } 14 String used like an array index!

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

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

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 bookings; std::map > bookings;... In public section: const std::map >* 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 >* 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(); }

End of Run 26