Containers

In this workshop, you will code three classes that are in composition and aggregation relations. The classes will simulate a very simplified form of managing reservations in a restaurant. The restaurant will manage a collection of reservations (composition); a messaging system will be used to send confirmations for the reservation.

Learning Outcomes

Upon successful completion of this workshop, you will have demonstrated the abilities to:

  • design and code composition and aggregation class relationships
  • use the member functions of the string class to parse a string into tokens based on simple rules
  • design and code a class that manages a dynamically allocated array of pointers to objects

In-Lab

The in-lab portion of this workshop consists of modules:

  • w4 (supplied)
  • Reservation

Enclose all your source code within the sdds namespace and include the necessary guards in each header file.

w4 Module (supplied)

Do not modify this module! Look at the code and make sure you understand it.

Reservation Module

This module defines a class that holds information about a single reservation at a restaurant for a date/time in October.

Design and code a class named Reservation that should be able to store the following information (for each attribute, you can chose any type you think it's appropriate--you must be able to justify the decisions you have made):

  • reservation id: an array of characters
  • the name on the reservation
  • the email that can be used to send the confirmation that the reservation can be honored or not
  • the number of people in the party
  • the day when the party is expected to come (for simplicity, the day is an integer between 1 and 31)
  • the hour when the party is expected to come (for simplicity, the hour is an integer between 1 and 24)

Public Members

  • a default constructor
  • Reservation(const std::string& m_res): A constructor that receives the reservation as a string; this constructor is responsible to extract the information about the reservation from the parameter and store it in the attributes of the instance. The parameter will always have the following format:
ID:NAME,EMAIL,PARTY SIZE,DAY,HOUR

This constructor should remove all spaces from the beginning and end of any token in the string.

When implementing the constructor, consider the following functions:

  • std::string::substr()
  • std::string::find()
  • std::string::erase()
  • std::stoi()

Friend Helpers

overload the insertion operator to insert the content of a reservation object into an ostream object:

if the hour is between 6AM and 9AM (inclusive), the kitchen serves breakfast:

Reservation ID: NAME < email> Breakfast on day DAY @ HOUR:00 for #PARTY_SIZE people.

if the hour is between 11AM and 3PM (inclusive), the kitchen serves lunch:

Reservation ID: NAME < email> Lunch on day DAY @ HOUR:00 for #PARTY_SIZE people.

if the hour is between 5PM and 9PM (inclusive), the kitchen serves dinner:

Reservation ID: NAME < email> Dinner on day DAY @ HOUR:00 for #PARTY_SIZE people.

at any other time the kitchen is closed and only drinks can be served:

Reservation ID: NAME < email> Drinks on day DAY @ HOUR:00 for #PARTY_SIZE people.

the name on the reservation should be printed on a field of size 10, aligned to the right

the email on the reservation (including the characters < and >) should be printed on a field of size 20, aligned to the left.

this operator should insert the endline character before exiting.

Sample Output

When the program is started with the command (the file data.txt is provided):

w4.exe data.txt

the output should look like:

Command Line:
--------------------------
1: w4.exe
2: data.txt
--------------------------

Reservations
--------------------------
Reservation RES-001: John < john@email.com> Drinks on day 3 @ 5:00 for 2 people.
Reservation RES-002: David < david@email.com> Breakfast on day 4 @ 6:00 for 1 people.
Reservation RES-003: Sara < sara@email.com> Breakfast on day 5 @ 7:00 for 2 people.
Reservation RES-004: Ana < ana@email.com> Breakfast on day 5 @ 8:00 for 1 people.
Reservation RES-005: John < john@email.com> Breakfast on day 4 @ 9:00 for 1 people.
Reservation RES-006: Vanessa < vanessa@email.com> Drinks on day 3 @ 10:00 for 2 people.
Reservation RES-007: Mike < mike@email.com> Lunch on day 4 @ 11:00 for 4 people.
Reservation RES-008: Mike < mike@email.com> Lunch on day 5 @ 12:00 for 8 people.
Reservation RES-009: Dan < dan@email.com> Lunch on day 3 @ 13:00 for 2 people.
Reservation RES-010: Donna < donna@email.com> Lunch on day 5 @ 14:00 for 5 people.
Reservation RES-011: Ana < ana@email.com> Lunch on day 4 @ 15:00 for 4 people.
Reservation RES-012: John < john@email.com> Drinks on day 5 @ 16:00 for 2 people.
Reservation RES-013: Sara < sara@email.com> Dinner on day 3 @ 17:00 for 6 people.
Reservation RES-014: Jennifer < jenn@email.com> Dinner on day 5 @ 18:00 for 6 people.
Reservation RES-015: Stan < stan@email.com> Dinner on day 4 @ 19:00 for 5 people.
Reservation RES-016: Chris < chris@email.com> Dinner on day 4 @ 20:00 for 3 people.
Reservation RES-017: Vanessa < vanessa@email.com> Dinner on day 4 @ 21:00 for 4 people.
Reservation RES-018: David < david@email.com> Drinks on day 5 @ 22:00 for 4 people.
Reservation RES-019: Chris < chris@email.com> Drinks on day 3 @ 23:00 for 1 people.
Reservation RES-020: Donna < donna@email.com> Drinks on day 4 @ 24:00 for 3 people.
--------------------------

At-Home

The at-home part of this workshop upgrades your in-lab solution to include two more modules:

  • Restaurant
  • ConfirmationSender

The module Reservation dosn't need any change.

Restaurant Module

Add a Restaurant module to your project. This module should maintain a dynamically allocated array of objects of type Reservation: Reservation* (each element of the array is an object of type Reservation).

Public Members

  • Restaurant(Reservation* reservations[], size_t cnt): a constructor that receives as a parameter an array of pointers to objects of type Reservation (i.e., each element of the array is a pointer). If you need a refresh on arrays of pointers, re-read the material from the last term (chapter Abstract Base Classes, section Array of Pointers).
    • this constructor should store copies of all reservations
  • add any other special members that are necessary to manage a the resource
  • size_t size() const: return how many reservations are in the system.

Friend Helpers

overload the insertion operator to insert the content of a Restaurant object into an ostream object:

if there are no reservations:

--------------------------
Fancy Restaurant
--------------------------
The object is empty!
--------------------------

if there are reservations:

--------------------------
Fancy Restaurant
--------------------------
RESERVATION
RESERVATION
...
--------------------------

ConfirmationSender Module

Add a ConfirmationSender module to your project. The purpose of this module is to receive all the reservations from multiple restaurants, and contact the recipients with a confirmation message.

This module should maintain a dynamically allocated array of pointers to objects of type Reservation: const sdds::Reservation** (each element of the array is a pointer to an object of type Reservation).

Public Members

  • add any special members that are necessary to manage a the resource (the resource is an array of pointers; the array must be managed by this class, but the objects at the addresses stored in the array are managed outside the class)
  • ConfirmationSender& operator+=(const Reservation& res): add the parameter to the array.
    • if res is already in the array, this operator does nothing
    • resize the array to make room for the res
    • store the address of res in the array (this function should not make copies of the parameter)
  • ConfirmationSender& operator-=(const Reservation& res): remove the parameter from the array
    • if res is not in the array, this operator does nothing
    • search the array for res, set the pointer in the array to nullptr when res is found. To challenge yourself, try to actually resize the array.

Friend Helpers

overload the insertion operator to insert the content of a ConfirmationSender object into an ostream object:

if there are no reservations to confirm:

--------------------------
Confirmations to Send
--------------------------
The object is empty!
--------------------------

if there are reservations to confirm

--------------------------
Confirmations to Send
--------------------------
RESERVATION
RESERVATION
...
--------------------------

Sample Output

When the program is started with the command:

w4.exe data.txt

the output should look like:

Command Line:
--------------------------
1: w4.exe
2: data.txt
--------------------------

Reservations
--------------------------
Reservation RES-001: John < john@email.com> Drinks on day 3 @ 5:00 for 2 people.
Reservation RES-002: David < david@email.com> Breakfast on day 4 @ 6:00 for 1 people.
Reservation RES-003: Sara < sara@email.com> Breakfast on day 5 @ 7:00 for 2 people.
Reservation RES-004: Ana < ana@email.com> Breakfast on day 5 @ 8:00 for 1 people.
Reservation RES-005: John < john@email.com> Breakfast on day 4 @ 9:00 for 1 people.
Reservation RES-006: Vanessa < vanessa@email.com> Drinks on day 3 @ 10:00 for 2 people.
Reservation RES-007: Mike < mike@email.com> Lunch on day 4 @ 11:00 for 4 people.
Reservation RES-008: Mike < mike@email.com> Lunch on day 5 @ 12:00 for 8 people.
Reservation RES-009: Dan < dan@email.com> Lunch on day 3 @ 13:00 for 2 people.
Reservation RES-010: Donna < donna@email.com> Lunch on day 5 @ 14:00 for 5 people.
Reservation RES-011: Ana < ana@email.com> Lunch on day 4 @ 15:00 for 4 people.
Reservation RES-012: John < john@email.com> Drinks on day 5 @ 16:00 for 2 people.
Reservation RES-013: Sara < sara@email.com> Dinner on day 3 @ 17:00 for 6 people.
Reservation RES-014: Jennifer < jenn@email.com> Dinner on day 5 @ 18:00 for 6 people.
Reservation RES-015: Stan < stan@email.com> Dinner on day 4 @ 19:00 for 5 people.
Reservation RES-016: Chris < chris@email.com> Dinner on day 4 @ 20:00 for 3 people.
Reservation RES-017: Vanessa < vanessa@email.com> Dinner on day 4 @ 21:00 for 4 people.
Reservation RES-018: David < david@email.com> Drinks on day 5 @ 22:00 for 4 people.
Reservation RES-019: Chris < chris@email.com> Drinks on day 3 @ 23:00 for 1 people.
Reservation RES-020: Donna < donna@email.com> Drinks on day 4 @ 24:00 for 3 people.
--------------------------

R: Testing Constuctor
==========================
--------------------------
Fancy Restaurant
--------------------------
Reservation RES-001: John < john@email.com> Drinks on day 3 @ 5:00 for 2 people.
Reservation RES-002: David < david@email.com> Breakfast on day 4 @ 6:00 for 1 people.
Reservation RES-003: Sara < sara@email.com> Breakfast on day 5 @ 7:00 for 2 people.
Reservation RES-004: Ana < ana@email.com> Breakfast on day 5 @ 8:00 for 1 people.
Reservation RES-005: John < john@email.com> Breakfast on day 4 @ 9:00 for 1 people.
Reservation RES-006: Vanessa < vanessa@email.com> Drinks on day 3 @ 10:00 for 2 people.
Reservation RES-007: Mike < mike@email.com> Lunch on day 4 @ 11:00 for 4 people.
Reservation RES-008: Mike < mike@email.com> Lunch on day 5 @ 12:00 for 8 people.
Reservation RES-009: Dan < dan@email.com> Lunch on day 3 @ 13:00 for 2 people.
Reservation RES-010: Donna < donna@email.com> Lunch on day 5 @ 14:00 for 5 people.
Reservation RES-011: Ana < ana@email.com> Lunch on day 4 @ 15:00 for 4 people.
Reservation RES-012: John < john@email.com> Drinks on day 5 @ 16:00 for 2 people.
Reservation RES-013: Sara < sara@email.com> Dinner on day 3 @ 17:00 for 6 people.
Reservation RES-014: Jennifer < jenn@email.com> Dinner on day 5 @ 18:00 for 6 people.
Reservation RES-015: Stan < stan@email.com> Dinner on day 4 @ 19:00 for 5 people.
Reservation RES-016: Chris < chris@email.com> Dinner on day 4 @ 20:00 for 3 people.
Reservation RES-017: Vanessa < vanessa@email.com> Dinner on day 4 @ 21:00 for 4 people.
Reservation RES-018: David < david@email.com> Drinks on day 5 @ 22:00 for 4 people.
Reservation RES-019: Chris < chris@email.com> Drinks on day 3 @ 23:00 for 1 people.
Reservation RES-020: Donna < donna@email.com> Drinks on day 4 @ 24:00 for 3 people.
--------------------------
==========================

R: Testing Copy Constuctor
==========================
--------------------------
Fancy Restaurant
--------------------------
Reservation RES-001: John < john@email.com> Drinks on day 3 @ 5:00 for 2 people.
Reservation RES-002: David < david@email.com> Breakfast on day 4 @ 6:00 for 1 people.
Reservation RES-003: Sara < sara@email.com> Breakfast on day 5 @ 7:00 for 2 people.
Reservation RES-004: Ana < ana@email.com> Breakfast on day 5 @ 8:00 for 1 people.
Reservation RES-005: John < john@email.com> Breakfast on day 4 @ 9:00 for 1 people.
Reservation RES-006: Vanessa < vanessa@email.com> Drinks on day 3 @ 10:00 for 2 people.
Reservation RES-007: Mike < mike@email.com> Lunch on day 4 @ 11:00 for 4 people.
Reservation RES-008: Mike < mike@email.com> Lunch on day 5 @ 12:00 for 8 people.
Reservation RES-009: Dan < dan@email.com> Lunch on day 3 @ 13:00 for 2 people.
Reservation RES-010: Donna < donna@email.com> Lunch on day 5 @ 14:00 for 5 people.
Reservation RES-011: Ana < ana@email.com> Lunch on day 4 @ 15:00 for 4 people.
Reservation RES-012: John < john@email.com> Drinks on day 5 @ 16:00 for 2 people.
Reservation RES-013: Sara < sara@email.com> Dinner on day 3 @ 17:00 for 6 people.
Reservation RES-014: Jennifer < jenn@email.com> Dinner on day 5 @ 18:00 for 6 people.
Reservation RES-015: Stan < stan@email.com> Dinner on day 4 @ 19:00 for 5 people.
Reservation RES-016: Chris < chris@email.com> Dinner on day 4 @ 20:00 for 3 people.
Reservation RES-017: Vanessa < vanessa@email.com> Dinner on day 4 @ 21:00 for 4 people.
Reservation RES-018: David < david@email.com> Drinks on day 5 @ 22:00 for 4 people.
Reservation RES-019: Chris < chris@email.com> Drinks on day 3 @ 23:00 for 1 people.
Reservation RES-020: Donna < donna@email.com> Drinks on day 4 @ 24:00 for 3 people.
--------------------------
--------------------------
Fancy Restaurant
--------------------------
Reservation RES-001: John < john@email.com> Drinks on day 3 @ 5:00 for 2 people.
Reservation RES-002: David < david@email.com> Breakfast on day 4 @ 6:00 for 1 people.
Reservation RES-003: Sara < sara@email.com> Breakfast on day 5 @ 7:00 for 2 people.
Reservation RES-004: Ana < ana@email.com> Breakfast on day 5 @ 8:00 for 1 people.
Reservation RES-005: John < john@email.com> Breakfast on day 4 @ 9:00 for 1 people.
Reservation RES-006: Vanessa < vanessa@email.com> Drinks on day 3 @ 10:00 for 2 people.
Reservation RES-007: Mike < mike@email.com> Lunch on day 4 @ 11:00 for 4 people.
Reservation RES-008: Mike < mike@email.com> Lunch on day 5 @ 12:00 for 8 people.
Reservation RES-009: Dan < dan@email.com> Lunch on day 3 @ 13:00 for 2 people.
Reservation RES-010: Donna < donna@email.com> Lunch on day 5 @ 14:00 for 5 people.
Reservation RES-011: Ana < ana@email.com> Lunch on day 4 @ 15:00 for 4 people.
Reservation RES-012: John < john@email.com> Drinks on day 5 @ 16:00 for 2 people.
Reservation RES-013: Sara < sara@email.com> Dinner on day 3 @ 17:00 for 6 people.
Reservation RES-014: Jennifer < jenn@email.com> Dinner on day 5 @ 18:00 for 6 people.
Reservation RES-015: Stan < stan@email.com> Dinner on day 4 @ 19:00 for 5 people.
Reservation RES-016: Chris < chris@email.com> Dinner on day 4 @ 20:00 for 3 people.
Reservation RES-017: Vanessa < vanessa@email.com> Dinner on day 4 @ 21:00 for 4 people.
Reservation RES-018: David < david@email.com> Drinks on day 5 @ 22:00 for 4 people.
Reservation RES-019: Chris < chris@email.com> Drinks on day 3 @ 23:00 for 1 people.
Reservation RES-020: Donna < donna@email.com> Drinks on day 4 @ 24:00 for 3 people.
--------------------------
==========================

R: Testing Move Constuctor
==========================
--------------------------
Fancy Restaurant
--------------------------
The object is empty!
--------------------------
--------------------------
Fancy Restaurant
--------------------------
Reservation RES-001: John < john@email.com> Drinks on day 3 @ 5:00 for 2 people.
Reservation RES-002: David < david@email.com> Breakfast on day 4 @ 6:00 for 1 people.
Reservation RES-003: Sara < sara@email.com> Breakfast on day 5 @ 7:00 for 2 people.
Reservation RES-004: Ana < ana@email.com> Breakfast on day 5 @ 8:00 for 1 people.
Reservation RES-005: John < john@email.com> Breakfast on day 4 @ 9:00 for 1 people.
Reservation RES-006: Vanessa < vanessa@email.com> Drinks on day 3 @ 10:00 for 2 people.
Reservation RES-007: Mike < mike@email.com> Lunch on day 4 @ 11:00 for 4 people.
Reservation RES-008: Mike < mike@email.com> Lunch on day 5 @ 12:00 for 8 people.
Reservation RES-009: Dan < dan@email.com> Lunch on day 3 @ 13:00 for 2 people.
Reservation RES-010: Donna < donna@email.com> Lunch on day 5 @ 14:00 for 5 people.
Reservation RES-011: Ana < ana@email.com> Lunch on day 4 @ 15:00 for 4 people.
Reservation RES-012: John < john@email.com> Drinks on day 5 @ 16:00 for 2 people.
Reservation RES-013: Sara < sara@email.com> Dinner on day 3 @ 17:00 for 6 people.
Reservation RES-014: Jennifer < jenn@email.com> Dinner on day 5 @ 18:00 for 6 people.
Reservation RES-015: Stan < stan@email.com> Dinner on day 4 @ 19:00 for 5 people.
Reservation RES-016: Chris < chris@email.com> Dinner on day 4 @ 20:00 for 3 people.
Reservation RES-017: Vanessa < vanessa@email.com> Dinner on day 4 @ 21:00 for 4 people.
Reservation RES-018: David < david@email.com> Drinks on day 5 @ 22:00 for 4 people.
Reservation RES-019: Chris < chris@email.com> Drinks on day 3 @ 23:00 for 1 people.
Reservation RES-020: Donna < donna@email.com> Drinks on day 4 @ 24:00 for 3 people.
--------------------------
==========================

CS: Testing Constuctor
==========================
--------------------------
Confirmations to Send
--------------------------
The object is empty!
--------------------------
==========================

CS: Testing Operators
==========================
--------------------------
Confirmations to Send
--------------------------
Reservation RES-006: Vanessa < vanessa@email.com> Drinks on day 3 @ 10:00 for 2 people.
Reservation RES-017: Vanessa < vanessa@email.com> Dinner on day 4 @ 21:00 for 4 people.
Reservation RES-009: Dan < dan@email.com> Lunch on day 3 @ 13:00 for 2 people.
--------------------------
--------------------------
Confirmations to Send
--------------------------
Reservation RES-006: Vanessa < vanessa@email.com> Drinks on day 3 @ 10:00 for 2 people.
Reservation RES-017: Vanessa < vanessa@email.com> Dinner on day 4 @ 21:00 for 4 people.
--------------------------
==========================

CS: Testing Copy Constuctor
==========================
--------------------------
Confirmations to Send
--------------------------
Reservation RES-006: Vanessa < vanessa@email.com> Drinks on day 3 @ 10:00 for 2 people.
Reservation RES-017: Vanessa < vanessa@email.com> Dinner on day 4 @ 21:00 for 4 people.
--------------------------
--------------------------
Confirmations to Send
--------------------------
Reservation RES-006: Vanessa < vanessa@email.com> Drinks on day 3 @ 10:00 for 2 people.
Reservation RES-017: Vanessa < vanessa@email.com> Dinner on day 4 @ 21:00 for 4 people.
--------------------------
==========================

CS: Testing Move Constuctor
==========================
--------------------------
Confirmations to Send
--------------------------
The object is empty!
--------------------------
--------------------------
Confirmations to Send
--------------------------
Reservation RES-006: Vanessa < vanessa@email.com> Drinks on day 3 @ 10:00 for 2 people.
Reservation RES-017: Vanessa < vanessa@email.com> Dinner on day 4 @ 21:00 for 4 people.
--------------------------
==========================
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.