Requirements ( needs to work with C++ please, Thank you .) Define a class called
ID: 3712719 • Letter: R
Question
Requirements ( needs to work with C++ please, Thank you .)
Define a class called Cart as below
The arrays descriptions, amounts and qty act as parallel arrays.
Write a default constructor that will initialize count and total to zero.
Write a public function void add_item(string description, double amount, int quantity). A shopping cart item is represented by a description, and amount and a quantity. Use this function to add an item into the descriptions, amount and qty arrays. Make sure to increment countwith each addition. Make sure to not allow adding more than 25 items (this is the maximum size of the array). Do not cout/cin for input in this function.
Write a private member function void calc_total(). This function will go through the parallel arrays calculating the total amount of items added so far. Make sure to use the count variable to know how many items in the arrays actually contain valid items. Write this function outside of the class – do not write this class inline; use the scope resolution operator.
Every time add_item() is called, make sure to update total using the calc_total() function.
Write a public member function double get_total(). This will return the current total of the shopping cart.
Write a public member function double get_count(). This will return the number of items in the shopping cart.
Write a public member function string delete_last(). This function deletes the last item added to the parallel arrays. When the item is deleted, it should no longer be included when calculating the total. This function returns the description of the item just deleted. Call calc_total() after the delete to update the current total. There is a very simple way to do this — don’t overthink the solution.
None of the functions in the class should cout or cin.
In main, create a menu that will ask the user to add an item to the cart, show the current total or delete the last item in the cart.
Sample main()
Sample Interaction
Explanation / Answer
Here is the completed code. Implemented all the features and defined the main method to interact with the user as necessary. Comments are included wherever important. Thanks.
//Cart.cpp
#include<iostream>
#define MAX 25
using namespace std;
class Cart
{
private:
int count;
double total;
string descriptions[25];
double amounts[25];
int qty[25];
public:
//all method prototypes (declarations)
Cart();
void add_item(string, double, int);
void calc_total();
double get_total();
double get_count();
string delete_last();
};
//default constructor
Cart::Cart(){
count=0;
total=0;
}
//method to add an item to the cart
void Cart::add_item(string description, double amount, int quantity){
if(count<MAX){
descriptions[count]=description;
amounts[count]=amount;
qty[count]=quantity;
count++;
//updating the total
calc_total();
}
}
//method to calculate and update the total
void Cart::calc_total(){
total=0;
for(int i=0;i<count;i++){
total+=amounts[i]*qty[i];
}
}
//method to delete the last item, and return the description
string Cart::delete_last(){
if(count==0){
//empty
return "";
}else{
string des=descriptions[count-1];
//decrementing the count
count--;
//updating total
calc_total();
//returning removed item
return des;
}
}
//returns the total amount
double Cart::get_total(){
return total;
}
//returns the item count
double Cart::get_count(){
return count;
}
int main()
{
Cart cart;
char input;
do
{
cout<<endl;
cout << "a - Add an item" << endl;
cout << "d - Delete last item" << endl;
cout << "t - Show total" << endl;
cout << "q - Quit"<<endl;
cout << "Command: " << endl;
cin >> input;
string des;
double amt;
int qty;
switch (input)
{
case 'a': // asking user for item details
cout<<"Item description? ";
cin>>des;
cout<<"Amount? ";
cin>>amt;
cout<<"Quantity? ";
cin>>qty;
cart.add_item(des,amt,qty);// adding to cart
cout<<cart.get_count()<<" item(s) in cart"<<endl;
break;
case 'd':
//checking if cart is empty
if(cart.get_count()==0){
cout<<"Cart is empty!"<<endl;
}else{
//removing last element
string des=cart.delete_last();
cout<<des<<" is removed from cart"<<endl;
}
break;
case 't': // show total
cout<<"Cart Total: $"<<cart.get_total()<<endl;
break;
case 'q':
cout<<"bye!"<<endl;
}
} while (input != 'q');
return 0;
}
/*OUTPUT*/
a - Add an item
d - Delete last item
t - Show total
q - Quit
Command:
a
Item description? DVD
Amount? 200
Quantity? 12
1 item(s) in cart
a - Add an item
d - Delete last item
t - Show total
q - Quit
Command:
t
Cart Total: $2400
a - Add an item
d - Delete last item
t - Show total
q - Quit
Command:
a
Item description? iPhone
Amount? 345
Quantity? 34
2 item(s) in cart
a - Add an item
d - Delete last item
t - Show total
q - Quit
Command:
t
Cart Total: $14130
a - Add an item
d - Delete last item
t - Show total
q - Quit
Command:
d
iPhone is removed from cart
a - Add an item
d - Delete last item
t - Show total
q - Quit
Command:
t
Cart Total: $2400
a - Add an item
d - Delete last item
t - Show total
q - Quit
Command:
q
bye!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.