Problem Description (to be solved in C++): When shopping online, you select item
ID: 3586138 • Letter: P
Question
Problem Description (to be solved in C++):
When shopping online, you select items and add them to a shopping cart. Duplicate items are permitted in a shopping cart, as you can purchase multiples of the same item. You also can remove an item from a shopping cart, if you change your mind about buying it. The shopping cart can show its current contents with their prices and the total cost of these items. Design the ADT item and shopping cart.
Requirements
Shopping cart
o Design the shopping cart as a derived class from the Bag class
o Declare total price as a data member
o Define a default constructor
o Define a member function to get the total price
o Override the base class methods: add and remove
Item
Declare three data members: name of item, unit price of item, quantity of item
Define a default constructor
Define a constructor that initializes all three data members by the parameters.
Define set and get functions for each data member.
Define the following operators:
Equal to: ==
Input : >>
Output: <<
Write and document the class in header and implementation files, and compile it separately from the client program.
Explanation / Answer
#include <iostream>
#include <vector>
#include <cstring>
#include <string.h>
using namespace std;
//Class Item definition
class Item
{
private:
//Data member to store name
string name;
//Data member to store quantity
int quantity;
//Data member to store price
double price;
public:
//Default constructor
Item()
{
name = "";
quantity = 0;
price = 0;
}//End of constructor
//Parameterized constructor
Item(string n, int q, double p)
{
name = n;
quantity = q;
price = p;
}//End of constructor
//Function to Set name
void setName(string itemName)
{
name = itemName;
}//End of function
//Function to Set price
void setPrice(double itemPrice)
{
price = itemPrice;
}//End of function
//Function to Set quantity
void setQuantity(int itemQuantity)
{
quantity = itemQuantity;
}//End of function
//Function to return name
string getName()
{
return name;
}//End of function
//Function to return price
double getPrice()
{
return price;
}//End of function
//Function to return quantity
int getQuantity()
{
return quantity;
}//End of function
};//End of class
//Class definition
class ShoppingCart
{
private:
//Data member object to store item class objects
vector<Item> item;
//To store total price
int total_price;
public:
//Prototype of member functions
ShoppingCart();
void addItem(string name, int quantity, int price);
void removeItem(string name, int quantity);
int getTotal();
void displayItems();
};//End of class
//Default constructor
ShoppingCart::ShoppingCart()
{
cout << " Creating a new shopping cart" << endl;
total_price = 0;
}//End of constructor
//Function to add an item to the shopping cart
void ShoppingCart::addItem(string name, int quantity, int price)
{
//Loops till size of the list
for (int i = 0; i < item.size(); i++)
{
//Checks the current item name with name given
if (item[i].getName() == name)
{
//If it is present then updates the quantity
item[i].setQuantity(item[i].getQuantity() + quantity);
return;
}//End of if
}//End of for
//Creates an temporary object of Item class
Item temp;
//Set the information for the new item
temp.setName(name);
temp.setQuantity(quantity);
temp.setPrice(price);
//Add it to the cart
item.push_back(temp);
}//End of function
//Function to remove an specified item from the shopping cart
void ShoppingCart::removeItem(string name, int quantity)
{
//Loops till the size of the item list
for (int i = 0; i < item.size(); i++)
{
//Compares the current item name with the name specified
if (item[i].getName() == name)
{
//Checks the current quantity is greater than or equals to the specified quantity
if (item[i].getQuantity() >= quantity)
{
//Update the quantity
item[i].setQuantity(item[i].getQuantity() - quantity);
return;
}//End of inner if
//If current quantity is less than the specified quantity
//Displays the message enough quantity is not found
cout << " Not enough items present in the cart to be removed" << endl;
return;
}//End of outer if
}//End of for
//If item not found displays the following message
cout << " This following item is not present in the cart: " << endl;
cout<<" Name: "<<name<<" Quantity: "<<quantity<<endl;
}//End of function
//Function to calculate and return total price
int ShoppingCart::getTotal()
{
//Initializes to zero
total_price = 0;
//Loops till size of the item list
for (int i = 0; i < item.size(); i++)
{
//Calculate the total
total_price += item[i].getQuantity()* item[i].getPrice();
}//End of for
//Returns the total price
return total_price;
}//End of function
//Function to display the Item in the shopping cart list
void ShoppingCart::displayItems()
{
cout<<" ********************* Item Information *********************** ";
//Loops till size of the item
for (int i = 0; i < item.size(); i++)
{
cout<<" Name: "<<item[i].getName()<<" Quantity: "<<item[i].getQuantity()<<" Price: "<<item[i].getPrice();
}//End of for
}//End of function
//Main function definition
int main()
{
//Creates an object of the shopping cart class
ShoppingCart cart;
//Adds an item to shopping cart
cart.addItem("Maggi", 10, 5);
//Displays the item list
cart.displayItems();
//Adds an item to shopping cart
cart.addItem("Biryani", 2, 15);
//Displays the item list
cart.displayItems();
//Adds an item to shopping cart
cart.addItem("Ketchup", 1, 5);
//Displays the item list
cart.displayItems();
cout << " Total cost: " << cart.getTotal() << endl;
//Adds an item to shopping cart
cart.addItem("Football", 2, 15);
cout << " Total cost: " << cart.getTotal() << endl;
//Displays the item list
cart.displayItems();
//Removes an item from shopping cart
cart.removeItem("Maggi", 4);
cout << " Total cost: " << cart.getTotal() << endl;
//Displays the item list
cart.displayItems();
//Removes an item from shopping cart
cart.removeItem("Ketchup", 2);
cout << " Total cost: " << cart.getTotal() << endl;
//Displays the item list
cart.displayItems();
//Adds an item to shopping cart
cart.addItem("Candy", 15, 2);
cout << " Total cost: " << cart.getTotal() << endl;
//Displays the item list
cart.displayItems();
//Removes an item from shopping cart
cart.removeItem("Bat", 1);
//Displays the item list
cart.displayItems();
cout << " Total cost: " << cart.getTotal() << endl;
}//End of function
Sample Run:
Creating a new shopping cart
********************* Item Information ***********************
Name: Maggi Quantity: 10 Price: 5
********************* Item Information ***********************
Name: Maggi Quantity: 10 Price: 5
Name: Biryani Quantity: 2 Price: 15
********************* Item Information ***********************
Name: Maggi Quantity: 10 Price: 5
Name: Biryani Quantity: 2 Price: 15
Name: Ketchup Quantity: 1 Price: 5
Total cost: 85
Total cost: 115
********************* Item Information ***********************
Name: Maggi Quantity: 10 Price: 5
Name: Biryani Quantity: 2 Price: 15
Name: Ketchup Quantity: 1 Price: 5
Name: Football Quantity: 2 Price: 15
Total cost: 95
********************* Item Information ***********************
Name: Maggi Quantity: 6 Price: 5
Name: Biryani Quantity: 2 Price: 15
Name: Ketchup Quantity: 1 Price: 5
Name: Football Quantity: 2 Price: 15
Not enough items present in the cart to be removed
Total cost: 95
********************* Item Information ***********************
Name: Maggi Quantity: 6 Price: 5
Name: Biryani Quantity: 2 Price: 15
Name: Ketchup Quantity: 1 Price: 5
Name: Football Quantity: 2 Price: 15
Total cost: 125
********************* Item Information ***********************
Name: Maggi Quantity: 6 Price: 5
Name: Biryani Quantity: 2 Price: 15
Name: Ketchup Quantity: 1 Price: 5
Name: Football Quantity: 2 Price: 15
Name: Candy Quantity: 15 Price: 2
This following item is not present in the cart:
Name: Bat Quantity: 1
********************* Item Information ***********************
Name: Maggi Quantity: 6 Price: 5
Name: Biryani Quantity: 2 Price: 15
Name: Ketchup Quantity: 1 Price: 5
Name: Football Quantity: 2 Price: 15
Name: Candy Quantity: 15 Price: 2
Total cost: 125
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.