Question
C++ (Inheritance and Recursion) Online Shopping Cart System Revisited I need assistance to introduce Inheritance and Recursion to my program. In addition, I need to
C++ (Inheritance and Recursion) Online Shopping Cart System Revisited
I need assistance to introduce Inheritance and Recursion to my program.
In addition, I need to explain the features of the new functionality.
My current program...
****** main.cpp ******
#include
void AddItem(ShoppingCart& cart){ ItemToPurchase addItem; string addItemName; string addItemDescription; int addItemPrice; int addItemQuantity; cout << "ADD ITEM TO CART" << endl; cout << "Enter the item name:" << endl; cin.ignore(); getline(cin, addItemName); addItem.SetName(addItemName); cout << "Enter the item description:" << endl; getline(cin, addItemDescription); addItem.SetDescription(addItemDescription); cout << "Enter the item price:" << endl; cin >> addItemPrice; addItem.SetPrice(addItemPrice); cout << "Enter the item quantity:" << endl; cin >> addItemQuantity; addItem.SetQuantity(addItemQuantity); cart.AddItem(addItem); }
void RemoveItem(ShoppingCart& cart){ string removeItem; cout << "REMOVE ITEM FROM CART" << endl; cout << "Enter name of item to remove:"; cout << endl; cin.ignore(); getline(cin, removeItem); cart.RemoveItem(removeItem); }
void ChangeItemQuantity(ShoppingCart& cart){ string changeQuantityItem; int changeQuantityNumber; ItemToPurchase updateItem; cout << "CHANGE ITEM QUANTITY" << endl; cout << "Enter the item name:" << endl; cin.ignore(); getline(cin, changeQuantityItem); updateItem.SetName(changeQuantityItem); cout << "Enter the new quantity:" << endl; cin >> changeQuantityNumber; updateItem.SetQuantity(changeQuantityNumber); cart.ModifyItem(updateItem); }
void OutputItemsDescriptions(ShoppingCart cart, string customerName, string todaysDate){ cout << "OUTPUT ITEMS' DESCRIPTIONS" << endl; cout << customerName << "'s Shopping Cart - " << todaysDate << endl; cout << endl; cart.PrintDescriptions(); cout << endl; }
void OutputShoppingCart(ShoppingCart cart, string customerName, string todaysDate){ cout << "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:" << endl; getline(cin, customerName); cout << "Enter today's date:" << endl; getline(cin, todaysDate); cout << endl; cout << "Customer name: " << customerName << endl; cout << "Today's date: " << todaysDate << endl; 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; cout << endl; do { cout << "Choose an option:" << endl; cin >> userOption; if (cin.fail()) { cin.clear(); cin.ignore(' ', 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; }
***** ItemToPurchase.cpp *****
#include
ItemToPurchase::ItemToPurchase() { itemName = ""; itemDescription = "none"; itemPrice = 0; itemQuantity = 0; }
ItemToPurchase::ItemToPurchase(string name, string description, int price, int quantity) { itemName = name; itemPrice = price; itemQuantity = quantity; itemDescription = description; }
string ItemToPurchase::GetName() { return itemName; }
int ItemToPurchase::GetPrice() { return itemPrice; }
int ItemToPurchase::GetQuantity() { return itemQuantity; }
void ItemToPurchase::SetName(string name) { itemName = name; }
void ItemToPurchase::SetPrice(int price) { itemPrice = price; } void ItemToPurchase::SetQuantity(int quantity) { itemQuantity = quantity; } void ItemToPurchase::SetDescription(string item) { itemDescription = item; } string ItemToPurchase::GetDescription() { return itemDescription; } void ItemToPurchase::PrintItemDescription() { cout << GetName() << ":" << GetDescription(); } void ItemToPurchase::PrintItemCost() { cout << GetName() << GetPrice() << "@" << GetQuantity() << GetPrice() * GetQuantity(); }
***** ItemToPurchase.h *****
#pragma once #include
class ItemToPurchase { public: ItemToPurchase(); ItemToPurchase(string, string, int, int); void SetName(string name); string GetName(); void SetPrice(int price); int GetPrice(); void SetQuantity(int quantity); int GetQuantity(); void SetDescription(string); string GetDescription(); void PrintItemDescription(); void PrintItemCost();
private: string itemName; int itemPrice; int itemQuantity; string itemDescription; };
***** ShoppingCart.cpp *****
#include
ShoppingCart::ShoppingCart(){ customerName = "none"; currentDate = "January 1, 2021"; }
ShoppingCart::ShoppingCart(string name, string date){ customerName = name; currentDate = date; }
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){ for (int i = 0; i < cartItems.size(); ++i){ if (this->cartItems.at(i).GetName() == itemName) { this->cartItems.erase(cartItems.begin() + i); return; } } cout << "Item not found in cart. Nothing removed."; cout << endl; }
void ShoppingCart::ModifyItem(ItemToPurchase item){ for (int i = 0; i < cartItems.size(); ++i){ if (cartItems.at(i).GetName() == (item.GetName())) { cartItems.at(i).SetQuantity(item.GetQuantity()); return; } } cout << "Item not found in cart. Nothing modified."; cout << endl; }
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; if (this->cartItems.size() == 0){ return 0; } for (int i = 0; i < cartItems.size(); ++i){ totalCost += (cartItems.at(i).GetPrice() * cartItems.at(i).GetQuantity()); } // If total value is less than $100, then add a shipping fee of $15 if (totalCost < 100){ totalCost += 15; cout << " --- *Shipping fee of $15 added.*" << endl; } if (totalCost >= 500){ // give 10% discount. totalCost = totalCost - totalCost * 0.10; cout << " --- *10% discount applies to Total Cost.*" << endl; } else if (totalCost >= 300){ // give 5% discount. totalCost = totalCost - totalCost * 0.05; cout << " --- *5% discount applied to Total Cost.*" << endl; } return totalCost; }
void ShoppingCart::PrintTotal(){ cout << "Number of Items: " << GetNumItemsInCart() << endl; for (int i = 0; i < cartItems.size(); ++i){ cout << endl; cout << 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 << "SHOPPING CART IS EMPTY"; } cout << endl; cout << endl; cout << "Total: $" << GetCostOfCart() << endl; }
void ShoppingCart::PrintDescriptions(){ cout << "Item Descriptions"; for (int i = 0; i < cartItems.size(); ++i){ cout << endl; cout << cartItems.at(i).GetName() << ": " << cartItems.at(i).GetDescription(); } }
***** ShoppingCart.h *****
#pragma once #include
class ShoppingCart { private: string customerName; string currentDate; vector
public: ShoppingCart(); ShoppingCart(string name, string date); string GetCustomerName(); string GetDate(); void AddItem(ItemToPurchase item); void RemoveItem(string itemName); void ModifyItem(ItemToPurchase item); int GetNumItemsInCart(); int GetCostOfCart(); void PrintTotal(); void PrintDescriptions(); };
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started