COMP 2404 B/C – Assignment #2
Due: Thursday, February 16 at 11:59 pm
1. Goal
For this assignment, you will write a program in C++, in the Ubuntu Linux environment of the course VM,
to manage the patron and reservation data for a restaurant. The restaurant data will include a reservation
schedule, as well as a collection of registered patrons (i.e. the guests of the restaurant). The program will
allow the end user create a new reservation. They will also be able to print out the restaurant data, including
the master schedule of all reservations, the reservations for a single day, and the list of registered patrons.
2. Learning Outcomes
With this assignment, you will:
• practice implementing a design that is given as a UML class diagram
• implement a program separated into control, view, entity, and collection objects
• write code that follows the principle of least privilege
3. Instructions
3.1. Understand the UML class diagram
You will begin by understanding the UML class diagram below. Your program will implement the objects
and class associations represented in the diagram, as they are shown. UML class diagrams are explained
in the course material in section 2.3.
Control
+launch()
-initPatrons(inout r:Restaurant*)
-initReservations(inout r:Restaurant*)
View
+showMenu(out choice:int&)
+printStr(in str:string)
+readInt(out num:int&)
+readStr(out str:string&)
Restaurant
-name: string
+addPatron(in p:Patron*)
+reserveTable(in patronId:int,in tableNum:int,
in year:int,in month:int,in day:int,
in hour:int,in minute:int)
+printReservations()
+printSchedule(in year:int,in month:int,
in day:int)
+printPatrons()
1
1
Patron
-name: string
-id: int
-nextId: int
+print()
Reservation
-table: int
+lessThan(in res:Reservation*): bool
+matchDate(in date:Date*): bool
+print()
*
Date
-day: int
-month: int
-year: int
+validate(in day:int,in month:int,in year:int): bool
+lessThan(in date:Date*): bool
+equals(in date:Date*): bool
+print()
-other helpers()
Time
-hours: int
-minutes: int
+validate(in hrs:int,in mins:int): bool
+lessThan(in time:Time*): bool
+print()
-convertToMins(): int
1
1
*
1
©2023 Christine Laurendeau COMP 2404 B/C :: Winter 2023 :: Assignment #2 1/6
3.2. Download the base code
The a2-posted.tar file has been provided for you in Brightspace. This file contains the View class that
your code must use for most communications with the end user. It contains a skeleton
Control class,
with some data initialization member functions that your code is required to call. It also contains the
Time class that your code must use, as indicated in the UML class diagram above.
3.3.
Modify the Date class
You will begin with the Date class that we worked on during lectures, in section 1.5, program #4, and
you will make the following changes, in accordance with the given UML class diagram:
3.3.1. Implement
validate(), as a static member function that verifies whether or not the given parameters represent a valid date.
3.3.2. Implement the
lessThan() member function that compares two dates. One date is considered less
than another if it occurs sooner.
3.3.3. Implement the
equals() member function that checks if two dates are the same.
3.3.4. Remove the printing functions, and replace them with one
print() member function that prints out
the date using the format YYYY-MM-DD. For example, the due date for this assignment is 2023-02-16.
Remember: An object can always access the private members of another object of the same class.
Do not provide unnecessary getters.
3.4.
Modify the Time class
Implement the following member functions for the provided Time class, in accordance with the given
UML class diagram:
3.4.1. The
validate() static member function verifies whether or not the given parameters are valid.
3.4.2. The
lessThan() member function compares two times. One time is considered less than another if
it occurs sooner.
3.5.
Implement the Patron class
You will create a new Patron class that represents a patron of a restaurant. The Patron class will contain
all the data members and member functions indicated in the UML class diagram. In addition:
3.5.1. The
nextId data member must be declared a static member of the class:
(a) you will find an example of this in the coding example of section 3.1, program #6
(b) this data member must be initialized in the source file, as shown in the coding example; for
example, the statement:
int Patron::nextId = 5001; can be used at file scope
3.5.2. The default constructor must do the following:
(a) take a single parameter to initialize the patron name
(b) initialize the new patron’s id to the next available id
(c) increment the next available id
3.5.3. The program requires getter member functions for both the patron id and name.
3.5.4. The
print() member function prints both the patron id and name to the screen.
3.6.
Implement the PatronArray class
You will copy the Array class that we implemented in the coding example of section 2.2, program #1,
into a new collection class called
PatronArray. Then, you must make the following changes:
3.6.1. Modify the collection class so that it stores
Patron object pointers as data.
3.6.2. Update the printed header in the existing
print() member function.
3.6.3. Implement a new
bool find(int id, Patron** p) member function that does the following:
(a) search the array for the patron with the given id
(b) if the patron is found, a pointer to that
Patron object is “returned” in the p parameter, and the
function returns success
(c) if the patron is not found, the pointer returned in
p is set to null, and the function returns failure
©2023 Christine Laurendeau COMP 2404 B/C :: Winter 2023 :: Assignment #2 2/6
3.7. Implement the Reservation class
You will create a new Reservation class that represents a reservation for the restaurant. The Reservation
class will contain all the data members and member functions indicated in the given UML class diagram.
In addition:
3.7.1. The patron making the reservation must be stored as a pointer to the corresponding
Patron object.
3.7.2. The date and time of the reservation must each be stored as pointers to a
Date object and a Time
object respectively, and both objects must be dynamically allocated.
3.7.3. The table number represents the table that is reserved for that patron, on that date, at that time.
3.7.4. The constructor must take four parameters, and initialize the data members accordingly.
3.7.5. The destructor must deallocated the required dynamically allocated memory. Make sure that you
deallocate only the objects that are not used elsewhere.
3.7.6. The
lessThan() member function determines which of two reservations occurs sooner, by comparing their dates and times, using functions implemented in previous steps.
3.7.7. The
matchDate() member function determines whether the reservation’s date is the same as the
given parameter, using a function implemented in a previous step.
3.7.8. The
print() member function prints out the date and time of the reservation, using previously
implemented functions, the table number, and the patron’s name. Make sure that each part of the
reservation data is printed so that it aligns with the other reservations. You
MUST follow the format
shown in the assignment workshop video for this.
3.8.
Implement the RsvArray class
You will implement a RsvArray class that manages a collection of reservations.
3.8.1. The
RsvArray class must have the following data members:
(a) a
dynamically allocated array of Reservation object pointers, which represents the elements
that are managed by the RsvArray class; you can consult the coding example of section 1.5,
program #5, for examples of the four different kinds of arrays
(b) a
capacity that indicates the maximum number of elements that can be stored in the array
(c) a
size that represents the current number of elements in the array; all collections are empty at
the beginning of the program
3.8.2. The class must have the following member functions:
(a) a default constructor that does the following:
(i) it takes an integer parameter representing the capacity of the array; the default value must
be set to a provided constant
(ii) it dynamically allocates the memory for the array of pointers, using the provided capacity
(iii) it initializes all the data members
(b) a destructor that deallocates the required dynamically allocated memory
(c) a
void add(Reservation* r) member function that adds the given reservation to the array, in
its correct position, so that the array remains in ascending order by reservation date and time
(i) you must shift the elements in the array towards the back of the array to make room for the
new element in its correct place
(ii) do not add to the end of the array and sort, as this is both inefficient and unnecessary
(iii) you must use the
Reservation class’s lessThan() member function to perform the comparison
(d) a getter member function for the array size
(e) a
Reservation* get(int index) member function that returns a pointer to the reservation
found at the given index in the array; you must perform error checking here! if an invalid index
is provided, the returned pointer must be the
NULL pointer
(f) a
print() member function that prints out the details of each reservation in the array
©2023 Christine Laurendeau COMP 2404 B/C :: Winter 2023 :: Assignment #2 3/6
3.9. Implement the Restaurant class
You will create a new Restaurant class that represents a restaurant. This class will contain all the data
members and member functions indicated in the given UML class diagram. In addition:
3.9.1. The restaurant’s collection of patrons must be stored using a
PatronArray object.
3.9.2. The restaurant’s collection of reservations must be stored using a
RsvArray object.
3.9.3. The default constructor must take one string parameter for the restaurant’s name, and initialize that
data member.
3.9.4. The destructor may be empty, but its presence is required so that the destructors of the two collection
objects are called automatically when the restaurant object is deallocated.
3.9.5. The
addPatron() member function adds the given patron to the restaurant’s patron collection.
3.9.6. The
reserveTable() member function does the following:
(a) check that the given patron exists in the restaurant’s patron collection, and that both the given
date and the time information is valid; if any of the provided data is invalid, a detailed error
message must be printed out, and the function returns
(b) dynamically allocate a new
Date and a new Time object, using the information in the parameters
(c) dynamically allocate a new
Reservation object, using the information in the parameters and the
new date and time objects
(d) add the new reservation to the restaurant’s reservation collection
3.9.7. The
printSchedule() member function does the following:
(a) check that the given date information is valid; if not, a detailed error message must be printed
out, and the function returns
(b)
statically allocate a new Date object, using the information in the parameters
(c) print out the requested date and the restaurant name as part of a print header
(d) loop over the reservation collection; if an individual reservation matches the date created from
the parameters, then the details of the reservation are printed out
3.9.8. The
printReservations() member function prints out the restaurant name, and the details of each
reservation in the reservation collection.
3.9.9. The
printPatrons() member function prints out the details of each patron in the patron collection.
3.10.
Implement the Control class
You will implement the Control class with all the data members and member functions indicated in the
given UML class diagram. In addition:
3.10.1. The restaurant data member must be stored as a
Restaurant pointer.
3.10.2. The constructor must dynamically allocate and initialize the restaurant to be managed.
3.10.3. The destructor must clean up the necessary memory.
3.10.4. The
launch() member function does the following:
(a) call the initialization functions provided in the
a2-posted.tar file; you must use these functions,
without modification, to initialize the data in your program
(b) use the
View object to repeatedly print out the main menu and read the user’s selection, until
the user chooses to exit
(c) if required by the user:
(i) print out the restaurant’s entire reservation schedule
(ii) use the
View object to prompt the user to enter a year, month, and day, and print out the
reservation schedule for that day only
(iii) print out the restaurant’s patron collection
(iv) use the
View object to prompt the user to enter a patron id, table number, the reservation
year, month, day, hours, and minutes, and reserve the table accordingly
NOTE: The above functionality must reuse existing functions, everywhere possible.
©2023 Christine Laurendeau COMP 2404 B/C :: Winter 2023 :: Assignment #2 4/6
3.11. Write the main() function
Your main() function must declare a Control object and call its launch() function. The entire program
control flow must be implemented in the
Control object as described in the previous instruction, and
the
main() function must do nothing else.
3.12.
Packaging and documentation
3.12.1. Your code must be correctly separated into header and source files, as we saw in class.
3.12.2. You must provide a
Makefile that separately compiles each source file into a corresponding object
file, then links all the object files to produce the program executable. Your
Makefile must also
include the
clean target that removes all the object files and the program executable. Do not use
an auto-generated Makefile; it must be specific to this program.
3.12.3. You must provide a plain text
README file that includes:
(a) a preamble (program author, purpose, list of source, header, and data files, if applicable)
(b) compilation and launching instructions, including any command line arguments
3.12.4. Use either the
tar or the zip utility in Linux to package into one .tar or .zip file all the files
required to build and execute your program.
3.12.5. Do not include any additional files or directories in your submission.
3.12.6. All class definitions must be documented, as described in the course material, section 1.3. Please
DO NOT place inline comments in your function implementations.
3.13.
Program execution
To earn full marks for execution, your program must meet all of the following criteria:
3.13.1. The code must compile in the course VM.
3.13.2. The code must be implemented strictly in accordance with the instructions and constraints listed in
this document, and it must execute in the VM as described in the requirements.
3.13.3. The classes and functions listed in the requirements must be implemented, they must be called in
the program, and they must not be commented out.
3.13.4. All the data handled must be printed to the screen, as described in the requirements, to demonstrate
that the code works correctly.
3.13.5. The code must execute with no memory errors reported by
valgrind, and with no memory leaks.
4. Constraints
Your design and implementation must comply with all the rules of correct software development and programming conventions that we have learned during the course lectures, including but not restricted to:
4.1. The code must be written using the C++11 standard that is supported by the course VM, and it must
compile and execute in that environment. It must not require the installation of libraries or packages or
any software not already provided in the course VM.
4.2. Your program must comply with the principle of least privilege, and it must follow correct encapsulation
principles, including the separation of control, view, entity, and collection object functionality.
4.3. Your program must not use any classes, containers, or algorithms from the C++ standard template library
(STL), unless explicitly permitted in the instructions.
4.4. If base code is provided, do not make any unauthorized changes to it.
4.5. Your program must follow basic OO programming conventions, including the following:
4.5.1. Do not use any global variables or any global functions other than
main().
4.5.2. Do not use
structs. You must use classes instead.
4.5.3. Objects must always be passed by reference, never by value.
4.5.4. Functions must return data using parameters, not using return values, except for basic getter functions and where otherwise permitted in the instructions.
©2023 Christine Laurendeau COMP 2404 B/C :: Winter 2023 :: Assignment #2 5/6
4.5.5. Existing functions and predefined constants must be reused everywhere possible.
4.5.6. All basic error checking must be performed.
4.5.7. All dynamically allocated memory must be explicitly deallocated.
4.6. You may implement helper classes and helper member functions, but only if the design is consistent with
the provided instructions. Design and implementation choices that undermine the learning outcomes
will not earn any marks.
5. Submission Requirements
5.1. You will submit in Brightspace, before the due date and time, one tar or zip file that includes all your
program files, as described in the
Packaging and documentation instructions.
5.2. Late submissions will be subject to the late penalty described in the course outline. No exceptions will
be made, including for technical and/or connectivity issues. Do not wait until the last day to submit.
5.3. Only files uploaded into
Brightspace will be graded. Submissions that contain incorrect, corrupt, or missing files will be graded as is. Corrections to submissions will not be accepted after the due date and time,
for any reason.
6. Grading Criteria
• 13 marks: correct design and implementation of Date and Time classes
• 4 marks: correct design and implementation of
Patron class
• 12 marks: correct design and implementation of
PatronArray class
• 16 marks: correct design and implementation of
Reservation class
• 14 marks: correct design and implementation of
RsvArray class
• 15 marks: correct design and implementation of
Restaurant class
• 8 marks: correct design and implementation of
Control class
• 8 marks: correct packaging and documentation
• 10 marks: correct program execution
©2023 Christine Laurendeau COMP 2404 B/C :: Winter 2023 :: Assignment #2 6/6