7.19 Ch 7 Program: Online shopping cart (continued) (C++) This program extends t
ID: 3840371 • Letter: 7
Question
7.19 Ch 7 Program: Online shopping cart (continued) (C++)
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).
(1) Extend the ItemToPurchase class per the following specifications:
Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)
Public member functions
SetDescription() mutator & GetDescription() accessor (2 pts)
PrintItemCost() - Outputs the item name followed by the quantity, price, and subtotal
PrintItemDescription() - Outputs the item name and description
Private data members
string itemDescription - Initialized in default constructor to "none"
Ex. of PrintItemCost() output:
Ex. of PrintItemDescription() output:
(2) Create three new files:
ShoppingCart.h - Class declaration
ShoppingCart.cpp - Class definition
main.cpp - main() function (Note: main()'s functionality differs from the warm up)
Build the ShoppingCart class with the following specifications. Note: Some can be function stubs (empty functions) initially, to be completed in later steps.
Default constructor
Parameterized constructor which takes the customer name and date as parameters (1 pt)
Private data members
string customerName - Initialized in default constructor to "none"
string currentDate - Initialized in default constructor to "January 1, 2016"
vector < ItemToPurchase > cartItems
Public member functions
GetCustomerName() accessor (1 pt)
GetDate() accessor (1 pt)
AddItem()
Adds an item to cartItems vector. Has parameter ItemToPurchase. Does not return anything.
RemoveItem()
Removes item from cartItems vector. Has a string (an item's name) parameter. Does not return anything.
If item name cannot be found, output this message: Item not found in cart. Nothing removed.
ModifyItem()
Modifies an item's description, price, and/or quantity. Has parameter ItemToPurchase. Does not return anything.
If item can be found (by name) in cart, check if parameter has default values for description, price, and quantity. If not, modify item in cart.
If item cannot be found (by name) in cart, output this message: Item not found in cart. Nothing modified.
GetNumItemsInCart() (2 pts)
Returns quantity of all items in cart. Has no parameters.
GetCostOfCart() (2 pts)
Determines and returns the total cost of items in cart. Has no parameters.
PrintTotal()
Outputs total of objects in cart.
If cart is empty, output this message: SHOPPING CART IS EMPTY
PrintDescriptions()
Outputs each item's description.
Ex. of PrintTotal() output:
Ex. of PrintDescriptions() output:
(3) In main(), prompt the user for a customer's name and today's date. Output the name and date. Create an object of type ShoppingCart. (1 pt)
Ex.
(4) Implement the PrintMenu() function. PrintMenu() has a ShoppingCart parameter, and outputs a menu of options to manipulate the shopping cart. Each option is represented by a single character. Build and output the menu within the function.
If the an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call PrintMenu() in the main() function. Continue to execute the menu until the user enters q to Quit. (3 pts)
Ex:
(5) Implement Output shopping cart menu option. (3 pts)
Ex:
(6) Implement Output item's description menu option. (2 pts)
Ex.
(7) Implement Add item to cart menu option. (3 pts)
Ex:
(8) Implement remove item menu option. (4 pts)
Ex:
(9) Implement Change item quantity menu option. Hint: Make new ItemToPurchase object and use ItemToPurchase modifiers before using ModifyItem() function. (5 pts)
Ex:
L
Lab Submission
7.19.1: Ch 7 Program: Online shopping cart (continued) (C++)
Instructions
Deliverables
ShoppingCart.cpp
ShoppingCart.h
ItemToPurchase.h
main.cpp
ItemToPurchase.cpp
We will expect the above file(s) to be submitted
Compile command
g++ ShoppingCart.cpp main.cpp ItemToPurchase.cpp -Wall -o a.out
We will use this command to compile your code
This Is the earlier programs code
-----My main
main.cpp
------My ItemToPurchase.cpp
------My ItemToPurchase.h
----Note All of the code was made with Clion. Unsure if that matters or not but wanted to throw it in just in case. Thanks ahead of time.
Instructions
Deliverables
ShoppingCart.cpp
,ShoppingCart.h
,ItemToPurchase.h
,main.cpp
andItemToPurchase.cpp
We will expect the above file(s) to be submitted
Compile command
g++ ShoppingCart.cpp main.cpp ItemToPurchase.cpp -Wall -o a.out
We will use this command to compile your code
Explanation / Answer
//ShoppingCart.h
#include <vector>
#include "ItemToPurchase.h"
class ShoppingCart
{
string customerName;
string currentDate;
vector < ItemToPurchase > cartItems;
public:
ShoppingCart(void);
~ShoppingCart(void);
ShoppingCart(string custName, string date);
string GetCustomerName() ;
string GetDate();
void AddItem(ItemToPurchase item); //Adds an item to cartItems vector.
void RemoveItem(string itemName); //Removes item from cartItems vector.
void ModifyItem(ItemToPurchase item);
int GetNumItemsInCart(); //Returns quantity of all items in cart.
int GetCostOfCart(); //Determines and returns the total cost of items in cart.
void PrintTotal(); //Outputs total of objects in cart.
void PrintDescriptions(); //Outputs each item's description.
};
//ShoppingCart.cpp
#include "StdAfx.h"
#include "ShoppingCart.h"
ShoppingCart::ShoppingCart(void)
{
customerName = "None"; //default name
currentDate = "January 1, 2016"; //default date
}
ShoppingCart::ShoppingCart(string custName, string date)
{
customerName = custName;
currentDate = date;
}
ShoppingCart::~ShoppingCart(void)
{
}
string ShoppingCart::GetCustomerName()
{
return customerName;
}
string ShoppingCart::GetDate()
{
return currentDate;
}
void ShoppingCart::AddItem(ItemToPurchase item)
{
cartItems.push_back(item);
}
//Adds an item to cartItems vector.
void ShoppingCart::RemoveItem(string itemName)
{
vector<ItemToPurchase>::iterator itr = cartItems.begin();
bool removed = false;
while( itr != cartItems.end())
{
if ( itr->GetName() == itemName )
{
cartItems.erase(itr);
removed= true;
break;
}
itr++;
}
if (!removed) {
cout<<" Item not found in cart. Nothing removed.";
}
}
//Removes item from cartItems vector.
bool HasItemDefaultValues(ItemToPurchase item)
{
return
(item.GetDescription() == "None")
&&
(item.GetQuantity() == 0)
&&
(item.GetPrice() == 0);
}
void ShoppingCart::ModifyItem(ItemToPurchase item){
vector<ItemToPurchase>::iterator itr = cartItems.begin();
bool found = false;
while( itr != cartItems.end())
{
if ( itr->GetName() == item.GetName() && !HasItemDefaultValues(item) )
{
itr->SetDescription(item.GetDescription());
itr->SetPrice(item.GetPrice());
itr->SetQuantity(item.GetQuantity());
found= true;
break;
}
itr++;
}
if (!found) {
cout<<" Item not found in cart. Nothing modified.";
}
}
//Returns quantity of all items in cart.
int ShoppingCart::GetNumItemsInCart(){
int count = 0;
vector<ItemToPurchase>::iterator itr = cartItems.begin();
while( itr != cartItems.end())
{
count += itr->GetQuantity();
itr++;
}
return count;
}
//Determines and returns the total cost of items in cart. Has no parameters.
int ShoppingCart::GetCostOfCart()
{
int cost = 0;
vector<ItemToPurchase>::iterator itr = cartItems.begin();
while( itr != cartItems.end())
{
cost += itr->GetQuantity() * itr->GetPrice();
itr++;
}
return cost;
}
//Outputs total of objects in cart.If cart is empty, output this message: SHOPPING CART IS EMPTY
void ShoppingCart::PrintTotal(){
cout<< customerName+"'s Shopping Cart - "<< currentDate+" ";
cout<<"Number of Items: "<<GetNumItemsInCart()<<" ";
vector<ItemToPurchase>::iterator itr = cartItems.begin();
while( itr != cartItems.end())
{
cout<<itr->GetName() <<" "<< itr->GetQuantity()<<" @ $"<<itr->GetPrice()<<" = $"<< itr->GetQuantity()*itr->GetPrice()<<endl;
itr++;
}
cout<<" Total : $"<<GetCostOfCart();
}
void ShoppingCart::PrintDescriptions()
{
cout<< customerName+"'s Shopping Cart - "<< currentDate+" ";
cout<<"Items Descriptions ";
vector<ItemToPurchase>::iterator itr = cartItems.begin();
while( itr != cartItems.end())
{
cout<<itr->GetName() <<": "<< itr->GetDescription()<<endl;
itr++;
}
}
//itemsToPurchase.cpp
#include "StdAfx.h"
#include "ItemToPurchase.h"
ItemToPurchase::ItemToPurchase() //default constructor
{
itemName = "none"; //default name
itemDescription = "none"; //default Description
itemPrice = 0; //default price
itemQuantity = 0; //default qty
}
//Parameterised constructor
ItemToPurchase::ItemToPurchase(string itemName, string itemDescription, int itemPrice, int itemQuantity)
{
this->itemName = itemName;
this->itemDescription = itemDescription;
this->itemPrice = itemPrice;
this->itemQuantity = itemQuantity;
}
//Assigning name
void ItemToPurchase::SetName(string resitemName)
{
itemName = resitemName;
}
//Returning name
string ItemToPurchase::GetName()
{
return itemName;
}
//Assigning price
void ItemToPurchase::SetPrice(int resitemPrice)
{
itemPrice = resitemPrice;
}
//Returning price
int ItemToPurchase::GetPrice()
{
return itemPrice;
}
//Assigning Quantity
void ItemToPurchase::SetQuantity(int resitemQuantity)
{
itemQuantity = resitemQuantity;
}
//Returning quantity
int ItemToPurchase::GetQuantity()
{
return itemQuantity;
}
void ItemToPurchase::SetDescription(string itemDescription)
{
this->itemDescription = itemDescription;
}
string ItemToPurchase::GetDescription() {
return itemDescription;
}
// Outputs the item name followed by the quantity, price, and subtotal
void ItemToPurchase::PrintItemCost()
{
cout<<itemName<<" "<<itemQuantity<<" @ "<<itemPrice<<" = "<<(itemPrice*itemQuantity)<<endl;
}
// Outputs the item name and description
void ItemToPurchase::PrintItemDescription()
{
cout<<itemName<<": "<<itemDescription<<","<<itemQuantity<<" Oz."<<endl;
}
//ItemsToPurchase.h
#pragma once
#ifndef SHOPPINGCART_ITEMTOPURCHASE_H
#define SHOPPINGCART_ITEMTOPURCHASE_H
#include <string>
using namespace std;
//Class definition
class ItemToPurchase
{
public:
ItemToPurchase();
ItemToPurchase(string itemName, string itemDescription, int itemPrice, int itemQuantity=0);
void SetName(string resitemName);
string GetName();
void SetPrice(int resitemPrice);
int GetPrice();
void SetQuantity(int resitemQuantity);
int GetQuantity();
void SetDescription(string itemDescription) ;
string GetDescription() ;
void PrintItemCost();
void PrintItemDescription();
private:
string itemName;
int itemPrice;
int itemQuantity;
string itemDescription;
};
#endif //SHOPPINGCART_ITEMTOPURCHASE_H
//main.cpp
#include "stdafx.h"
#include "ShoppingCart.h"
void PrintMenu(ShoppingCart cart)
{
char op;
do
{
ItemToPurchase item;
cout<<" MENU"<<endl;
cout<<"a - Add item to cart ";
cout<<"d - Remove item from cart ";
cout<<"c - Change item quantity ";
cout<<"i - Output items' descriptions ";
cout<<"o - Output shopping cart ";
cout<<"q - Quit ";
cout<<"Choose an option:";
op = getchar();
switch (op)
{
case 'a':
// Get items details here using item obj
//finall add item to cart.
cart.AddItem(item);
break;
case 'd':
// Get items details here using item obj
//finall add item to cart.
cart.RemoveItem(item.GetName());
break;
case 'c':
// Get items details here using item obj
//finall add item to cart.
cart.ModifyItem(item);
break;
case 'i':
// Get items details here using item obj
//finall add item to cart.
cart.PrintDescriptions();
break;
case 'o':
// Get items details here using item obj
//finall add item to cart.
cart.PrintTotal();
case 'q':
break;
default :
cout<<"Wrong Choice";
}
}
while(op != 'q');
}
void main()
{
string name, date;
cout<<"Enter customer's name: ";
getline(cin, name);
cout<<"Enter today's date: ";
getline(cin, date);
ShoppingCart cart(name, date);
cout<<" Customer name: "<<name;
cout<<" Today's date: "<<date;
PrintMenu(cart);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.