In: Computer Science
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:
Ex. of PrintItemCost() output:
Bottled Water 10 @ $1 = $10
Ex. of PrintItemDescription() output:
Bottled Water: Deer Park, 12 oz.
(2) Create three new files:
Build the ShoppingCart class with the following specifications. Note: Some can be function stubs (empty functions) initially, to be completed in later steps.
Ex. of PrintTotal() output:
John Doe's Shopping Cart - February 1, 2016
Number of Items: 8
Nike Romaleos 2 @ $189 = $378
Total: $
Ex. of PrintDescriptions() output:
John Doe's Shopping Cart - February 1, 2016
Item Descriptions
Nike Romaleos: Volt color, Weightlifting shoes
Chocolate Chips: Semi-sweet
Powerbeats 2 Headphones: Bluetooth headphones
(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)
(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:
MENU
a - Add item to cart
d - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit
Choose an option:
(5) Implement Output shopping cart menu option. (3 pts)
Ex:
OUTPUT SHOPPING CART
John Doe's Shopping Cart - February 1, 2016
Number of Items: 8
Nike Romaleos 2 @ $189 = $378
Total:
(6) Implement Output item's description menu option. (2 pts)
Ex.
OUTPUT ITEMS' DESCRIPTIONS
John Doe's Shopping Cart - February 1, 2016
Item Descriptions
Nike Romaleos: Volt color, Weightlifting shoes
Chocolate Chips: Semi-sweet
Powerbeats 2 Headphones: Bluetooth headphones
(7) Implement Add item to cart menu option. (3 pts)
Ex:
ADD ITEM TO CART
Enter the item name:
Nike Romaleos
Enter the item description:
Volt color, Weightlifting shoes
Enter the item price:
189
Enter the item quantity:
2
(8) Implement remove item menu option. (4 pts)
Ex:
REMOVE ITEM FROM CART
Enter name of item to remove:
Chocolate Chips
(9) Implement Change item quantity menu option. Hint: Make new
ItemToPurchase object and use ItemToPurchase modifiers before using
ModifyItem() function. (5 pts)
Ex:
CHANGE ITEM QUANTITY
Enter the item name:
Nike Romaleos
Enter the new quantity:
3
main.cpp
----------------------------------------------------------------------------
#include <iostream>
#include <string>
#include "ItemToPurchase.h"
#include "ShoppingCart.h"
using namespace std;
void AddItem(ShoppingCart &cart) {
ItemToPurchase addItem;
string addItemName;
string addItemDescription;
int addItemPrice;
int addItemQuantity;
cout << endl << "ADD ITEM TO CART";
cout << endl << "Enter the item name: ";
cin.ignore();
getline(cin, addItemName);
addItem.SetName(addItemName);
cout << endl << "Enter the item description: ";
getline(cin, addItemDescription);
addItem.SetDescription(addItemDescription);
cout << endl << "Enter the item price: ";
cin >> addItemPrice;
addItem.SetPrice(addItemPrice);
cout << endl << "Enter the item quantity: ";
cin >> addItemQuantity;
addItem.SetQuantity(addItemQuantity);
cart.AddItem(addItem);
}
void RemoveItem(ShoppingCart &cart) {
string removeItem;
cout << endl << "REMOVE ITEM FROM CART";
cout << endl << "Enter name of item to remove: ";
cin.ignore();
getline(cin, removeItem);
cart.RemoveItem(removeItem);
}
void ChangeItemQuantity(ShoppingCart &cart) {
string changeQuantityItem;
int changeQuantityNumber;
cout << endl << "CHANGE ITEM QUANTITY";
cout << endl << "Enter the item name: ";
cin.ignore();
getline(cin, changeQuantityItem);
cout << endl << "Enter the new quantity: ";
cin >> changeQuantityNumber;
cart.UpdateQuantity(changeQuantityItem,
changeQuantityNumber);
}
void OutputItemsDescriptions(ShoppingCart cart, string
customerName, string todaysDate) {
cout << "OUTPUT ITEMS' DESCRIPTIONS";
cout << endl << customerName << "'s Shopping Cart
- " << todaysDate;
cout << endl;
cart.PrintDescriptions();
}
void OutputShoppingCart(ShoppingCart cart, string customerName,
string todaysDate) {
cout << endl << "OUTPUT SHOPPING CART";
cout << endl << customerName << "'s Shopping Cart
- " << todaysDate;
cout << endl;
cart.PrintTotal();
}
void PrintMenu(ShoppingCart cart) {
string customerName;
string todaysDate;
char userOption = '1';
cout << "Enter Customer's Name: ";
getline(cin, customerName);
cout << endl << "Enter Today's Date: ";
getline(cin, todaysDate);
cout << endl << "Customer Name: " <<
customerName;
cout << endl << "Today's Date: " <<
todaysDate;
do {
cout << endl << "MENU" << endl << "a - Add
item to cart" << endl << "d - Remove item from cart"
<< endl
<< "c - Change item quantity" << endl << "i -
Output items\' descriptions" << endl << "o - Output
shopping cart"
<< endl << "q - Quit" << endl;
do {
cout << endl << "Choose an option: ";
cin >> userOption;
if (cin.fail()) {
cin.clear();
cin.ignore('\n', 1000);
return;
}
} while (userOption != 'a' && userOption != 'd' &&
userOption != 'c' && userOption != 'i' &&
userOption != 'o' && userOption != 'q');
if (userOption == 'a') {
AddItem(cart);
}
else if (userOption == 'd') {
RemoveItem(cart);
}
else if (userOption == 'c') {
ChangeItemQuantity(cart);
}
else if (userOption == 'i') {
OutputItemsDescriptions(cart, customerName, todaysDate);
}
else if (userOption == 'o') {
OutputShoppingCart(cart, customerName, todaysDate);
}
} while (userOption != 'q');
}
int main() {
ShoppingCart cart;
PrintMenu(cart);
return 0;
}
---------------------------------------------------------------------------------------------------------
ShoppingCart.cpp
--------------------------------------------------------
#include "ShoppingCart.h"
ShoppingCart::ShoppingCart()
{
customerName = "none";
currentDate = "January 1, 2016";
}
ShoppingCart::ShoppingCart(string name, string date)
{
customerName = name;
currentDate = date;
}
ShoppingCart::~ShoppingCart()
{
}
string ShoppingCart::GetCustomerName()
{
return customerName;
}
string ShoppingCart::GetDate()
{
return currentDate;
}
void ShoppingCart::AddItem(ItemToPurchase item)
{
this->cartItems.push_back(item);
}
void ShoppingCart::RemoveItem(string itemName)
{
//this->cartItems.erase(find(cartItems.begin(), cartItems.end(),
itemName));
for (int i = 0; i < cartItems.size(); i++) {
if (this->cartItems.at(i).GetName() == itemName) {
this->cartItems.erase(cartItems.begin() + i);
return;
}
}
cout << endl << "Item not found in cart. Nothing
removed.";
}
void ShoppingCart::UpdateQuantity(string itemName, int
newQuantity)
{
for (int i = 0; i < cartItems.size(); i++) {
if (this->cartItems.at(i).GetName() == itemName) {
cartItems.at(i).SetQuantity(newQuantity);
return;
}
}
cout << endl << "Item not found in cart. Nothing
modified.";
}
int ShoppingCart::GetNumItemsInCart()
{
int numItems = 0;
for (int i = 0; i < cartItems.size(); i++) {
numItems += cartItems.at(i).GetQuantity();
}
return numItems;
}
int ShoppingCart::GetCostOfCart()
{
int totalCost = 0;
for (int i = 0; i < cartItems.size(); i++) {
totalCost += (cartItems.at(i).GetPrice() *
cartItems.at(i).GetQuantity());
}
return totalCost;
}
void ShoppingCart::PrintTotal()
{
cout << "Number of Items: " <<
GetNumItemsInCart();
for (int i = 0; i < cartItems.size(); i++) {
cout << endl << cartItems.at(i).GetName() << " "
<< cartItems.at(i).GetQuantity() << " @ $" <<
cartItems.at(i).GetPrice()
<< " = $" << (cartItems.at(i).GetQuantity() *
cartItems.at(i).GetPrice());
}
if (cartItems.size() == 0) {
cout << endl << endl << "SHOPPING CART IS
EMPTY";
}
cout << endl << endl << "Total: $" <<
GetCostOfCart();
}
void ShoppingCart::PrintDescriptions()
{
cout << endl << "Item Descriptions";
for (int i = 0; i < cartItems.size(); i++) {
cout << endl << cartItems.at(i).GetName() << ": "
<< cartItems.at(i).GetDescription();
}
}
----------------------------------------------------------------------------------------------------------------------------------------
ShoppingCart.h
--------------------------------------------------------------------
#pragma once
#include <string>
#include "ItemToPurchase.h"
#include <vector>
#include <iostream>
using namespace std;
class ShoppingCart
{
private:
string customerName;
string currentDate;
vector <ItemToPurchase> cartItems;
public:
ShoppingCart();
ShoppingCart(string name, string date);
~ShoppingCart();
string GetCustomerName();
string GetDate();
void AddItem(ItemToPurchase item);
void RemoveItem(string itemName);
void UpdateQuantity(string itemname, int newQuantity);
int GetNumItemsInCart();
int GetCostOfCart();
void PrintTotal();
void PrintDescriptions();
};
-----------------------------------------------------------------------------------------------------------------------
ItemToPurchase.cpp
-------------------------------------------------------------------------
#include "ItemToPurchase.h"
#include <iostream>
ItemToPurchase::ItemToPurchase()
{
itemName = "none";
itemPrice = 0;
itemQuantity = 0;
itemDescription = "none";
}
ItemToPurchase::ItemToPurchase(string name, string description,
int price, int quantity)
{
itemName = name;
itemDescription = description;
itemPrice = price;
itemQuantity = quantity;
}
ItemToPurchase::~ItemToPurchase()
{
}
void ItemToPurchase::SetName(string someString)
{
this->itemName = someString;
}
string ItemToPurchase::GetName()
{
return itemName;
}
void ItemToPurchase::SetPrice(int somePrice)
{
this->itemPrice = somePrice;
}
int ItemToPurchase::GetPrice()
{
return itemPrice;
}
void ItemToPurchase::SetQuantity(int someQuantity)
{
this->itemQuantity = someQuantity;
}
int ItemToPurchase::GetQuantity()
{
return itemQuantity;
}
void ItemToPurchase::SetDescription(string description)
{
this->itemDescription = description;
}
string ItemToPurchase::GetDescription()
{
return itemDescription;
}
void ItemToPurchase::PrintItemCost()
{
cout << GetName() << " " << GetQuantity()
<< " @ $" << GetPrice() << " = $" <<
GetQuantity() * GetPrice();
}
void ItemToPurchase::PrintItemDescription()
{
cout << GetName() << ": " <<
GetDescription();
}
---------------------------------------------------------------------------------------------------
ItemToPurchase.h
-------------------------------------------------------------
#pragma once
#include <string>
using namespace std;
class ItemToPurchase
{
private:
string itemName;
int itemPrice;
int itemQuantity;
string itemDescription;
public:
ItemToPurchase(string name, string description, int price, int
quantity);
ItemToPurchase();
~ItemToPurchase();
void SetName(string someString);
string GetName();
void SetPrice(int somePrice);
int GetPrice();
void SetQuantity(int someQuantity);
int GetQuantity();
void SetDescription(string description);
string GetDescription();
void PrintItemCost();
void PrintItemDescription();
};
Output Screenshot:-
Please give me a UPVOTE. Thank you ?