Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ (Inheritance and Recursion) Online Shopping Cart System Revisited I need assistance to introduce Inheritance and Recursion to my program. Recursion: I was looking at

C++ (Inheritance and Recursion) Online Shopping Cart System Revisited

I need assistance to introduce Inheritance and Recursion to my program.

Recursion:

I was looking at something like this...

function: budgetPlanner(myShoppingCart, myBudget)

returns: myReducedCart

1. If *least* expensive item in myShoppingCart is over budget

return -1 or error or something.

2. Check if myShoppingCart is under myBudget

a. If yes, we're done here! (return myReducedCart = myShoppingCart)

b. If not, find most expensive item in myShoppingCart, and eliminate it

3. myShoppingCart = myShoppingCart minus most expensive element

4. Call budgetPlanner(myShoppingCart, myBudget)

Inheritance:

I want to add a Receipt class that inherits my ShoppingCart using the following, but I get error messages for the Receipt.cpp file (I also want the program to call the ReceiptPrint function when the user selects "q" to quit...

***** Receipt.h *****

#pragma once #include #include "ShoppingCart.h" using namespace std;

class Receipt : public ShoppingCart { public: void ReceiptPrint(); }

***** Receipt.cpp ****

#include #include "Receipt.h" using namespace std;

Receipt :: ReceiptPrint() { cout << customerName << "'s Receipt" << endl;

// Number of Items

// Item List and Individual Price and Quantities

// Grand Total

My current program...

****** main.cpp ******

#include #include #include "ItemToPurchase.h" #include "ShoppingCart.h" using namespace std;

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 #include "ItemToPurchase.h" using namespace std;

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 #include using namespace std;

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 #include "ShoppingCart.h" using namespace std;

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 #include #include #include "ItemToPurchase.h" using namespace std;

class ShoppingCart { private: string customerName; string currentDate; vector cartItems;

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

How did you feel about taking piano lessons as a child? (general)

Answered: 1 week ago