Design a class called ItemToPurchase This class has the foll
Design a class called ItemToPurchase.
This class has the following attributes: item_name, item_price, and item_quanity
The ItemToPurchase class should have methods to return the current values of these attributes. In addition, this class has a method get_item_cost to
return the cost of this item (which is the product of item_price and item_quantity)
Design a class called Customer.
This class has the following attributes: customer_name, shopping_date, and shopping_cart. The shopping_cart attribute is a list of ItemToPurchase
objects.
The Customer class should have methods to get values of the name and date attributes, to add items into the shopping, and to remove items from the
shopping cart. Item removal should be done based on item name; that is, the item_remove method takes item_name as a parameter, searches an item
using the given item_name, and removes the found item from the shopping cart.
The Customer class also has a method for checking out, which should display the following information:
custoerm name, shopping date, a list of purchased items with item_name, item_quanity, and item_price, and the total cost of all the items in the
shopping cart.
In the main function, the program should create a Customer object, then ask the user to enter how many items the customer will purchase and provide
the name, price, and quantity for each item. The program adds all the items to the shopping cart. Then the program asks the user to enter the number of
items to remove from the shopping cart and provide names for these items. The program removes these items from the shopping cart. Then the
program performs check out function for the customer to display the customer name, shopping date, a list of purchased items, and the total cost.
The following is a sample output of the program:
Enter Customer Name: Mark Smith
Enter Shopping Date: November 2016
How many items does the customer want add into shopping cart? 5
Item #1
Item Name: Bottle Water
Item Price: 1.5
Item Quantity: 20
Item #2
Item Name: Chips
Item Price: 3.5
Item Quantity: 6
Item #3
Item Name: Soda
Item Price: 4.0
Item Quantity: 10
Item #4
Item Name: Coffe
Item Price: 6.5
Item Quantity: 3
Item #5
Item Name: Tea
Item Price: 5.5
Item Quantity: 4
How many items does the customer want to remove from the shopping cart? 2
Item name for removing: Soda
Soda has been removed from shopping cart.
Item name for removing: Tea
Tea has been removed from shopping cart.
Checking out
Customer Name: Mark Smith
Shopping Date: November 2016
20 Bottle Water @ $1.5
6 Chips @ $3.5
3 Coffe @ $6.5
Total Amount: $70.5
Solution
#include<iostream>
 #include<string>
 #include<stdio.h>
 using namespace std;
 int c = 0;
 //Declaration of class Item to purchase
 class ItemToPurchase
 {
 //Private data member
 string item_name;
 double item_price, item_quanity;
 public:
 //Sets the item quantity
 void setIQty(double q)
 {
 item_quanity = q;
 }
 //Sets the item price
 void setIPrice(double p)
 {
 item_price = p;
 }
 //Sets the item name
 void setIName(string s)
 {
 item_name = s;
 }
 //returns item name
 string getIName()
 {
 return item_name;
 }
 //returns item price
 double getIPrice()
 {
 return item_price;
 }
 //returns item quantity
 double getIQty()
 {
 return item_quanity;
 }
 };
 //creates a class customer from Item to purchase class
 class Customer : public ItemToPurchase
 {
 //Declaration of private data member
 string customer_name, shopping_date;
 ItemToPurchase shopping_cart[5];
 public:
 //Adds an item
 void addItem(string n, double p, double q)
 {
 fflush(stdin);
 shopping_cart[c].setIName(n);
 fflush(stdin);
 shopping_cart[c].setIPrice(p);
 fflush(stdin);
 shopping_cart[c].setIQty(q);
 c++; //Counter for number of items
 }
 //Displays item information
 void disp()
 {
 double amt = 0.0;
 cout<<\"\ \  Check out \ \";
 cout<<\"\  Customer Name: \"<<getCName();
 cout<<\"\  Shopping Date: \"<<getCDate();
 for(int t = 0; t < c; t++)
 {
 cout<<endl<<shopping_cart[t].getIQty()<<\" \"<<shopping_cart[t].getIName()<<\" @ $ \"<<shopping_cart[t].getIPrice();
 amt = amt + shopping_cart[t].getIQty() * shopping_cart[t].getIPrice();
 }
 cout<<\"\ Total Amount: $ \"<<amt;
 }
 //Removes an item
 void removeItem(string s)
 {
 int t, f = -1;
 for(t = 0; t < c; t++)
 {
 string temp = shopping_cart[t].getIName();
 //Compares the item to be removed
 if(s.compare(temp) == 0)
 {
 f = t;
cout<<\"\  Position: \"<<f;
 break;
 }
 }
 if(f != -1)
 {
 cout<<s<<\" has been removed from shopping cart.\ \";
 for(t = f; t < c - 1; t++)
 {
 fflush(stdin);
 shopping_cart[t].setIName(shopping_cart[t+1].getIName());
 cout<<shopping_cart[t].getIName()<<endl;
 shopping_cart[t].setIQty(shopping_cart[t+1].getIQty());
 cout<<shopping_cart[t].getIQty()<<endl;
 shopping_cart[t].setIPrice(shopping_cart[t+1].getIPrice());
 cout<<shopping_cart[t].getIPrice()<<endl;
 }
 c--; //Decrease the counter for number of items
 }
 else
 {
 cout<<\"\  Item \"<<s<<\" Not found\";
 }
 }
//Sets the purchase date
 void setCDate(string d)
 {
 shopping_date = d;
 }
 //Sets the customer name
 void setCName(string s)
 {
 customer_name = s;
 }
 //Returns the customer name
 string getCName()
 {
 return customer_name;
 }
 //Returns the customer date
 string getCDate()
 {
 return shopping_date;
 }
 //Returns the number of item purchased
 };
 int main()
 {
 //Creates the object for customer
 Customer cu;
 int n, r, t;
 double q, p;
 std::string a;
 cout<<\"Enter Customer Name: \";
 fflush(stdin);
 std::getline(std::cin,a);
 cu.setCName(a);
 cout<<\"Enter Shopping Date: \";
 fflush(stdin);
 std::getline(std::cin,a);
 cu.setCDate(a);
 cout<<\"How many items does the customer want add into shopping cart? \";
 fflush(stdin);
 cin>>n;
 //Enter the item details
 for(t = 0; t < n; t++)
 {
 cout<<\"\ Item # \"<<t;
 cout<<\"\  Item Name: \";
 fflush(stdin);
 std::getline(std::cin,a);
 cout<<\"\ Item Price: \";
 fflush(stdin);
 cin>>p;
 cout<<\"\ Item Quantity: \";
 fflush(stdin);
 cin>>q;
 cu.addItem(a, p, q);
 }
 cout<<\"\  How many items does the customer want to remove from the shopping cart? \";
 cin>>r;
 for(t = 0; t < r; t++)
 {
 cout<<\"\  Item name for removing: \";
 fflush(stdin);
 std::getline(std::cin,a);
 cu.removeItem(a);
 }
 cu.disp();
 }
Output:
Enter Customer Name: Mohan
 Enter Shopping Date: 22 Dec 2015
 How many items does the customer want add into shopping cart? 5
Item # 0
 Item Name: Lux
Item Price: 10
Item Quantity: 3
Item # 1
 Item Name: Dove
Item Price: 20
Item Quantity: 2
Item # 2
 Item Name: Colgate
Item Price: 30
Item Quantity: 2
Item # 3
 Item Name: Cinthol
Item Price: 20
Item Quantity: 3
Item # 4
 Item Name: Mouse
Item Price: 60
Item Quantity: 1
How many items does the customer want to remove from the shopping cart? 2
Item name for removing: Cinithol
Item Cinithol Not found
 Item name for removing: Mouse
Position: 4 Mouse has been removed from shopping cart.
 Check out
Customer Name: Mohan
 Shopping Date: 22 Dec 2015
 3 Lux @ $ 10
 2 Dove @ $ 20
 2 Colgate @ $ 30
 3 Cinthol @ $ 20
 Total Amount: $ 190






