Question
ID Description Quantity Price Tax rate
B003 C++ Programming CD 2 30.5 3.5%
A001 iPhone 6 Plus 1 700 7%
A002 Macbook Pro 1 800 7%
Task 1. Declare a struct OrderItem to help store information for an item in an order.
Task 2. Declare a class Order (within the submitted cpp file) with the following requirements:
a. Have an array of OrderItem structs to store the items of an order. Since the number of ordered items in an order varies, and this class should have a field to store that number and the array of OrderItem should be a dynamic array.
b. Have two methods to load and save the order to a CSV (comma-separated values) file. Their prototypes are
void loadCSV(char *filename);
void saveCSV(char *filename);
For example, the previous order will be saved as a CSV file with the following content:
ID,Description,Quantity,Price,Tax
B003,C++ Programming CD,2,30.5,3.5%
A001,iPhone 6 Plus,1,700,7%
A002,Macbook Pro,1,800,7%
Hint: When loading an order from a CSV file, you should read it quickly to determine the number of items. Then you can allocate the array of OrderItem with size given by that number. Finally you re-read the file and parse each line as an item in that array.
c. Have a method to compute the total of an order, including taxes. For example, the previous order has the total of
2 * 30.5 * (1 + 0.035) + 1 * 700 * (1 + 0.07) + 1 * 800 * (1 + 0.07) = 1668.135
Its prototype is:
double computeTotal()
d. Have a method to update the quantity of an item in an order. Its prototype is
void updateQuantity(string itemID, int quantity)
For example, if the shopper decides to buy one C++ Programming CD rather than two, this function will be called by:
order.updateQuantity("B003", 1);
This function should compare the old quantity and the new quantity and print out an error message if they are the same.
Task 3. Write a main() function to test your code. The testing procedure is as the following:
1 Load the order from a CSV file.
2 Compute the total and compare it with your pre-computed value.
3 Update quantity of two different items in the order.
4 Save the updated order to another CSV file and manually check that file.
You will create at least two CSV files for this (Hint: You could use Excel to create them and pre-compute the totals). Remember to attach those two CSV files as comments in the submitted cpp file.
Solution Preview
This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.
#include <iostream>#include <string>
#include <fstream>
using namespace std;
struct OrderItem {
int quantity;
string description, id;
double price, taxRate;
};
class Order {
OrderItem *items;
int arraySize;
public:
void loadCSV(char *fileName)
{
arraySize = 0;
string data;
ifstream inFile;
inFile.open(fileName);
if(!inFile.is_open())
{
cout << "File: " << fileName << " does not exist, exiting program...\n";
exit(0);
}
getline(inFile, data);
while(getline(inFile, data))
arraySize++;
items = new OrderItem[200];
inFile.close();
inFile.open(fileName);
getline(inFile, data);
for(int i = 0; i < arraySize; i++)
{...