Question
An online store would like you to write a C++ program that reads, stores, and displays customer orders that occurred on a single day. Each
An online store would like you to write a C++ program that reads, stores, and displays customer orders that occurred on a single day. Each order involves a single product and consists of an ID for the order, price of the product, quantity of the product, and the time of the order in military time using hours, minutes, and seconds.
DO NOT delete nor change the code already given to you in the code template. You will add your code to this template to formulate your solution. Here is a sample run of the code (user input is in bold):
> run Enter today's orders: Order #1 - ID: a1Price: 20.50Quantity: 100Time (hrs, min, sec): 10 45 30Order a1, $20.50, 100 units, costs $2050.00, taken @ 10:45:30 Order #2 - ID: 3bPrice: 30.25Quantity: 10Time (hrs, min, sec): 8 0 0Order 3b, $30.25, 10 units, costs $302.50, taken @ 08:00:00 Order #3 - ID: 45Price: 78.11Quantity: 456Time (hrs, min, sec): 23 10 2Order 45, $78.11, 456 units, costs $35618.16, taken @ 23:10:02 Order #4 - ID: Orders: Order a1, $20.50, 100 units, costs $2050.00, taken @ 10:45:30 Order 3b, $30.25, 10 units, costs $302.50, taken @ 08:00:00 Order 45, $78.11, 456 units, costs $35618.16, taken @ 23:10:02 Price Histogram: 20: 40: $$ 60: 80: $ 100:
Important!: Write your code incrementally. This means implement your solution one function at a time and fill in the relevant call(s) to the main function (if the call belongs here). When you implement a function, repeatedly compile, run, and test the function before implementing the next function. Use the provided test cases to help you arrive at your final solution. Take a look at the test cases now to get an idea of your program's expected behavior.
TASK 3: Military time operates on a 24-hour clock that uses the range 0 - 23 to represent the hours in a 24 hour period. Midnight (12am) is 0 hours, 1:00 a.m. is 1 hour, 2:00 a.m. is 2 hours, …, noon is 12 hours, 1pm is 13 hours, all the way to 11:00 p.m. being 23 hours. The class Time is a data type (see lecture notes) that represents a military time value consisting of hours, minutes, and seconds (see the data members defined in the class). For example, if these data members are set to hours = 13, minutes = 35, and seconds = 45, this military time represents 1:35pm and 45 seconds. The code for the member functions setHours, setMinutes, setSeconds, getHours, getMinutes, and getSeconds has been provided for you. Do NOT modify this code.
TASK 3a: Write code for the member function toString in the class Time by replacing /* INSERT YOUR CODE */ with your solution. This member function returns a string with the format "HH:MM:SS" that contains the hours, minutes, and seconds stored in an object, i.e. an instance of the class Time. To ensure that the values are expressed in this two digit format, call the function in TASK 3b, which converts a number to a string containing two digit characters. You can test the toString function by running the following code in the main function:
Time time; time.setHours(3); time.setMinutes(29); time.setSeconds(9); cout << time.toString() << endl;
Here the toString function returns the string "03:29:09", i.e. 3:29am and 9 seconds.
TASK 3b: Write code for the member function twoDigits in the class Time by replacing /* INSERT YOUR CODE */ with your solution. This function converts an integer to a string with length two. Use the C++ to_string function to convert a number to a string. For example, to_string(29) evaluates to the string "29". Your code must ensure that a single digit number will be preceded by a leading zero in the returned string. Here are examples: the function call twoDigits(0) returns "00"; the function call twoDigits(5) returns "05"; the function call twoDigits(25) returns "25." This function is called by the toString function in class Time (see TASK 3a).
TASK 4: Write code for the accessor and mutator member functions in the class Order by replacing /* INSERT YOUR CODE */ with your solution.
TASK 4a: Write code in the member function display in the class Order by replacing /* INSERT YOUR CODE */ with your solution. This function displays to the screen the data member values (id, price, quantity), the total cost (price x quantity), and the time the order was taken. The price and total cost are displayed with two digits after the decimal place. Call the toString function in the class Time in this function. You can test the display function by running the following code in the main function:
Time time; Order order; time.setHours(3); time.setMinutes(29); time.setSeconds(9); order.setID("aa"); order.setPrice(5.5); order.setQuantity(10); order.setTime(time); order.display();
Here the display function will display to the screen:
Order aa, $5.50, 10 units, costs $55.00, taken @ 03:29:09
TASK 5: Write code in the main function (remove code you may have inserted into the main to debug TASK 3 and TASK 4) using the algorithm specified in the comments. Write code in the main function to do the following:
i. Define a constant that holds the integer 100.0, which is the highest possible price for any product that the user is allowed to enter.
ii. Call the function in TASK 6 that reads all orders from the user.
iii. If the user entered at least one order then display all the orders (call the function in TASK 13) and draw a histogram in bar graph form of the prices (call the function in TASK 14) of the entered orders..
iv. You will write nine functions as specified below. Choose descriptive function and variable names.
Place function prototypes BEFORE the main function after the comment // FUNCTION PROTOTYPES GO HERE:
Place function definitions AFTER the main function after the comment // FUNCTION DEFINITIONS GO HERE:
Each function should have a comment (placed above the function definition) that describes the function's task.
Include in this comment a short description of each input parameter.
TASK 6: Write a procedure to read all of the orders from the user. The function has two input parameters: 1) a vector holding values of type class Order (temperature samples entered by the user) and 2) an integer holding the highest price that the user can enter (see TASK 5 part i). Here is the algorithm for the function:
Display the phrase "Enter today's orders:".
Before reading each order from the user display the phrase "Order #?? -", where ?? is an integer starting from one indicating which order the user is currently entering.
Read each order (ID, price, quantity, and time) by calling the functions in TASK 7, TASK 10, and TASK 11. Stop reading orders when the user enters no characters (just clicks the Enter key) when entering the ID. Display the order to the screen before reading the next order.
Add the order into the vector in the input parameter.
You must determine where to use the cin and getline statements. You will encounter an issue with user input when you read a number followed by a string. Read zyBooks chapter 2.15 (Section 2.15 in the section "Mixing cin and getline") to solve this problem.
TASK 7: Write a function to read a valid ID from the user. The function has one input parameter: a vector holding values of type class Order. The function returns a string. A valid ID contains exactly two characters where either character can be a lower case letter or a digit. Repeatedly ask the user again for an ID if an invalid ID is entered and display the phrase "Invalid id, try again." in this case. Call the function in TASK 8 to determine whether a character is a lower case letter or a digit Call the function in TASK 9 to determine if an ID has already been entered by the user. I.e., we don't allow duplicate IDs in the list of orders.
TASK 8: Write a function that determines whether a character is a lower case letter or a digit. The function has one input parameter: a character (not a string). The function returns a boolean value where true indicates that the character in the input parameter is either a lower case letter or a digit. Otherwise it returns false.
TASK 9: Write a function that determines whether an ID is already being used in a previous order. The function has two input parameters: a vector holding values of type class Order and 2) a string holding an ID. The function returns a boolean value where true indicates that the ID specified in the input parameter is already used in an order in the input vector. Otherwise it returns false.
TASK 10: Write a function that reads a price from the user. The function has one input parameter: a double type of value holding the highest price the user is allowed to enter. The function returns a double. Prompt the user with "Price: " and repeatedly ask the user again if an invalid price is entered and display the phrase "Invalid price, try again." in this case. A valid price is a positive number (remember zero is not a positive number) that does not exceed the input parameter value.
TASK 11: Write a function that reads a quantity from the user. The function has no input parameters. The function returns an integer. Prompt the user with "Quantity: " and repeatedly ask the user again if an invalid price is entered and display the phrase "Invalid quantity, try again." in this case. A valid quantity is a positive number (remember zero is not a positive number).
TASK 12: Write a function that reads a single military time value (hours, minutes, and seconds) from the user. The function has no parameters. The function returns an instance (object) of the class Time containing the military time values entered by the user. Prompt the user with the phrase "Time (hrs, min, sec): " and read in the hours, minutes, and seconds. The hours must be an integer in the range 0 - 23, the minutes must be an integers in the range 0 - 59, and the seconds must be an integer in the range 0 - 59. Repeatedly ask the user again if the entered values violate any of these range restrictions and display the phrase "Invalid time, try again." in this case. Use the mutator member functions of the class Time to store these value into an object.
TASK 13: Write a procedure that displays all of the orders. The function has one input parameter: a vector holding values of type class Order. The procedure displays the phrase "Orders:" followed by each order, one per line. Avoid writing redundant code by calling a member function in the class Order.
TASK 14: Write a procedure that displays a bar graph containing prices from a list of order. The procedure has two input parameters: 1) a vector holding values of type class Order and 2) an integer holding the highest possible price. The procedure displays the phrase "Price Histogram: " followed by the bar graph, which displays rows labeled with prices in increments of 20 dollars up to the highest possible dollars. For example, if the second input parameter holds 100, then the bar graph will display five rows with the prices 20, 40, 60, 80, and 100. Each of these prices represents a price range that includes that price. Thus in the same example, the price 20 represents the prices in the range (0 - 20], temperature 40 represents the range (20 - 40], the price 60 represents the range (40 - 60], the price 80 represents the range (60 - 80], and the price 100 represents the range (80 - 100]. As another example, if the second input parameter to this procedure holds 200, then the bar graph will display 10 rows with the prices 20, 40, 60, 80, 100, 120, 140, 160, and 200. Each row will display a count of how many prices in the list of orders (first input parameter) contain a price in the price range indicated by that row. The count is displayed as sequence of dollar sign characters, $, whose number matches the count.
Be sure that there is a comment documenting each variable.
Be sure that your if statements, for and while loops and blocks are properly indented.
Test your solution.
Here is the code.
8 #include 9 #include 10 #include 11 #include 12 13 using namespace std; 14 15 class Time int seconds; string twoDigits (const int num) const; 16 { 17 private: 18 int hours; 19 int minutes; 20 21 22 23 24 25 26 27 28 29 30 31 }; 32 public: void setHours(const int hrs); void setMinutes (const int mins); void setSeconds (const int secs); int getHours() const; int getMinutes() const; int getSeconds() const; string toString() const; 33 class Order 34 { 35 private: 36 37 double price; int quantity; 38 string id; 39 Time time; 40 41 public: 42 43 44 45 46 void setPrice (const double cost); void setQuantity (const int quant); void setID (const string ident); void setTime (const Time t); double getPrice() const; 47 int getQuantity() const; 48 string getID() const; 49 Time getTime() const; 50 51}; void display () const;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started