Question

In: Computer Science

C++ programming Instructions Create a ShopCart class that allows you to add items to a shopping...

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.

Solutions

Expert Solution

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


Related Solutions

C# Programming (Class Registration class) Add a Schedule property to the Person class. Add a new...
C# Programming (Class Registration class) Add a Schedule property to the Person class. Add a new behavior as well: add(Section s) this behavior will add a section to the Person’s schedule. The Person’s display() function will also need to be modified to display() the Person’s Schedule. Schedule class has Properties: An array of Sections and a Count. Constructors: only a default constructor is needed. Behaviors: add() and display(). This is my schedule class class Schedule     {         private int...
Calculator Class Instructions Create a calculator class that will add, subtract, multiply, and divide two numbers....
Calculator Class Instructions Create a calculator class that will add, subtract, multiply, and divide two numbers. It will have a method that will accept three arguments consisting of a string and two numbers example ("+", 4, 5) where the string is the operator and the numbers are what will be used in the calculation. The class must check for a correct operator (+,*,-,/), and a number (integer) for the second and third argument entered. The calculator cannot divide by zero...
Create a basic program (C programming language) that accomplishes the following requirements: Allows the user to...
Create a basic program (C programming language) that accomplishes the following requirements: Allows the user to input 2 numbers, a starting number x and ending number y Implements a while loop that counts from x (start) to y(end). Inside the loop, print to the screen the current number Print rather the current number is even or odd If the number is even , multiply by the number by 3 and print the results to the screen. If the number is...
1. Create a console program in C#, * Create a class: "Student.cs" * Add 3 variables:...
1. Create a console program in C#, * Create a class: "Student.cs" * Add 3 variables: StudentName (string), SchoolYear (int), YearsUntilGraduation(int) * Method YTK() = 12 - SchoolYear; 2. Main *Enter name *Enter age *You will attend school:____ years before graduating.
Instructions: program in C++, add pseudocode and comment throughout the program. Assignment: Create a program to...
Instructions: program in C++, add pseudocode and comment throughout the program. Assignment: Create a program to keep track of the statistics for a kid’s soccer team. The program will have a structure that defines what data the program will collect for each of the players. The structure will keep the following data: Players Name (string) Players Jersey Number (integer) Points scored by Player (integer) The program will have an array of 12 players (use less for testing and development, use...
WRITE IN C++ Create a class named Coord in C++ Class has 3 private data items...
WRITE IN C++ Create a class named Coord in C++ Class has 3 private data items               int xCoord;               int yCoord;               int zCoord; write the setters and getters. They should be inline functions               void setXCoord(int)             void setYCoord(int)            void setZCoord(int)               int getXCoord()                     int getYCoord()                   int getZCoord() write a member function named void display() that displays the data items in the following format      blank line      xCoord is                          ????????      yCoord is                          ????????      zCoord...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
C++ Programming. Create a class hierarchy to be used in a university setting. The classes are...
C++ Programming. Create a class hierarchy to be used in a university setting. The classes are as follows: The base class isPerson. The class should have 3 data members: personID (integer), firstName(string), and lastName(string). The classshould also have a static data member called nextID which is used to assignanID numbertoeach object created(personID). All data members must be private. Create the following member functions: Accessor functions to allow access to first name and last name (updates and retrieval). These functions should...
Class Instructions You will create a password verification program using C-strings and character testing. The user...
Class Instructions You will create a password verification program using C-strings and character testing. The user will enter a password as a C-string. (Character array). You must test that the password contains at least: One lowercase letter One uppercase letter One number (0-9) The password can contain no spaces You are to add one more requirement to the password. I need help in setting this program up. #include <iostream> #include <cctype> using namespace std; int main() { char input; cout...
USING C PROGRAMMING ***CODE MUST BE MODULARIZED** Instructions: Create a program that will collect multiple receipts...
USING C PROGRAMMING ***CODE MUST BE MODULARIZED** Instructions: Create a program that will collect multiple receipts from multiple classrooms to accumulate total cookie sales.   Once all receipts have been processed, the program must show what classroom is won and the total amount of cookies they sold. The classrooms that are participating are from the:                 2nd Floor: 201, 202, 203, 204, 205, 206, 207, 208                 3rd Floor: 301,302, 303, 304, 305, 306, 307, 308, 309                 4th Floor: 401,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT