In: Computer Science
IN C++
7.22 LAB*: Program: Online shopping cart (Part 2)
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.
Code:
#include <iostream>
#include <string>
#include "ItemToPurchase.h"
using namespace std;
//main
int main()
{
//Declare two objects
ItemToPurchase p1,p2;
string myName;
int myPrice,myQuant;
//Get item1 details
cout<<"Item 1"<<endl;
cout<<"Enter the item name:";
getline(cin,myName);
cout<<"Enter the item price:";
cin>>myPrice;
cout<<"Enter the item quantity:";
cin>>myQuant;
p1.SetName(myName);
p1.SetPrice(myPrice);
p1.SetQuantity(myQuant);
cin.ignore();
//Get item2 details
cout<<"\nItem 2"<<endl;
cout<<"Enter the item name:";
getline(cin,myName);
cout<<"Enter the item price:";
cin>>myPrice;
cout<<"Enter the item quantity:";
cin>>myQuant;
p2.SetName(myName);
p2.SetPrice(myPrice);
p2.SetQuantity(myQuant);
//print total cost
cout<<"\nTOTAL COST"<<endl;
cout<<p1.GetName()<<" "<<p1.GetQuantity()<<" @ $"<<p1.GetPrice()<<" = $"<<p1.GetQuantity()*p1.GetPrice()<<endl;
cout<<p2.GetName()<<" "<<p2.GetQuantity()<<" @ $"<<p2.GetPrice()<<" = $"<<p2.GetQuantity()*p2.GetPrice()<<endl;
cout<<"\nTotal: $"<<p1.GetQuantity()*p1.GetPrice()+p2.GetQuantity()*p2.GetPrice()<<endl;
system("pause");
return 0;
}
Note: Could you plz go through this code and let me
know if u need any changes in this.Thank You
=================================
====== ItemToPurchase.h ======
#ifndef ITEMTOPURCHASE
#define ITEMTOPURCHASE
#include <iostream>
#include <string>
using namespace std;
class ItemToPurchase
{
public:
// Zero argumented constructor
ItemToPurchase();
// Parameterized constructor
ItemToPurchase(string, string, int, int);
// getters and setters
void SetName(string);
void SetPrice(int);
void SetQuantity(int);
void SetDescription(string);
void PrintItemCost();
void PrintItemDescription();
string GetName() const;
int GetPrice() const;
int GetQuantity() const;
string GetDescription() const;
private:
string itemName;
int itemPrice;
int itemQuantity;
string itemDescription;
};
#endif
=====================================
=====================================
#include <iostream>
#include <string>
#include "ItemToPurchase.h"
using namespace std;
ItemToPurchase::ItemToPurchase(string name, string
description, int price, int quantity)
{
itemName = name;
itemPrice = price;
itemQuantity = quantity;
itemDescription = description;
}
ItemToPurchase::ItemToPurchase()
{
itemName = "";
itemPrice = 0;
itemQuantity = 0;
itemDescription = "";
}
void ItemToPurchase::SetName(string itemNam)
{
itemName = itemNam;
}
void ItemToPurchase::SetPrice(int itemPric)
{
itemPrice = itemPric;
}
void ItemToPurchase::SetQuantity(int itemQuantit)
{
itemQuantity = itemQuantit;
}
void ItemToPurchase::SetDescription(string itemDescriptio)
{
itemDescription = itemDescriptio;
}
string ItemToPurchase::GetName() const
{
return itemName;
}
int ItemToPurchase::GetPrice() const
{
return itemPrice;
}
int ItemToPurchase::GetQuantity() const
{
return itemQuantity;
}
string ItemToPurchase::GetDescription() const
{
return itemDescription;
}
void ItemToPurchase::PrintItemCost()
{
cout << itemName << " " << itemQuantity <<
" @ $" << itemPrice << " = $";
cout << itemQuantity * itemPrice << endl;
return;
}
void ItemToPurchase::PrintItemDescription()
{
cout << itemName << ": " << itemDescription
<< endl;
return;
}
=====================================
====== ShoppingCart.h ======
#ifndef SHOPPINGCART
#define SHOPPINGCART
#include <string>
#include <vector>
#include "ItemToPurchase.h"
using namespace std;
class ShoppingCart
{
public:
// Parameterized constructor
ShoppingCart(string cName = "none", string cDate = "none");
// getters
string GetCustomerName();
string GetDate();
void AddItem(ItemToPurchase);
void RemoveItem(string itemNames);
void ModifyItem(string name);
int GetNumItemsInCart();
int GetCostOfCart();
void PrintTotal();
void PrintDescriptions();
private:
string customerName;
string currentDate;
vector<ItemToPurchase> cartItems;
};
#endif
=====================================
=== ShoppingCart.cpp ====
#include <iostream>
#include <string>
#include "ShoppingCart.h"
using namespace std;
unsigned int i;
// parametrized constructor
ShoppingCart::ShoppingCart(string cName, string cDate)
{
this->currentDate = "January 1, 2016";
this->currentDate = cDate;
this->customerName = "none";
this->customerName = cName;
}
// function to get name
string ShoppingCart::GetCustomerName()
{
return customerName;
}
// function to get date
string ShoppingCart::GetDate()
{
return currentDate;
}
// This function will add item to the vector
void ShoppingCart::AddItem(ItemToPurchase item)
{
cartItems.push_back(item);
}
// This function will remove item from the vector
void ShoppingCart::RemoveItem(string name)
{
int indx = -1;
for (unsigned int i = 0; i < cartItems.size();
i++)
{
if (cartItems[i].GetName().compare(name) == 0)
{
indx = i;
break;
}
}
if (indx != -1)
{
cartItems.erase(cartItems.begin() + indx);
cout << "** Item Successfully Removed **"
<< endl;
}
else if (indx == -1)
{
cout << "** Item Not Found **" << endl;
}
}
// This function will modify an item in the vector based on the
name
void ShoppingCart::ModifyItem(string name)
{
int qty;
int indx = -1;
for (unsigned int i = 0; i < cartItems.size();
i++)
{
if (cartItems[i].GetName().compare(name) == 0)
{
indx = i;
break;
}
}
if (indx != -1)
{
cout << "Enter New Quantity :";
cin >> qty;
cartItems[indx].SetQuantity(qty);
cout << "** Item Successfully Modified **"
<< endl;
}
else if (indx == -1)
{
cout << "** Item Not Found **" << endl;
}
}
// This function will get total number of items
int ShoppingCart::GetNumItemsInCart()
{
int totItems = 0;
for (i = 0; i < cartItems.size(); i++)
{
totItems = totItems + cartItems.at(i).GetQuantity();
}
return totItems;
}
// function to get cost
int ShoppingCart::GetCostOfCart()
{
int total = 0;
for (i = 0; i < cartItems.size(); i++)
{
total = total + (cartItems.at(i).GetPrice() *
cartItems.at(i).GetQuantity());
}
return total;
}
// function to print total
void ShoppingCart::PrintTotal()
{
int total = 0;
cout << customerName << "'s Shopping Cart - " <<
currentDate << endl;
if (cartItems.size() == 0)
{
cout << "Number of Items: 0" << endl <<
endl;
cout << "SHOPPING CART IS EMPTY" << endl <<
endl;
cout << "Total: $0" << endl << endl;
}
else
{
cout << "Number of Items: " << GetNumItemsInCart()
<< endl << endl;
for (i = 0; i < this->cartItems.size(); i++)
{
this->cartItems.at(i).PrintItemCost();
total
= total + (this->cartItems.at(i).GetPrice() *
this->cartItems.at(i).GetQuantity());
}
cout << endl << "Total: $" << total << endl
<< endl;
}
}
// This function will display the items description
in the vector
void ShoppingCart::PrintDescriptions()
{
cout << customerName << "'s Shopping Cart - " <<
currentDate << endl;
cout << "Item Descriptions" << endl;
for (i = 0; i < cartItems.size(); i++)
{
cartItems.at(i).PrintItemDescription();
}
}
=====================================
======= main.cpp =======
#include <iostream>
#include <string>
#include "ShoppingCart.h"
using namespace std;
// Function declarations
ItemToPurchase AddItem();
char PrintMenu();
int main()
{
// Declaring variables
string itemToRemove;
string customerName;
string currentDate;
//Getting the input entered by the user
cout << "Enter customer's name:" << endl;
getline(cin, customerName);
cout << "Enter today's date:" << endl;
getline(cin, currentDate);
cout << "Customer name: " << customerName <<
endl;
cout << "Today's date: " << currentDate << endl
<< endl;
// Creating an instance of Shopping Cart
ShoppingCart itemCart(customerName, currentDate);
//calling the function
char answer = PrintMenu();
/* This while loop continues to execute
* until the user enters either 'q'
*/
while (answer != 'q')
{
if (answer == 'o' || answer == 'O')
{
cout << "OUTPUT SHOPPING CART" << endl;
itemCart.PrintTotal();
}
if (answer == 'a' || answer == 'A')
{
itemCart.AddItem(AddItem());
}
if (answer == 'i' || answer == 'I')
{
cout << "OUTPUT ITEMS' DESCRIPTIONS" << endl;
itemCart.PrintDescriptions();
}
if (answer == 'd' || answer == 'D')
{
cout << "REMOVE ITEM FROM CART" << endl << "Enter
name of item to remove:" << endl;
cin.ignore();
getline(cin, itemToRemove);
itemCart.RemoveItem(itemToRemove);
}
if (answer == 'c' || answer == 'C')
{
cin.ignore();
string itemName;
cout << "Enter the Item name :";
getline(cin, itemName);
itemCart.ModifyItem(itemName);
}
if (answer == 'q' || answer == 'Q')
{
break;
}
answer = PrintMenu();
}
}
// This function will display the menu
char PrintMenu()
{
char answer;
cout << "MENU" << endl;
cout << "a - Add item to cart" << endl;
cout << "d - Remove item from cart" << endl;
cout << "c - Change item quantity" << endl;
cout << "i - Output items' descriptions" << endl;
cout << "o - Output shopping cart" << endl;
cout << "q - Quit" << endl << endl;
while (true)
{
cout << "Choose an option:" << endl;
cin >> answer;
if (answer == 'a' || answer == 'A')
break;
if (answer == 'd' || answer == 'D')
break;
if (answer == 'c' || answer == 'C')
break;
if (answer == 'i' || answer == 'I')
break;
if (answer == 'o' || answer == 'O')
break;
if (answer == 'q' || answer == 'Q')
break;
}
return answer;
}
ItemToPurchase AddItem()
{
string itemName = "";
string itemDescription;
int itemQuantity;
int itemPrice;
cout << "ADD ITEM TO CART" << endl;
cout << "Enter the item name:" << endl;
cin.ignore();
getline(cin, itemName);
cout << "Enter the item description:" << endl;
getline(cin, itemDescription);
cout << "Enter the item price:" << endl;
cin >> itemPrice;
cout << "Enter the item quantity:" << endl;
cin >> itemQuantity;
ItemToPurchase itemToAdd(itemName, itemDescription, itemPrice,
itemQuantity);
return itemToAdd;
}
=====================================
=====================================
Output:
Output:
=====================.Thank You