In: Computer Science
IN C++ PLEASE:
(1) Extend the ItemToPurchase class per the following
specifications:
(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.
//class ItemToPurchase definition
class ItemToPurchase {
string itemDescription;
public:
string itemName;
int itemPrice, itemQuantity;
//Parameterized Constructor with default values
ItemToPurchase(string itemName = "none", string itemDescription = "none", int itemPrice = 0, int itemQuantity = 0) {
this.itemName = itemName;
this.itemDescription = itemDescription;
this.itemPrice = itemPrice;
this.itemQuantity = itemQuantity;
}
void SetDescription(string itemDescription) {
this.itemDescription = itemDescription;
}
string GetDescription() {
return this.itemDescription;
}
void PrintItemDescription() {
cout << this.itemName << ": " << this.itemDescription << endl;
}
};
ShoppingCart.h
#ifndef SHOPPINGCART_H
#define SHOPPINGCART_H
class ShoppingCart : public ItemToPurchase{
private :
string customerName,currentDate;
vector<ItemToPurchase> cartItems;
public :
ShoppingCart();
ShoppingCart(string customerName,string date);
string GetCustomerName();
string GetDate();
bool IsItemInCart(string itemName, int &index);
void ModifyItem(ItemToPurchase item);
void AddItem(ItemToPurchase item);
void RemoveItem(string itemName);
int GetNumItemsInCart();
int GetCostOfCart();
void PrintTotal();
void PrintDescriptions();
};
#endif
ShoppingCart.cpp
#include<bits/stdc++.h>
#include"ShoppingCart.h"
using namespace std;
//Default Constructor
ShoppingCart::ShoppingCart(){
customerName="none";
currentDate="January 1, 2016"
}
//parametrised Construtor
ShoppingCart::ShoppingCart(string customerName, string date){
this.customerName = customerName;
this.currentDate = date;
}
string ShoppingCart::GetCustomerName(){
return customerName;
}
string ShoppingCart::GetDate(){
return currentDate;
}
bool ShoppingCart::IsItemInCart(string itemName,int &index){
bool found = false;
int pos = 0;
for(ItemToPurchase item : cartItems){
if(item.itemName == itemName){
found = true;
index = pos;
return found;
}
pos++;
}
return found;
}
void ShoppingCart::ModifyItem(ItemToPurchase item){
int pos = -1;
if( IsItemInCart(item.itemName, pos) ){
// Modifies an item's description, price, and/or quantity. Has parameter ItemToPurchase with updated data members.
cartItems[pos].SetDescription(item.GetDescription());
cartItems[pos].itemPrice=item.itemPrice;
cartItems[pos].itemQuantity=item.itemQuantity;
}else{
//Item cannot be found (by name) in cart, output this message:
cout<<"Item not found in cart. Nothing modified.\n";
}
}
void ShoppingCart :: AddItem(ItemToPurchase item){
// Adds the item to cartItems vector as long as the quantity is not zero.
if(item.itemQuantity!=0){
cartItems.push_back(item);
}
}
void ShoppingCart::RemoveItem(string itemName){
int pos = -1;
if( IsItemInCart(itemName, pos) ){
cartItems.erase( cartItems.begin() + pos );
}else{
cout<<"Item not found in cart. Nothing removed.\n";
}
}
int ShoppingCart::GetNumItemsInCart(){
// Returns quantity of all items in cart. Has no parameters.
int noOfItems = 0;
for(ItemToPurchase item:cartItems){
noOfItems += item.itemQuantity;
}
return noOfItems;
}
int ShoppingCart::GetCostOfCart(){
// Determines and returns the total cost of items in cart.
int totalCost = 0;
for(ItemToPurchase item:cartItems){
totalCost += (item.itemQuantity*item.itemPrice);
}
return totalCost;
}
void ShoppingCart::PrintTotal(){
// Outputs total of objects in cart.
if(cartItems.size() == 0){
cout << "SHOPPING CART IS EMPTY\n";
}else{
cout << cartItems.size()<<"\n";
}
}
void ShoppingCart::PrintDescriptions(){
// Outputs each item's description.
for(ItemToPurchase item:cartItems){
cout << item.PrintItemDescription();
}
}
main.cpp
#include<bits/stdc++.h>
#include"ShoppingCart.h"
using namespace std;
int main(){
// your code
return 0;
}