Question

4.18 Ch 7 Program: Online shopping cart (continued) (C++) This program extends the earlier "Online shopping...

4.18 Ch 7 Program: Online shopping cart (continued) (C++)

This program extends the earlier "Online shopping cart" program. (solution from previous lab assignment is provided in Canvas).

(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.


Ex. of PrintTotal() output:

John Doe's Shopping Cart - February 1, 2016
Number of Items: 8

Nike Romaleos 2 @ $189 = $378
Chocolate Chips 5 @ $3 = $15
Powerbeats 2 Headphones 1 @ $128 = $128

Total: $521


Ex. of PrintDescriptions() output:

John Doe's Shopping Cart - February 1, 2016

Item Descriptions
Nike Romaleos: Volt color, Weightlifting shoes
Chocolate Chips: Semi-sweet
Powerbeats 2 Headphones: Bluetooth headphones


(3) In main(), prompt the user for a customer's name and today's date. Output the name and date. Create an object of type ShoppingCart. (1 pt)

Ex.

Enter customer's name:
John Doe
Enter today's date:
February 1, 2016

Customer name: John Doe
Today's date: February 1, 2016


(4) Implement the PrintMenu() function. PrintMenu() has a ShoppingCart parameter, and outputs a menu of options to manipulate the shopping cart. Each option is represented by a single character. Build and output the menu within the function.

If the an invalid character is entered, continue to prompt for a valid choice. Hint: Implement Quit before implementing other options. Call PrintMenu() in the main() function. Continue to execute the menu until the user enters q to Quit. (3 pts)

Ex:

MENU
a - Add item to cart
d - Remove item from cart
c - Change item quantity
i - Output items' descriptions
o - Output shopping cart
q - Quit

Choose an option:


(5) Implement Output shopping cart menu option. (3 pts)

Ex:

OUTPUT SHOPPING CART
John Doe's Shopping Cart - February 1, 2016
Number of Items: 8

Nike Romaleos 2 @ $189 = $378
Chocolate Chips 5 @ $3 = $15
Powerbeats 2 Headphones 1 @ $128 = $128

Total: $521


(6) Implement Output item's description menu option. (2 pts)

Ex.

OUTPUT ITEMS' DESCRIPTIONS
John Doe's Shopping Cart - February 1, 2016

Item Descriptions
Nike Romaleos: Volt color, Weightlifting shoes
Chocolate Chips: Semi-sweet
Powerbeats 2 Headphones: Bluetooth headphones


(7) Implement Add item to cart menu option. (3 pts)

Ex:

ADD ITEM TO CART
Enter the item name:
Nike Romaleos
Enter the item description:
Volt color, Weightlifting shoes
Enter the item price:
189
Enter the item quantity:
2


(8) Implement remove item menu option. (4 pts)

Ex:

REMOVE ITEM FROM CART
Enter name of item to remove:
Chocolate Chips


(9) Implement Change item quantity menu option. Hint: Make new ItemToPurchase object and use ItemToPurchase modifiers before using ModifyItem() function. (5 pts)

Ex:

CHANGE ITEM QUANTITY
Enter the item name:
Nike Romaleos
Enter the new quantity:
3

C++ using vectors

my work form past code:

MAIN.CPP

#include

#include

#include "ItemToPurchase.h"

using namespace std;

//main

int main()

{

    //Declareimg objects

          ItemToPurchase p1,p2;

     string myName;

//string name

       int myPrice,myQuant;

    //Get item1

       cout<<"Item 1"<

       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"<

       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"<

     cout << p1.GetName() << " " << p1.GetQuantity() << " @ $"<

     cout << p2.GetName() << " " << p2.GetQuantity() << " @ $"<

     cout<<"\nTotal: $" << p1.GetQuantity()*p1.GetPrice()+p2.GetQuantity()*p2.GetPrice() << endl;

       system("pause");

    return 0;

}

ItemtoPurchase.cpp

#include

#include

#include "ItemToPurchase.h"

using namespace std;

//Constructor

ItemToPurchase::ItemToPurchase()

{

//Set the default values

itemName="none";

itemQuantity=0;

itemPrice=0;

}

//set itemQuantity
void ItemToPurchase::SetQuantity(int myQuant)

{

itemQuantity = myQuant;

}
//set itemPrice

void ItemToPurchase::SetPrice(int myPrice)
{

itemPrice = myPrice;

}

//set the itemQuantity

//return the itemName

string ItemToPurchase::GetName()

{

return itemName;

}
// set the itemName

void ItemToPurchase::SetName(string myName)

{

itemName = myName;

}

// return the itemPrice

int ItemToPurchase::GetPrice()

{

return itemPrice;

}

// return the itemQuantity

int ItemToPurchase::GetQuantity()

{

return itemQuantity;

}

ItemtoPurchase.h

//Header file

#ifndef _ITEMTOPURCHASE_H

#define _ITEMTOPURCHASE_H

#include

using namespace std;

//Declare class ItemToPurchase

class ItemToPurchase

{

//Declare the needed variables

private:

int itemPrice;

int itemQuantity;

string itemName;

public:

//Constructor

ItemToPurchase();

//Setter methods

void SetName(string myName);

void SetPrice(int myPrice);

void SetQuantity(int myQuant);

string GetName();

int GetPrice();

int GetQuantity();

};

#endif

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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:

ItemToPurchase();

ItemToPurchase(string, string, int, int); // constructor

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

___________________

// ItemToPurchase.cpp

#include <iostream>

#include <string>

#include "ItemToPurchase.h"

using namespace std;

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;}

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::PrintItemCost() {

cout << itemName << " " << itemQuantity << " @ $" << itemPrice << " = $" << 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:

ShoppingCart(string cName = "none", string cDate="none");

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;

}

