Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Project 4.b Define a class called Item that consists of a string called name, a

ID: 3663124 • Letter: P

Question

Project 4.b

Define a class called Item that consists of a string called name, a double called price, and an int called quantity. The price represents the price per unit, so if you have an Item with the name "apple", the price "0.80" and the quantity 4, then it means that this Item represents 4 apples, with each apple costing 80 cents. It should have get and set methods for each field (following the normal naming convention - getName, setName, getPrice, setPrice, getQuantity and setQuantity). It should have a constructor that takes three parameters (in the order given above) and passes them to the set methods. It should have a default constructor that sets name to "", price to 0.0 and quantity to 0.

Define a ShoppingCart class which contains as a data member an array of pointer-to-Item (Item*) that can contain up to 100 Item pointers. It should also have an int data member called arrayEnd that keeps track of the index of the next empty spot in the array. You should have a default constructor that initializes each element of the array to NULL and initializes arrayEnd to zero. It should have a function called addItem that takes as a parameter a pointer to an Item and adds it to the array (and updates arrayEnd). It should have a function called totalPrice that returns the total cost of all Items in the ShoppingCart (for which you must take into account the quantity of each Item). Your classes may get used as follows:

Potential parenthesis pitfalls: Remember not to use empty parentheses when declaring an object. If you are invoking a constructor that takes parameters, then you would use parentheses with parameters inside, but if you are invoking the default constructor, you don't use parentheses at all. Also remember that all function calls must have parentheses - if a function doesn't take any parameters, then you must put an empty pair of parentheses.

The files must be called: Item.hpp, Item.cpp, ShoppingCart.hpp and ShoppingCart.cpp.

Item.cpp and ShoppingCart.hpp should both #include Item.hpp. ShoppingCart.cpp should #include ShoppingCart.hpp. The main method you write for testing will also need to include ShoppingCart.hpp. If you named the file with your main method "cartMain.cpp", then you can compile your program with "g++ Item.cpp ShoppingCart.cpp cartMain.cpp -o cart".

Explanation / Answer

/*Create an empty project in visual studio. Name the project as ShoppingCartProject.
Right click on the Source Files folder and
add the following files
Item.h
Item.cpp
ShoppingCart.h
ShoppingCart.cpp
cartMain.cpp
Copy the code to the corresponding files
and run the main program ,press F
*/

//Tester program
//cartMain.cpp
#include<iostream>
//include header files Item.h and ShoppingCart.h
#include "Item.h"
#include "ShoppingCart.h"
using namespace std;

int main()
{

   //Create four Item objects
   Item a("affadavit", 179.99, 12);
   Item b("Bildungsroman", 0.7, 20);
   Item c("capybara", 4.5, 6);
   Item d("dirigible", 0.05, 16);

   //Create an instance of ShoppingCart
   ShoppingCart sc1;
   //Add items to the shopping cart
   sc1.addItem(&a);
   sc1.addItem(&b);
   sc1.addItem(&c);
   sc1.addItem(&d);
   //Call totalPrice on sc1
   double diff = sc1.totalPrice();
   //Print diff
   cout<<"Total price is : "<<diff;

   system("pause");
   return 0;
}

------------------------------------------------------------------------------------------------------------------------------------

//Item.h
#ifndef ITEM_H
#define ITEM_H
#include <string>
using namespace std;
class Item
{
public:
   //default constructor
   Item();
   //parameter constructor
   Item(string nm,double pr,int qty);

   //get and set methods
   void setName(string nm);
   void setPrice(double pr);
   void setQuantity(int qty);

   string getName();
   double getPrice();
   int getQuantity();

private:

   string name;
   double price;
   int quantity;
};

#endif ITEM_H

----------------------------------------------------------------------


//Item.cpp
#include "Item.h"
//Set values for default costructor
Item::Item()
{
   name="";
   price=0;
   quantity=0;
}
//Set values for parameterized construcor
Item::Item(string name,double pr,int qty)
{
   this->name=name;
   price=pr;
   quantity=qty;
}

//Set name
void Item::setName(string name)
{
   this->name=name;
}
//Get name
string Item::getName()
{
   return name;
}
//set price
void Item::setPrice(double price)
{
   this->price=price;
}
//Get price
double Item::getPrice()
{
   return price;
}
//set quantity
void Item::setQuantity(int qty)
{
   quantity=qty;
}
//Get quantity
int Item::getQuantity()
{
   return quantity;
}

----------------------------------------------------------------------

//ShoppingCart.h
#ifndef SHOPPINGCART_H
#define SHOPPINGCART_H
#include "Item.h"
class ShoppingCart
{
public:
   //default constructor
   ShoppingCart();
   //metod to add items
   void addItem(Item *ptrItem);
   double totalPrice();

private:
   Item *items[100];
   //declare 100 items of pointers
   int counter;
};

#endif SHOPPINGCART_H

----------------------------------------------------------------------


//ShoppingCart.cpp
#include "ShoppingCart.h"
ShoppingCart::ShoppingCart()
{
   //set items to null
   for(int i=0;i<100;i++)
      items[i]=NULL;

   counter=0;
}


//Add items to the shopping cart
void ShoppingCart::addItem(Item *tItem)
{
   //adding each items to items
   items[counter++]=tItem;
}

//Returns total price
double ShoppingCart::totalPrice()
{
   double total=0;
   for(int i=0;i<counter;i++)
       //add items and quantity
      total+=(items[i]->getQuantity()*items[i]->getPrice());

   //return total
   return total;
}

----------------------------------------------------------------------

Note: program runs under windows platform.Run in unix/linux or other platforms as you wish.

Sample output:

Total price is : 2201.68