Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

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...

1 answer below »
COMP 2404 B/C – Assignment #2
Due: Thursday, Fe
uary 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,
XXXXXXXXXXin year:int,in month:int,in day:int,
XXXXXXXXXXin hour:int,in minute:int)
+printReservations()
+printSchedule(in year:int,in month:int,
XXXXXXXXXXin 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 parame-
ters 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 XXXXXXXXXX.
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; fo
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 PatronA
ay class
You will copy the A
ay class that we implemented in the coding example of section 2.2, program #1,
into a new collection class called PatronA
ay. 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 a
ay 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
Youwill 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 co
esponding 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 compar-
ing 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
eservation 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 RsvA
ay class
You will implement a RsvA
ay class that manages a collection of reservations.
3.8.1. The RsvA
ay class must have the following data members:
(a) a dynamically allocated a
ay of Reservation object pointers, which represents the elements
that are managed by the RsvA
ay class; you can consult the coding example of section 1.5,
program #5, for examples of the four different kinds of a
ays
(b) a capacity that indicates the maximum number of elements that can be stored in the a
ay
(c) a size that represents the cu
ent number of elements in the a
ay; 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 a
ay; the default value must
e set to a provided constant
(ii) it dynamically allocates the memory for the a
ay 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 a
ay, in
its co
ect position, so that the a
ay remains in ascending order by reservation date and time
(i) you must shift the elements in the a
ay towards the back of the a
ay to make room for the
new element in its co
ect place
(ii) do not add to the end of the a
ay and sort, as this is both inefficient and unnecessary
(iii) you must use the Reservation class’s lessThan() member function to perform the compar-
ison
(d) a getter member function for the a
ay size
(e) a Reservation* get(int index) member function that returns a pointer to the reservation
found at the given index in the a
ay; you must perform e
or checking here! if an invalid index
is provided, the returned pointer must be the NULL pointe
(f) a print() member function that prints out the details of each reservation in the a
ay
©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 PatronA
ay object.
3.9.2. The restaurant’s collection of reservations must be stored using a RsvA
ay 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 e
o
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 e
or 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 heade
(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
eservation 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:
XXXXXXXXXXThe restaurant data member must be stored as a Restaurant pointer.
XXXXXXXXXXThe constructor must dynamically allocate and initialize the restaurant to be managed.
XXXXXXXXXXThe destructor must clean up the necessary memory.
XXXXXXXXXXThe 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
eservation 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
XXXXXXXXXXYour code must be co
ectly separated into header and source files, as we saw in class.
XXXXXXXXXXYou must provide a Makefile that separately compiles each source file into a co
esponding object
file, then links all the object files to produce the program executable. Your Makefile must also
include the clean target that removes all
Answered 2 days After Feb 14, 2023

Solution

Gaurav answered on Feb 16 2023
39 Votes
#include #include #include #include #include #include #include using namespace std;
void swapReserveItems(int reservations[][5], int indexA, int indexB)
{
swaps 2 items in reservation by row index
for(int j = 0; j<5; j++)
{
int temp = reservations[indexA][j];
reservations[indexA][j]=reservations[indexB][j];
reservations[indexB][j]=temp;
}
}
void readTable(std::istream& in, int row[], int numCols)
{

eads a single table row from a line in a stream
for (int i = 0; i in
row[i];
}
void sortReservations(int reservations[][5], int numItems)
{

sorts the reservations by reservation number(the first element in each row of the a
ay)
int temp[5];
for (int i = 0; i for (int j = i+1; j if (reservations[i][0] > reservations[j][0]){
for (int k = 0; k<5; k++){
temp[k] = reservations[i][k];
}
for (int k = 0; k<5; k++){
reservations[i][k] = reservations[j][k];
}
for (int k = 0; k<5; k++){
reservations[i][k] = temp[k];
}
}
}
}
}
void writeTable(std::ostream& out, int row[], int numCols)
{

writes a single table to a stream
for(int i = 0; i< numCols; i++)
{
out
ow[i]
" ";
}
out
std::endl;
}
void writeGuest(std::ostream& out, std::string guest[], int numCols)
{

writes a single guest to a stream
for (int i = 0; i out
guest[i]
" ";
}
}
void readGuest(std::istream& in, std::string guest[], int numCols)
{

eads a single guest from a line in a stream
for(int i=0; i {
in
guest[i];
}
}
void loadGuests(std::istream& in, std::string guests[][5], int numGuests)
{

loads guests from a stream
for(int i=0; i {
readGuest(in, guests[i], 5);
}
}
void swapGuestItems(std::string guest[][5], int indexA, int indexB)
{

swaps 2 items in guest by row index

string temp[5];
for (int k = 0; k<5; k++){
temp[k] = guest[indexA][k];
}
for (int k = 0; k<5; k++){
guest[indexA][k] = guest[indexB][k];
}
for (int k = 0; k<5; k++){
guest[indexB][k] = temp[k];
}
}
std::string getGuestPhone()
{

prompts the user for a guests phone numbe
while(true)
{
std::string phone="";
std::cout
"Enter phone(no punctuation): ";
std::cin
phone;
if(!std::cin || phone.length()!=10 || !(phone.find_first_not_of( "0123456789" ) == string::npos))
checks phone number is a series of digits of the right length
{
std::cout
"\r:::Invalid Entry!!!:::";
std::cin.clear();
std::cin.ignore(256,'\n');
}
else
{
std::cin.clear();
std::cin.ignore(256,'\n');
return phone;
}
}
return "";
}
void sortGuests(std::string guests[][5], int numItems)
{

sorts the guests by guest numbe
for(int i=1; i {
for(int j=0; j {
if(atoi(guests[j][0].c_str())>atoi(guests[j+1][0].c_str()))
{
swapGuestItems(guests,j,j+1);
}
}
}
}
void saveGuests(std::string guests[][5], int numGuests)
{

saves guest a
ay to a file
std::fstream outFile;
outFile.open("guests.txt", std::ios::out);
outFile
numGuests
std::endl;
for(int i=0; i {
writeGuest(outFile, guests[i], 5);
}
outFile.close();
}
void saveTables(int table[][9], int numRows)
{

saves tables in table a
ay to a file
std::fstream outFile;
outFile.open("tables.txt", std::ios::out);
outFile
numRows
std::endl;
for(int i=0; i {
writeTable(outFile, table[i],9);
}
outFile.close();
}
ool guestExists(std::string guests[][5], int numGuests, std::string Gname)
{

checks to see if a guest exists by their last name
for( int i=0; i {
if (guests[i][1]==Gname)
{
return true;
}
}
return false;
}
void addGuest(std::string guests[][5], int numGuests, std::string row[], int numCols)
{

adds a guest held in row[] to a temp a
ay and saves the new a
ay to file
std::string temp[numGuests+1][5];
for(int i = 0; i {
for(int j = 0; j {
temp[i][j]=guests[i][j];
}
}
for(int i = 0; i {
temp[numGuests][i]=row[i];
}
saveGuests(temp, numGuests+1);
}
void readReservation(std::istream& in, int reservation[], int numCols)
{

eads a single reservation from a stream and loads it into the row reservation[]
for(int i=0; i {
in
eservation[i];
}
}
std::string genNewGuestID(std::string guests[][5], int numGuests)
{

finds either an empty ID to return or returns a new one
bool found = false;
int largest=0;
int ID=0;
sortGuests(guests,numGuests);

looks for a gap in the sorted IDs
for(int i=0; i {
if(atoi(guests[i][0].c_str()) < atoi(guests[i+1][0].c_str())-1)
{
ID = atoi(guests[i][0].c_str())+1;
found = true;

eak;
}
if(atoi(guests[i][0].c_str())>largest)
{
largest = atoi(guests[i][0].c_str());
}
}

if a gap was not found, set ID to largest + 1
if(!found)
{
std::stringstream ss;
ss
largest+2;
return ss.str();
}
std::stringstream ss;
ss
ID;
return ss.str();
}
std::string GuestNameByGID(std::string guests[][5],int numGuests,int GID)
{

returns a guests name given the GID
for(int i=0;...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here