Question: Assignment Part 2: Online shopping cart (continued) This program extends the earlier Online shopping cart program. (Consider first backing up your earlier program). Create three
Assignment Part 2:
Online shopping cart (continued)
This program extends the earlier "Online shopping cart" program. (Consider first backing up your earlier program).
Create three new files:
ShoppingCart.h - Class declaration
ShoppingCart.cpp - Class definition
main.cpp - Note: main's functionality differs from the previous exercise
Build the ShoppingCart class with the following specifications:
ShoppingCart
- string customerName
- vector cartItems + ShoppingCart()
+ ShoppingCart(string name)
+ string getCustomerName() const
+ void addItem(ItemToPurchase)
+ void removeItem(string)
+ void changeQuantity(string, int)
+ double getTotalCost()
+ void printCart()
addItem
use push_back to add the given ItemToPurchase argument to the end of cartItems
removeItem
loop through the cartItems vector and look for cartItems[i].getName() to match the given string argument
once found, call cartItems.erase(cartItems.begin() + i)
If the item is not found, output "Item not found in cart. Nothing removed."
changeQuantity
Loop through looking for a matching string, then call cartItems[i].setQuantity(n)
If the item is not found, output "Item not found in cart. Nothing modified."
totalCost
this one is a bit more complicated than you would first imagine. Create a running sum, initialized to zero, loop through cartItems, and add cartItems[i].getQuantity() * cartItems[i].getPrice() to your total as you go.
Note: For unit testing purposes, do not implement any input or output in any functions except main and printCart
printItems
Output customerName's Shopping Cart
Loop through cartItems and call printItemCost on each one
State the total at the end
Ex. of printCart output:
Ryan Hermle's Shopping Cart Air Jordans 2 @ $189.00 = $378.00
Chocolate Chips 5 @ $3.00 = $15.00
Beats by Dre Headphones 1 @ $128.00 = $128.00
Total: $521.00
main
prompt the user for a customer's name. Create an object of type ShoppingCart with this name.
be careful with your use of getline and cin, make sure to put cin.ignore() whenever a getline is followed by a cin.
Name: Ryan Hermle
- Prompt the user for three items:
Enter the item name:
Air Jordans
Enter the item price:
189
Enter the item quantity:
2
Enter the item name:
Chocolate Chips
Enter the item price:
3
Enter the item quantity:
5
Enter the item name:
Beats by Dre Headphones
Enter the item price:
128
Enter the item quantity:
1
Call printItems():
Ryan Hermle's Shopping Cart
Air Jordans 2 @ $189.00 = $378.00
Chocolate Chips 5 @ $3.00 = $15.00
Beats by Dre Headphones 1 @ $128.00 = $128.00
Total: $521.00
************************************************************************************************************************************
Here is a code from Part 1:
// ItemToPurchase.h #includeusing namespace std; class ItemToPurchase { private: string itemName; double itemPrice; int itemQuantity; public: ItemToPurchase(); ItemToPurchase(string name, double price, int quantity); void setName(string); void setPrice(double); void setQuantity(int); string getName() const; double getPrice() const; int getQuantity() const; void printItemCost(); }; ///////////////////////////////////// // ItemToPurchase.cpp #include #include "ItemToPurchase.h" using namespace std; ItemToPurchase::ItemToPurchase() { itemName = "none"; itemPrice = 0; itemQuantity = 0; } ItemToPurchase::ItemToPurchase(string n, double p, int q) { itemName = n; itemPrice = p; itemQuantity = q; } void ItemToPurchase::setName(string s){ itemName = s; } void ItemToPurchase::setPrice(double p){ itemPrice = p; } void ItemToPurchase::setQuantity(int q){ itemQuantity = q; } string ItemToPurchase::getName() const{ return itemName; } double ItemToPurchase::getPrice() const{ return itemPrice; } int ItemToPurchase::getQuantity() const{ return itemQuantity; } void ItemToPurchase::printItemCost(){ cout << itemName << " " << itemQuantity << " @ $" << itemPrice << " = " << (itemQuantity * itemPrice) << endl; } //////////////////////////////////////////// // main.cpp #include #include #include "ItemToPurchase.h" using namespace std; int main() { cout << fixed << setprecision(2); string name; double price; int quantity; cout << "Enter the item name: "; getline(cin, name); cout << "Enter the item price: "; cin >> price; cout << "Enter the item quantity: "; cin >> quantity; ItemToPurchase first(name, price, quantity); cin.ignore(); cout << " Enter the item name: "; getline(cin, name); cout << "Enter the item price: "; cin >> price; cout << "Enter the item quantity: "; cin >> quantity; ItemToPurchase second(name, price, quantity); cout << endl; first.printItemCost(); second.printItemCost(); double total = first.getPrice() * first.getQuantity() + second.getPrice() * second.getQuantity(); cout << " Total: $" << total << endl; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
