Question: Need help with a c++ question. This module includes the definition of a class that manages information about a single reservation at a restaurant for
Need help with a c++ question.
This module includes the definition of a class that manages information about a single reservation at a restaurant for a date/time in February.
Design and code a class named Reservation that can store the following information (for each attribute, chose any type that you think is appropriate--you must be able to justify the decisions you make):
- reservation id: an array of characters
- the name on the reservation
- the email to be used to send a confirmation that the reservation can be honoured or cannot
- the number of people in the party
- the day when the party expects to come (for simplicity, the day is an integer between 1 and 28)
- the hour when the party expects to come (for simplicity, the hour is an integer between 1 and 24)
Public Members
a default constructor
void update(int day, int time): a modifier that receives as parameters a new day and time for the reservation and updates the attributes with received values. Assume the parameters are correct and don't require validation.
Reservation(const std::string& res): A constructor that receives the reservation as a string; this constructor is responsible for extracting information about the reservation from the string and storing the tokens in the instance's attributes. The string will always have the following format:
ID:NAME,EMAIL,PARTY_SIZE,DAY,HOUR This constructor should remove all leading and trailing spaces from the beginning and end of any token extracted from the string.
When implementing the constructor, consider this following functions:
- std::string::substr()
- std::string::find()
- std::string::erase()
- std::stoi()
Friend Helpers
- overload the insertion operator to insert the contents of a reservation object into an ostream object:
- if the hour is between 6AM and 9AM (inclusive), the kitchen serves breakfast:
- if the hour is between 11AM and 3PM (inclusive), the kitchen serves lunch:
- if the hour is between 5PM and 9PM (inclusive), the kitchen serves dinner:
- at any other time the kitchen is closed and only drinks can be served:
- the ID on the reservation should display on a field of size 10, aligned to the right
- the name on the reservation should display on a field of size 20, aligned to the right
- the email on the reservation (including the characters < and >) display on a field of size 20, aligned to the left.
- this operator should insert the end line character before returning control.
- if the reservation is for one person, your output should say "person" instead of "people".
data.txt
# ID : Name, email, # of people, Day, Time #------------------------------------------------------------ 1234: John, j..n@email.com, 2, 3, 5 ab defghij : David, d..d@email.com, 1, 4, 6 RES - 003 : Sara, s..a@email.com, 2, 5, 7 RES - 004: Ana, a..a@email.com, 1, 5, 8 RES - 005: John, j..n@email.com, 1, 4, 9 RES - 006: Vanessa, v..a@email.com, 2, 3, 10 RES 007: Mike, m..e@email.com, 4, 4, 11 RES - 008: Mike, m..e@email.com, 8, 5, 12 RES - 009: Dan, d..n@email.com, 2, 3, 13 RES - 010: Donna, d..a@email.com, 5, 5, 14 RES - 011: Ana, a..a@email.com, 4, 4, 15 RES - 012: Jon Snow, j..n@email.com, 2, 5, 16 RES - 013: Sara, s..a@email.com, 6, 3, 17 RES - 014: Jennifer, j..n@email.com, 6, 5, 18 RES - 015 :Stan, s..n@email.com, 5, 4, 19 RES - 016: Chris, c..s@email.com, 3, 4, 20 RES - 017: Vanessa, v..a@email.com, 4, 4, 21 RES - 018: David, d..d@email.com, 4, 5, 22 RES - 019: Chris, c..s@email.com, 1, 3, 23 RES - 020: Donna, d..a@email.com, 3, 4, 24
Main.cpp
#include
int main(int argc, char** argv) { std::cout << "Command Line: "; std::cout << "-------------------------- "; for (int i = 0; i < argc; i++) std::cout << std::setw(3) << i + 1 << ": " << argv[i] << ' '; std::cout << "-------------------------- ";
sdds::Reservation** ppReservations = nullptr; size_t cnt = 0;
// Process the file if (argc > 1) { std::ifstream file(argv[1]); if (!file) { std::cerr << "ERROR: Cannot open file [" << argv[1] << "]. "; return 1; }
std::string strReservation; // count how many records are in the file do { std::getline(file, strReservation);
// if the previous operation failed, the "file" object is // in error mode if (file) { // Check if this is a commented line. // In the input file, commented lines start with '#' if (strReservation[0] != '#') ++cnt; } } while (file);
ppReservations = new sdds::Reservation*[cnt]; cnt = 0;
// read again from the file, but this time load and store data file.clear(); file.seekg(std::ios::beg); do { std::getline(file, strReservation);
// if the previous operation failed, the "file" object is // in error mode if (file) { // Check if this is a commented line. // In the input file, commented lines start with '#' if (strReservation[0] != '#') { ppReservations[cnt] = new sdds::Reservation(strReservation); ++cnt; } } } while (file); file.close(); } std::cout << " Reservations -------------------------- "; for (auto i = 0u; i < cnt; ++i) { std::cout << *ppReservations[i]; } std::cout << "-------------------------- ";
// modify dates for some reservations if (cnt > 2) { ppReservations[0]->update(7, 6); ppReservations[2]->update(6, 6); }
std::cout << " Updated Reservations -------------------------- "; for (auto i = 0u; i < cnt; ++i) { std::cout << *ppReservations[i]; } std::cout << "-------------------------- ";
// cleanup for (auto i = 0u; i < cnt; ++i) delete ppReservations[i]; delete[] ppReservations;
return 0; }
output:
Command Line : -------------------------- 1 : ws 2 : data.txt --------------------------
Reservations -------------------------- Reservation 1234 : John &..n@email.com> Drinks on day 3 @ 5:00 for 2 people. Reservation ab defghij : David &..d@email.com> Breakfast on day 4 @ 6:00 for 1 person. Reservation RES - 003 : Sara &..a@email.com> Breakfast on day 5 @ 7:00 for 2 people. Reservation RES - 004 : Ana &..a@email.com> Breakfast on day 5 @ 8:00 for 1 person. Reservation RES - 005 : John &..n@email.com> Breakfast on day 4 @ 9:00 for 1 person. Reservation RES - 006 : Vanessa &..a@email.com> Drinks on day 3 @ 10:00 for 2 people. Reservation RES 007 : Mike &..e@email.com> Lunch on day 4 @ 11:00 for 4 people. Reservation RES - 008 : Mike &..e@email.com> Lunch on day 5 @ 12:00 for 8 people. Reservation RES - 009 : Dan &..n@email.com> Lunch on day 3 @ 13:00 for 2 people. Reservation RES - 010 : Donna &..a@email.com> Lunch on day 5 @ 14:00 for 5 people. Reservation RES - 011 : Ana &..a@email.com> Lunch on day 4 @ 15:00 for 4 people. Reservation RES - 012 : Jon Snow &..n@email.com> Drinks on day 5 @ 16:00 for 2 people. Reservation RES - 013 : Sara &..a@email.com> Dinner on day 3 @ 17:00 for 6 people. Reservation RES - 014 : Jennifer &..n@email.com> Dinner on day 5 @ 18:00 for 6 people. Reservation RES - 015 : Stan &..n@email.com> Dinner on day 4 @ 19:00 for 5 people. Reservation RES - 016 : Chris &..s@email.com> Dinner on day 4 @ 20:00 for 3 people. Reservation RES - 017 : Vanessa &..a@email.com> Dinner on day 4 @ 21:00 for 4 people. Reservation RES - 018 : David &..d@email.com> Drinks on day 5 @ 22:00 for 4 people. Reservation RES - 019 : Chris &..s@email.com> Drinks on day 3 @ 23:00 for 1 person. Reservation RES - 020 : Donna &..a@email.com> Drinks on day 4 @ 24:00 for 3 people. --------------------------
Updated Reservations -------------------------- Reservation 1234: John &..n@email.com> Breakfast on day 7 @ 6:00 for 2 people. Reservation ab defghij : David &..d@email.com> Breakfast on day 4 @ 6:00 for 1 person. Reservation RES - 003 : Sara &..a@email.com> Breakfast on day 6 @ 6:00 for 2 people. Reservation RES - 004 : Ana &..a@email.com> Breakfast on day 5 @ 8:00 for 1 person. Reservation RES - 005 : John &..n@email.com> Breakfast on day 4 @ 9:00 for 1 person. Reservation RES - 006 : Vanessa &..a@email.com> Drinks on day 3 @ 10:00 for 2 people. Reservation RES 007 : Mike &..e@email.com> Lunch on day 4 @ 11:00 for 4 people. Reservation RES - 008 : Mike &..e@email.com> Lunch on day 5 @ 12:00 for 8 people. Reservation RES - 009 : Dan &..n@email.com> Lunch on day 3 @ 13:00 for 2 people. Reservation RES - 010 : Donna &..a@email.com> Lunch on day 5 @ 14:00 for 5 people. Reservation RES - 011 : Ana &..a@email.com> Lunch on day 4 @ 15:00 for 4 people. Reservation RES - 012 : Jon Snow &..n@email.com> Drinks on day 5 @ 16:00 for 2 people. Reservation RES - 013 : Sara &..a@email.com> Dinner on day 3 @ 17:00 for 6 people. Reservation RES - 014 : Jennifer &..n@email.com> Dinner on day 5 @ 18:00 for 6 people. Reservation RES - 015 : Stan &..n@email.com> Dinner on day 4 @ 19:00 for 5 people. Reservation RES - 016 : Chris &..s@email.com> Dinner on day 4 @ 20:00 for 3 people. Reservation RES - 017 : Vanessa &..a@email.com> Dinner on day 4 @ 21:00 for 4 people. Reservation RES - 018 : David &..d@email.com> Drinks on day 5 @ 22:00 for 4 people. Reservation RES - 019 : Chris &..s@email.com> Drinks on day 3 @ 23:00 for 1 person. Reservation RES - 020 : Donna &..a@email.com> Drinks on day 4 @ 24:00 for 3 people. --------------------------
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
