360 likes | 549 Views
Bookings. Download. Program at end of last class http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Downloads/2011_04_18_XML_Booking/ We will add singleton class Ticket_Booth. Container for all information about venues, shows, and bookings. Ticket_Booth. Venue. Show. Booking.
E N D
Download • Program at end of last class • http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Downloads/2011_04_18_XML_Booking/ • We will add singleton class Ticket_Booth. • Container for all information about venues, shows, and bookings.
Ticket_Booth Venue Show Booking Class Ticket_Booth Instance * * *
The STL Vector • The STL vector template is similar to an array • Used for the same purposes. • Avoids some of the limitations and problems. • Automatically allocates storage. • Can grow indefinitely • Provides random access with an index operator • Looks and acts like an array. • Use in class Ticket_Booth as container for venues, shows, and bookings
Class Ticket_Booth • Add class Ticket_Booth to the project. • Download: • http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Downloads/2011_04_20_Ticket_Booth/ • Ticket_Booth_Classes.zip • Expand • Copy Ticket_Booth.h and Ticket_Booth.cpp into project folder. • Add to project. • Build.
Ticket_Booth.h #pragma once #include <vector> #include <string> #include "Venue.h" #include "Show.h" class Ticket_Booth { protected: static Ticket_Booth* instance; std::vector<Venue*> venues; std::vector<Show*> shows; Ticket_Booth(void) {}; ~Ticket_Booth(void) {}; public: static Ticket_Booth* Instance(); void Add_Venue(Venue* v); void Add_Show(Show* s); Venue* Get_Venue(string name) const; Show* Get_Show(string name) const; }; Note protected constructor. Class cannot be instantiated from outside.
Ticket_Booth.cpp #include "Ticket_Booth.h" Ticket_Booth* Ticket_Booth::instance = 0; Ticket_Booth* Ticket_Booth::Instance() { if (instance == 0) { instance = new Ticket_Booth(); } return instance; } void Ticket_Booth::Add_Venue(Venue* v) { venues.push_back(v); } void Ticket_Booth::Add_Show(Show* s) { shows.push_back(s); }
Ticket_Booth.cpp (continued) Venue* Ticket_Booth::Get_Venue(string name) const { for (size_t i = 0; i < venues.size(); ++i) { if (venues[i]->Name() == name) { return venues[i]; } } return 0; } Show* Ticket_Booth::Get_Show(string name) const { for (size_t i = 0; i < shows.size(); ++i) { if (shows[i]->Name() == name) { return shows[i]; } } return 0; }
Ticket_Booth • Add code to main() to create a Ticket_Booth object and add the venue and show to it. • #include "Ticket_Booth.h"
main.cpp int main(void) { cout << "This is XML_Booking\n"; Venue* venue; ... venue = Venue_from_Xml::Get_Venue(venue_node); venue->Display_All(); Ticket_Booth::Instance()->Add_Venue(venue); Show* show = Get_Show(); show->Display(); Ticket_Booth::Instance()->Add_Show(show); cout << "Normal termination\n"; cin.get(); cin.get(); return 0; }
Price Spec Show Booking Performance Performance Venue Address Street Name City State Zip Code * Seat Row Name Nr Seats Section Name Price Level A booking must have a price spec for each seating section of its venue. Class Diagram 1 * 1 1 * 1 1 * 1 Name Date * * * * Time * * * Seat Price Schedule * Row Name Name 1 1 3 1 1 Price Section Name We have the Show We have the Venue. Now get the Booking
Getting the Booking • Start by adding class Performance. • Copy Performance.h and Performance.cpp into the project folder. • Add to project. • Build.
Add Class Performance Performance.h #pragma once #include <ctime> #include <string> #include "Venue.h" #include "Show.h" class Performance { public: private: const Show* show; const Venue* venue; tm when; string price_schedule;
Performance.h (continued) public: Performance() {}; Performance(Show* show_, Venue* venue_, tm when_, string price_schedule_); const Venue* Get_Venue() const {return venue;}; const Show* Get_Show() const {return show;}; tm Get_When() const {return when;}; string Get_Price_Schedule() const {return price_schedule;}; void Display() const; };
Performance.cpp #include <iostream> #include <string.h> #include "Performance.h" using namespace std; Performance::Performance(Show* show_, Venue* venue_, tm when_, string price_schedule_) : show(show_), venue(venue_), when(when_), price_schedule(price_schedule_) {}
Performance.cpp (continued) void Performance::Display() const { cout.fill('0'); cout << "Performance: "; cout << show->Name() << endl; cout << when.tm_mday << "/" << when.tm_mon+1 << "/" << when.tm_year + 1900; cout << " at "; cout.width(2); cout << when.tm_hour << ":"; cout.width(2); cout << when.tm_min << endl; venue->Display(); cout << "Price schedule: " << price_schedule << endl; }
Getting the Booking • Now add class Booking. • Copy Booking.h and Booking.cpp into the project folder. • Add to project. • Build.
Class Booking Booking.h #pragma once #include <vector> #include "Show.h" #include "Venue.h" #include "Performance.h" class Booking { public: struct Price_Spec { string section_name; int price_level; }; Note that this is a price level, not a price.
Booking.h (continued) private: const Show* show; const Venue* venue; std::vector<Performance*> performances; int number_of_performances; // Delete std::vector<Price_Spec> price_specs; int number_of_price_specs; // Delete public: Booking(void) {}; Booking (const Venue* venue_, const Show* show_); void Add_Performance(Performance* p) {performances.push_back(p);}; void Add_Price_Spec(Price_Spec pc) {price_specs.push_back(pc);}; void Display() const; };
Booking.cpp #include <iostream> #include "Booking.h" using namespace std; Booking::Booking (const Venue* venue_, const Show* show_) : venue(venue_), show(show_), number_of_performances(0), number_of_price_specs(0) {} void Booking::Display() const { cout << "Booking:\n"; cout << venue->Name() << endl; cout << show->Name() << endl; for (size_t i = 0; i < performances.size(); ++i) { cout << endl; performances[i]->Display(); } }
Getting the Booking • Finally add class Booking_from_XML • Copy Booking_from_XML.h and Booking_from_XML.cpp into the project folder. • Add to project. • Build.
Class Booking_from_XML Booking_from_XML.h #pragma once #include <ctime> #include "tinyxml.h" #include "Booking.h" #include "Performance.h" #include "Ticket_Booth.h" class Booking_from_XML { public: static Booking* Get_Booking(TiXmlNode* booking_node);
Booking_from_XML.h (continued) private: Booking_from_XML (void) {}; static Booking::Price_Spec Get_Price_Spec(TiXmlNode* price_spec_node); static Performance* Get_Performance(Venue* venue, Show* show, TiXmlNode* performance_node); static tm Get_Date_and_Time(TiXmlNode* date_node, TiXmlNode* time_node); };
Booking_from_XML::Get_Booking #include <cstdlib> #include <cassert> #include "Booking_from_XML.h" #include "Ticket_Booth.h" Booking* Booking_from_XML::Get_Booking(TiXmlNode* booking_node) { string show_name; string venue_name; TiXmlNode* show_name_node = booking_node->FirstChild(); assert(show_name_node != 0); show_name = show_name_node->FirstChild()->Value(); Show* show = Ticket_Booth::Instance()->Get_Show(show_name); assert(show != 0); TiXmlNode* venue_name_node = show_name_node->NextSibling(); assert(venue_name_node != 0); venue_name = venue_name_node->FirstChild()->Value(); Venue* venue = Ticket_Booth::Instance()->Get_Venue(venue_name); assert (venue != 0); Booking* booking = new Booking(venue, show);
Booking_from_XML::Get_Booking (continued) TiXmlNode* price_spec_node = booking_node->FirstChild("price_spec"); while ((price_spec_node != 0) && (price_spec_node->Value() == "price_spec")); { Booking::Price_Spec ps = Get_Price_Spec(price_spec_node); booking->Add_Price_Spec(ps); price_spec_node = price_spec_node->NextSibling(); } TiXmlNode* performance_node = booking_node->FirstChild("performance"); while (performance_node != 0) { Performance* p = Get_Performance(venue, show, performance_node); booking->Add_Performance(p); performance_node = performance_node->NextSibling(); } return booking; }
Booking_from_XML:: Get_Price_Spec Booking::Price_Spec Booking_from_XML::Get_Price_Spec(TiXmlNode* price_spec_node) { Booking::Price_Spec price_spec; string temp = price_spec_node->Value(); TiXmlNode* section_node = price_spec_node->FirstChild(); assert(section_node != 0); price_spec.section_name = section_node->FirstChild()->Value(); TiXmlNode* price_level_node = section_node->NextSibling(); assert(price_level_node != 0); price_spec.price_level = atoi(price_level_node->FirstChild()->Value()); return price_spec; }
Booking_from_XML:: Get_Performance Performance* Booking_from_XML::Get_Performance(Venue* venue, Show* show, TiXmlNode* performance_node) { TiXmlNode* date_node = performance_node->FirstChild("date"); TiXmlNode* time_node = performance_node->FirstChild("start_time"); tm when = Get_Date_and_Time(date_node, time_node); TiXmlNode* price_schedule_node = performance_node->FirstChild("price_schedule"); string price_schedule = price_schedule_node->FirstChild()->Value(); Performance* p = new Performance(show, venue, when, price_schedule); return p; }
Booking_from_XML.cpp • Delete Get_Booking() • Will do this in main()
Get_Date_and_Time tm Booking_from_XML::Get_Date_and_Time(TiXmlNode* date_node, TiXmlNode* time_node) { tm date_time = {0}; TiXmlNode* year_node = date_node->FirstChild(); string year_str = year_node->FirstChild()->Value(); date_time.tm_year = atoi(year_str.c_str()) - 1900; TiXmlNode* month_node = year_node->NextSibling(); string month_str = month_node->FirstChild()->Value(); date_time.tm_mon = atoi(month_str.c_str()) - 1; TiXmlNode* day_node = month_node->NextSibling(); string day_str = day_node->FirstChild()->Value(); date_time.tm_mday = atoi(day_str.c_str()); TiXmlNode* hour_node = time_node->FirstChild(); string hour_str = hour_node->FirstChild()->Value(); date_time.tm_hour = atoi(hour_str.c_str()); TiXmlNode* minute_node = hour_node->NextSibling(); string minute_str = minute_node->FirstChild()->Value(); date_time.tm_min = atoi(minute_str.c_str()); return date_time; }
Add to Ticket_Booth.h #pragma once #include <vector> #include <string> #include "Venue.h" #include "Show.h" #include "Booking.h" class Ticket_Booth { protected: static Ticket_Booth* instance; std::vector<Venue*> venues; std::vector<Show*> shows; std::vector<Booking*> bookings; Ticket_Booth(void) {}; ~Ticket_Booth(void) {}; public: static Ticket_Booth* Instance(); void Add_Venue(Venue* v); void Add_Show(Show* s); void Add_Booking(Booking* b); Venue* Get_Venue(string name) const; Show* Get_Show(string name) const; };
Add to Ticket_Booth.cpp void Ticket_Booth::Add_Booking(Booking* b) { bookings.push_back(b); }
Add to main.cpp http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Downloads/2011_04_20_Ticket_Booth/Get_Booking.cpp.txt Booking* Get_Booking() { string booking_filename = "Booking.xml"; TiXmlDocument doc(booking_filename); bool loadOkay = doc.LoadFile(); if (!loadOkay) { cout << "Could not load file " << booking_filename << endl; cout << "Error='" << doc.ErrorDesc() <<"'. Exiting.\n"; cin.get(); exit( 1 ); } TiXmlNode* booking_node = doc.FirstChild("booking"); assert(booking_node != 0); Booking* booking = Booking_from_XML::Get_Booking(booking_node); return booking; }
main.cpp • Add includes: #include "Booking.h" #include "Booking_from_XML.h"
main.cpp • Add at end of main(): Booking* booking = Get_Booking(); booking->Display(); cout << "Normal termination\n"; cin.get(); cin.get(); return 0; }
Bookings.xml • Download to project directory • http://www.cse.usf.edu/~turnerr/Object_Oriented_Design/Downloads/2011_03_28_XML/ • File Booking.xml