In: Computer Science
C++ programming
Instructions
Create a ShopCart class that allows you to add items to a shopping cart and get the total price of purchases made.
Items are simply described by an Item class as follows:
class Item {
public:
std :: String description;
float price;
};
The ShopCart class must be able to add and remove items and display an invoice. This class must use a dynamically allocated array of items whose capacity is fixed in advance to the build. The class must include the following methods:
• a constructor specifying the capacity of the cart;
• a copy constructor;
• an addsItem method allowing to add to the cart the item in argument (passage by reference). This method returns true if the addition was made and false if the basket is full;
• an removeItem method to remove the item for which the description is given (std :: String). It will return false if no item in the basket has a description corresponding to that given;
• an billInfo method to display the list of items purchased, their individual price and the total purchase price;
• a destroyer.
Your classes must be in the shop.h file, but all methods must have their definition in the shop.cpp file. You must also create a main function (contained in the dev1.cpp file) to test this class.
ShopCart.h
#ifndef SHOPCART_H_INCLUDED
#define SHOPCART_H_INCLUDED
//item
class Item
{
public:
std :: string description;
float price;
//constructor
Item()
{
this->description="";
this->price=0;
}
//set item
void setItem(std::string d,float p)
{
this->description=d;
this->price=p;
}
};
//shopcart
class ShopCart
{
public:
//variables
int maxCapacity;
int totalItem;
Item* it[50]; //item
//methods
ShopCart();
~ShopCart();
bool addsItem (Item* i);
bool removeItem (std::string);
void displayBill();
};
#endif // SHOPCART_H_INCLUDED
ShopCart.cpp
#include <iostream>
#include "ShopCart.h"
using namespace std;
//constructor
ShopCart::ShopCart()
{
this->maxCapacity=10;
this->totalItem=0;
}
//destructor
ShopCart::~ShopCart()
{
cout<<"\nDestructor called\n";
}
//add item
bool ShopCart::addsItem (Item* i)
{
//check capacity
if(totalItem<maxCapacity)
{
this->it[totalItem]=i;
totalItem++;
return true; //return true if item added
}
return false; //else return false
}
//remove item
bool ShopCart::removeItem (string name)
{
//check cart empty
if(totalItem==0)
return false;
//if cart has item
for(int i=0;i<totalItem;i++)
{
if(it[i]->description==name) //if item found
{
if(i<totalItem-2)
{
for(int j=i;j<totalItem-1;j++)
it[j]=it[j+1];
}
totalItem--;
return true;
}
}
return false; //else return false
}
//display the bill
void ShopCart::displayBill()
{
float total=0;
for(int i=0;i<totalItem;i++)
{
cout<<"\n"<<it[i]->description<<"
"<<it[i]->price;
total+=it[i]->price;
}
cout<<"\n\nTotal Amount: "<<total<<"\n"; //total
amount print
}
main.cpp
#include <iostream>
#include <string>
#include "ShopCart.h"
using namespace std;
int main()
{
//variable declaration
int c,n=0;
string name;
float price;
//classes
ShopCart sc;
Item it[20];
//menu
do{
cout<<"\n1.Add Item"
<<"\n2.Remove Item"
<<"\n3.Display Bill"
<<"\n4.Exit"
<<"\n\nEnter choice: ";
cin>>c;
switch(c)
{
//add item
case 1:
{
cout<<"\nEnter item description: ";
cin>>name;
cout<<"Enter price: ";
cin>>price;
it[n].setItem(name,price);
//check cart
if(sc.addsItem(&it[n])){
cout<<"\nitem added successfully";
n++;
}
else
cout<<"\ncart is full";
}
break;
//remove item
case 2:
cout<<"\nEnter item description: ";
cin>>name;
//check cart
if(sc.removeItem(name))
cout<<"\nitem removed successfully";
else
cout<<"\ncart is empty";
break;
//display bill
case 3:
sc.displayBill();
break;
//exit
case 4:
return 0;
default:
cout<<"\nInvalid choice\n";
}
}while(c<3);
return 0;
}
output:
//for any clarification , please do comments, if you found this solution useful,please give me thumbs up