Question

In: Computer Science

IN C++ 7.22 LAB*: Program: Online shopping cart (Part 2) This program extends the earlier "Online...

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:

  • Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)
  • Public member functions
    • SetDescription() mutator & GetDescription() accessor (2 pts)
    • PrintItemCost() - Outputs the item name followed by the quantity, price, and subtotal
    • PrintItemDescription() - Outputs the item name and description
  • Private data members
    • string itemDescription - Initialized in default constructor to "none"

Ex. of PrintItemCost() output:

Bottled Water 10 @ $1 = $10


Ex. of PrintItemDescription() output:

Bottled Water: Deer Park, 12 oz.


(2) Create three new files:

  • ShoppingCart.h - Class declaration
  • ShoppingCart.cpp - Class definition
  • main.cpp - main() function (Note: main()'s functionality differs from the warm up)

Build the ShoppingCart class with the following specifications. Note: Some can be function stubs (empty functions) initially, to be completed in later steps.

  • Default constructor
  • Parameterized constructor which takes the customer name and date as parameters (1 pt)
  • Private data members
    • string customerName - Initialized in default constructor to "none"
    • string currentDate - Initialized in default constructor to "January 1, 2016"
    • vector < ItemToPurchase > cartItems
  • Public member functions
    • GetCustomerName() accessor (1 pt)
    • GetDate() accessor (1 pt)
    • AddItem()
      • Adds an item to cartItems vector. Has parameter ItemToPurchase. Does not return anything.
    • RemoveItem()
      • Removes item from cartItems vector. Has a string (an item's name) parameter. Does not return anything.
      • If item name cannot be found, output this message: Item not found in cart. Nothing removed.
    • ModifyItem()
      • Modifies an item's description, price, and/or quantity. Has parameter ItemToPurchase. Does not return anything.
      • If item can be found (by name) in cart, check if parameter has default values for description, price, and quantity. If not, modify item in cart.
      • If item cannot be found (by name) in cart, output this message: Item not found in cart. Nothing modified.
    • GetNumItemsInCart() (2 pts)
      • Returns quantity of all items in cart. Has no parameters.
    • GetCostOfCart() (2 pts)
      • Determines and returns the total cost of items in cart. Has no parameters.
    • PrintTotal()
      • Outputs total of objects in cart.
      • If cart is empty, output this message: SHOPPING CART IS EMPTY
    • PrintDescriptions()
      • Outputs each item's description.

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;

}

Solutions

Expert Solution

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


Related Solutions

C++.This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1)...
C++.This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt) Public member functions SetDescription() mutator & GetDescription() accessor (2 pts) PrintItemCost() - Outputs the item name followed by the quantity, price, and subtotal PrintItemDescription() - Outputs the item name and description Private data members string itemDescription -...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1)...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class to contain a new attribute. (2 pts) item_description (string) - Set to "none" in default constructor Implement the following method for the ItemToPurchase class. print_item_description() - Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter. Ex. of print_item_description() output: Bottled Water: Deer Park, 12 oz. (2) Build the ShoppingCart class with the following data attributes and related methods....
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1)...
This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class to contain a new attribute. (2 pts) item_description (string) - Set to "none" in default constructor Implement the following method for the ItemToPurchase class. print_item_description() - Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter. Ex. of print_item_description() output: Bottled Water: Deer Park, 12 oz. (2) Build the ShoppingCart class with the following data attributes and related methods....
In python 3 please: This program extends the earlier "Online shopping cart" program. (Start working from...
In python 3 please: This program extends the earlier "Online shopping cart" program. (Start working from your Program 7). (1) Build the ShoppingCart class with the following data attributes and related methods. Note: Some can be method stubs (empty methods) initially, to be completed in later steps.(3 pt) ● Parameterized constructor which takes the customer name and date as parameters ● Attributes ○ customer_name (string) ○ current_date (string) ○ cart_items (list) ● Methods ○ add_item() ■ Adds an item to...
9.25 LAB*: Program: Online shopping cart (Part 2) Note: Creating multiple Scanner objects for the same...
9.25 LAB*: Program: Online shopping cart (Part 2) Note: Creating multiple Scanner objects for the same input stream yields unexpected behavior. Thus, good practice is to use a single Scanner object for reading input from System.in. That Scanner object can be passed as an argument to any methods that read input. This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase class per the following specifications: Private fields string itemDescription -...
This week you will write a program that mimics an online shopping cart . You will...
This week you will write a program that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. It should include all of the following: The basics - Add items to a cart until the user says "done" and then print out a list of items in the cart and a total price. - Ensure the user types in numbers for quantities -...
Python This week you will write a program in Python that mimics an online shopping cart...
Python This week you will write a program in Python that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. It should include all of the following: The basics - Add items to a cart until the user says "done" and then print out a list of items in the cart and a total price. - Ensure the user types in numbers...
Python This week you will write a program in Pyhton that mimics an online shopping cart...
Python This week you will write a program in Pyhton that mimics an online shopping cart . You will use all the programming techniques we have learned thus far this semester, including if statements, loops, lists and functions. It should include all of the following: The basics - Add items to a cart until the user says "done" and then print out a list of items in the cart and a total price. - Ensure the user types in numbers...
Create a Python 3 program that acts as a grocery store shopping cart. 1. Create a...
Create a Python 3 program that acts as a grocery store shopping cart. 1. Create a class named GroceryItem. This class should contain: - an __init__ method that initializes the item’s name and price - a get_name method that returns the item’s name - a get_price method that returns the item’s price - a __str__ method that returns a string representation of that GroceryItem - an unimplemented method is_perishable Save this class in GroceryItem.py. 2. Create a subclass Perishable that...
HOMEWORK PROJECT #1 – SHOPPING CART Part I. Create two files to submit: ItemToPurchase.java – Class...
HOMEWORK PROJECT #1 – SHOPPING CART Part I. Create two files to submit: ItemToPurchase.java – Class Definition ShoppingCartPrinter.java – Contains main() method Build the ItemToPurchase class with the following specifications: Specifications Description ItemToPurchase(itemName) itemName – The name will be a String datatype and Initialized in default constructor to “none”. ItemToPurchase(itemPrice) itemPrice – The price will be integer datatype and Initialized in default constructor to 0. ItemToPurchase(itemQuantity) itemQuantity – The quantity will be integer datatype Initialized in default constructor to 0....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT