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

11.17 Assignment Part 2: Online shopping cart (continued) This program extends t

ID: 3716961 • Letter: 1

Question

11.17 Assignment Part 2: Online shopping cart (continued)

This program extends the earlier "Online shopping cart" program. (Consider first backing up your earlier program).

Create three new files:

ShoppingCart.h - Class declaration

ShoppingCart.cpp - Class definition

main.cpp - Note: main's functionality differs from the previous exercise

Build the ShoppingCart class with the following specifications:

ShoppingCart

addItem

use push_back to add the given ItemToPurchase argument to the end of cartItems

removeItemloop through the cartItems vector and look for cartItems[i].getName() to match the given string argument

once found, call cartItems.erase(cartItems.begin() + i)

If the item is not found, output "Item not found in cart. Nothing removed."

changeQuantity

Loop through looking for a matching string, then call cartItems[i].setQuantity(n)

If the item is not found, output "Item not found in cart. Nothing modified."

totalCost

this one is a bit more complicated than you would first imagine. Create a running sum, initialized to zero, loop through cartItems, and add cartItems[i].getQuantity() * cartItems[i].getPrice() to your total as you go.

Note: For unit testing purposes, do not implement any input or output in any functions except main and printCart

printItems

Output customerName's Shopping Cart

Loop through cartItems and call printItemCost on each one

State the total at the end

Ex. of printCart output:

main

prompt the user for a customer's name. Create an object of type ShoppingCart with this name.

be careful with your use of getline and cin, make sure to put cin.ignore() whenever a getline is followed by a cin.

Call printItems():

The rest of your code will be tested through unit testing.

Explanation / Answer

First we wil nedd to create the header file with the provided structure in the question. I am assuming you already have ItemstoPurchase class, which is used in vector.

Here is the header file:

#ifndef ShoppingCart_h

#define ShoppingCart_h

using namespace std;

class ShoppingCart {

private:

string customerName;

vector<ItemToPurchase> cartItems;

public:

ShoppingCart(); //defualt constructor

ShoppingCart(string name);

string getCustomerName() const;

void addItem(ItemToPurchase item);

void removeItem(string itemname);

void changeQuantity(string itemname, int quantity);

double getTotalCost();

void printCart();

};

#endif

After defining the structure of our class, we need to provide implementations for the functions as per the question requirements.

//ShoppingCart.cc

//Defination for ShoppingCart.h

#include "ShoppingCart.h"

ShoppingCart::ShoppingCart() {

//default constructor

}

ShoppingCart::ShoppingCart(string n) {

customerName = n;

}

string ShoppingCart:: getCustomerName() const {

reutrn customerName;

}

void ShoppingCart::addItem(ItemtoPurchase item) {

//push the item to vector cartItems

cartItems.push_back(item);

}

void ShoppingCart::removeItem(string itemname) {

//look throught the cartItems vector if itemname is found delete item

for(int i = 0; i < cartItems.size(); i++) {

if(cartItems[i].getName() == itemname)

cartItems.erase(cartItems.begin() + i);

}

}

void ShoppingCart::changeQuantity(string itemname, int n) {

bool found = false;

for(int i = 0; i < cartItems.size(); i++) {

if(cartItems[i].getName() == itemname) {

cartItems.setQuantity(n);

found = true;

}

}

//if not found print message

if(!found) {

cout<<"Item not found in cart. Nothing modified."<<endl;

}

}

double ShoppingCart::getTotalCost() {

double sum = 0;

for(int i = 0; i < cartItems.size(); i++) {

sum += cartItems[i].getQuantity() * cartItems[i].getPrice();

}

return sum;

}

void ShoppingCart::printCart() {

for(int i = 0; i < cartItems.size(); i++) {

cartItems[i].printItemCost();

}

//state the total at the end

getTotalCost();

}

//end of ShoppingCart.cc