// function to add item

void ShoppingCart::AddItem(ItemToPurchase item){

cartItems.push_back(item);

}

// function to remove item

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;
}
  


}

// function to modify item

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;
}
  
  

}

// function to get 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: " << this->cartItems.size() + 1 << 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;

}

}

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;

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;

cin.ignore();

getline(cin, itemDescription);

cout << "Enter the item price:" << endl;

cin >> itemPrice;

cout << "Enter the item quantity:" << endl

<< endl;

cin >> itemQuantity;

ItemToPurchase itemToAdd(itemName, itemDescription, itemPrice, itemQuantity);

return itemToAdd;

}

int main()

{

string itemToRemove;

string customerName;

string currentDate;

cout << "Enter customer's name:" << endl;

getline(cin, customerName);

cout << "Enter today's date:" << endl

<< endl;

getline(cin, currentDate);

cout << "Customer name: " << customerName << endl;

cout << "Today's date: " << currentDate << endl

<< endl;

ShoppingCart itemCart(customerName, currentDate);

char answer = PrintMenu();

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();

}

}

_______________________

Output:


_______________Thank You

Add a comment
Know the answer?
Add Answer to:
4.18 Ch 7 Program: Online shopping cart (continued) (C++) This program extends the earlier "Online shopping...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program).

    Ch 7 Program: Online shopping cart (continued) (Java)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 fieldsstring itemDescription - Initialized in default constructor to "none"Parameterized constructor to assign item name, item description, item price, and item quantity (default values of 0). (1 pt)Public member methodssetDescription() mutator & getDescription() accessor (2 pts)printItemCost() - Outputs the item name followed by the quantity, price, and subtotalprintItemDescription() - Outputs the...

  • 11.12 LAB*: Program: Online shopping cart (continued) This program extends the earlier "Online shopping cart" pr...

    11.12 LAB*: Program: Online shopping cart (continued)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 constructorImplement 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. Note: Some can be method stubs...

  • 7.11 LAB: Online shopping cart - Part 2 This program extends the earlier "Online shopping cart" program. (Consid...

    7.11 LAB: Online shopping cart - Part 2 This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase namedtuple to contain a new attribute. (2 pts) item_description (string) - Set to "none" in the construct_item() function Implement the following function with an ItemToPurchase as a parameter. print_item_description() - Prints item_name and item_description attribute for an ItemToPurchase namedtuple. Has an ItemToPurchase parameter. Ex. of print_item_description() output: Bottled Water: Deer Park, 12 oz....

  • I need help with this assignment, can someone HELP ? This is the assignment: Online shopping...

    I need help with this assignment, can someone HELP ? This is the assignment: Online shopping cart (continued) (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...

  • 8.7 LAB*: Program: Online shopping cart (Part 2)

    8.7 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 fieldsstring itemDescription - Initialized in default constructor to "none"Parameterized...

  • Need three seperate files. ShoppingCartManager.java ShoppingCart.java ItemsToPurchase.java These are from part 1 what do you mean...

    Need three seperate files. ShoppingCartManager.java ShoppingCart.java ItemsToPurchase.java These are from part 1 what do you mean by deep study 7.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...

  • Zybooks 11.12 LAB*: Program: Online shopping cart (continued) Python 3 is the code needed and this...

    Zybooks 11.12 LAB*: Program: Online shopping cart (continued) Python 3 is the code needed and this is in Zybooks Existing Code # Type code for classes here class ItemToPurchase: def __init__(self, item_name="none", item_price=0, item_quantity=0): self.item_name = item_name self.item_price = item_price self.item_quantity = item_quantity # def __mul__(self): # print_item_cost = (self.item_quantity * self.item_price) # return '{} {} @ ${} = ${}' .format(self_item_name, self.item_quantity, self.item_price, print_item_cost) def print_item_cost(self): self.print_cost = (self.item_quantity * self.item_price) print(('{} {} @ ${} = ${}') .format(self.item_name, self.item_quantity, self.item_price,...

  • CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer;...

    CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; 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) {...

  • Warm up: Online shopping cart (Part 1)

    8.6 LAB*: Warm up: Online shopping cart (Part 1)(1) Create two files to submit:ItemToPurchase.java - Class definitionShoppingCartPrinter.java - Contains main() methodBuild the ItemToPurchase class with the following specifications:Private fieldsString itemName - Initialized in default constructor to "none"int itemPrice - Initialized in default constructor to 0int itemQuantity - Initialized in default constructor to 0Default constructorPublic member methods (mutators & accessors)setName() & getName() (2 pts)setPrice() & getPrice() (2 pts)setQuantity() & getQuantity() (2 pts)(2) In main(), prompt the user for two items and create...

  • Can someone please help me with this Python code? Thank you in advance, Zybooks keeps giving me e...

    Can someone please help me with this Python code? Thank you in advance, Zybooks keeps giving me errors. Thanks in advance!! 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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT