1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
int main() { // The client code changes only because we opt to place the new types of WatchableItems on the shelf.
WatchableCategory *newReleaseVHS = new WatchableCategory ("New Releases VHS", 5.75), *newReleaseDVD = new WatchableCategory ("New Releases DVD", 7.25),
*generalVHS = new WatchableCategory ("General VHS", 3.25), *generalDVD = new WatchableCategory ("General DVD", 5.25),
*documentaryDVD = new WatchableCategory ("Documentary DVD", 3.49), *howToVHS = new WatchableCategory ("How-To-Do-It VHS", 3.25), // There are no DVDs for the How-To-Do-It category due to lack of interest from the consumers.
*howToSeriesDVDOneWeekRental = new WatchableCategory ("How-To-Do-It in Series (One week rental)", 12.25); // If there is to be a shorter or longer duration for rental, create a new WatchableCategory for those (with their adjusted rental prices).
RatedMovie *starWarsMovieVHS = new RatedMovie (newReleaseVHS, "Star Wars", SciFi, PG), *starWarsMovieDVD = new RatedMovie (newReleaseDVD, "Star Wars", SciFi, PG);
Documentary* NASA = new Documentary (documentaryDVD, "NASA");
HowTo* howToCook = new HowTo (howToVHS, "How To Cook");
const std::string HowToPlaySnooker = "How To Play Snooker (3-part series)";
PartOfSeries *playSnookerPart1 = new PartOfSeries (howToSeriesDVDOneWeekRental, HowToPlaySnooker, 1, "Potting the Ball"),
*playSnookerPart2 = new PartOfSeries (howToSeriesDVDOneWeekRental, HowToPlaySnooker, 2, "Cue Ball Control"),
*playSnookerPart3 = new PartOfSeries (howToSeriesDVDOneWeekRental, HowToPlaySnooker, 3, "Tournament Preparation");
PartOfSeries* snookerParts[] = {playSnookerPart1, playSnookerPart2, playSnookerPart3};
ShowSeries* howToPlaySnookerParts1To3 = new ShowSeries (howToSeriesDVDOneWeekRental, HowToPlaySnooker);
for (PartOfSeries* x : snookerParts)
howToPlaySnookerParts1To3->addPart(x);
std::list<RentableItem*> inventory;
for (int i = 0; i < 6; i++)
inventory.emplace_back (new VideoTape (starWarsMovieVHS));
for (int i = 0; i < 4; i++)
inventory.emplace_back (new DVD (starWarsMovieDVD));
for (int i = 0; i < 4; i++)
inventory.emplace_back (new DVD (NASA));
for (int i = 0; i < 2; i++)
inventory.emplace_back (new VideoTape (howToCook));
inventory.emplace_back (new DVD (howToPlaySnookerParts1To3)); // Only one copy of this item will be placed because not too many people want to watch it.
for (RentableItem* x: inventory)
x->print();
std::cout << std::endl << "Star Wars is no longer a new release and is moved to category General." << std::endl << std::endl;
starWarsMovieVHS->changeCategory (generalVHS); // changeCategory is now a method in WatchableItem, so we could call changeCategory for NASA, howToCook, and howToPlaySnookerParts1To3 too if we wanted to.
starWarsMovieDVD->changeCategory (generalDVD);
for (RentableItem* x: inventory)
x->print();
Person stephen ("Stephen Hendry", 20.00);
RentableItem* stephensDVD = *std::find_if (inventory.begin(), inventory.end(), [howToPlaySnookerParts1To3](const RentableItem* x)->bool {return x->getShow() == howToPlaySnookerParts1To3;});
stephen.rentItem (stephensDVD, inventory); // 'howToPlaySnookerParts1To3' being rented from 'inventory'
stephen.play (stephensDVD); // Using the PartOfSeries::play() function of the Composite Pattern.
stephen.returnItem (stephensDVD, inventory);
std::cin.get();
}
/*
Output:
Video tape with title "Star Wars" and rental price $5.75 is on the shelf.
Video tape with title "Star Wars" and rental price $5.75 is on the shelf.
Video tape with title "Star Wars" and rental price $5.75 is on the shelf.
Video tape with title "Star Wars" and rental price $5.75 is on the shelf.
Video tape with title "Star Wars" and rental price $5.75 is on the shelf.
Video tape with title "Star Wars" and rental price $5.75 is on the shelf.
DVD with title "Star Wars" and rental price $7.25 is on the shelf.
DVD with title "Star Wars" and rental price $7.25 is on the shelf.
DVD with title "Star Wars" and rental price $7.25 is on the shelf.
DVD with title "Star Wars" and rental price $7.25 is on the shelf.
DVD with title "NASA" and rental price $3.49 is on the shelf.
DVD with title "NASA" and rental price $3.49 is on the shelf.
DVD with title "NASA" and rental price $3.49 is on the shelf.
DVD with title "NASA" and rental price $3.49 is on the shelf.
Video tape with title "How To Cook" and rental price $3.25 is on the shelf.
Video tape with title "How To Cook" and rental price $3.25 is on the shelf.
DVD with title "How To Play Snooker (3-part series)" and rental price $12.25 is on the shelf.
Star Wars is no longer a new release and is moved to category General.
Video tape with title "Star Wars" and rental price $3.25 is on the shelf.
Video tape with title "Star Wars" and rental price $3.25 is on the shelf.
Video tape with title "Star Wars" and rental price $3.25 is on the shelf.
Video tape with title "Star Wars" and rental price $3.25 is on the shelf.
Video tape with title "Star Wars" and rental price $3.25 is on the shelf.
Video tape with title "Star Wars" and rental price $3.25 is on the shelf.
DVD with title "Star Wars" and rental price $5.25 is on the shelf.
DVD with title "Star Wars" and rental price $5.25 is on the shelf.
DVD with title "Star Wars" and rental price $5.25 is on the shelf.
DVD with title "Star Wars" and rental price $5.25 is on the shelf.
DVD with title "NASA" and rental price $3.49 is on the shelf.
DVD with title "NASA" and rental price $3.49 is on the shelf.
DVD with title "NASA" and rental price $3.49 is on the shelf.
DVD with title "NASA" and rental price $3.49 is on the shelf.
Video tape with title "How To Cook" and rental price $3.25 is on the shelf.
Video tape with title "How To Cook" and rental price $3.25 is on the shelf.
DVD with title "How To Play Snooker (3-part series)" and rental price $12.25 is on the shelf.
Before renting "How To Play Snooker (3-part series)", Stephen Hendry has $20 (inventory size = 17).
After renting "How To Play Snooker (3-part series)", Stephen Hendry has $7.75 (inventory size = 16).
"Potting the Ball", part 1 of the series "How To Play Snooker (3-part series)", being played.
"Cue Ball Control", part 2 of the series "How To Play Snooker (3-part series)", being played.
"Tournament Preparation", part 3 of the series "How To Play Snooker (3-part series)", being played.
Stephen Hendry returns "How To Play Snooker (3-part series)" (inventory size = 17).
*/
|