Question

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 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 (empty methods) initially, to be completed in later steps.

  • Parameterized constructor which takes the customer name and date as parameters (2 pts)
  • Attributes
  • customer_name (string) - Initialized in default constructor to "none"
  • current_date (string) - Initialized in default constructor to "January 1, 2016"
  • cart_items (list)
  • Methods
  • add_item()
    • Adds an item to cart_items list. Has parameter ItemToPurchase. Does not return anything.
  • remove_item()
    • Removes item from cart_items list. 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.
  • modify_item()
    • 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.
  • get_num_items_in_cart() (2 pts)
    • Returns quantity of all items in cart. Has no parameters.
  • get_cost_of_cart() (2 pts)
    • Determines and returns the total cost of items in cart. Has no parameters.
  • print_total()
    • Outputs total of objects in cart.
    • If cart is empty, output this message: SHOPPING CART IS EMPTY
  • print_descriptions()
    • Outputs each item's description.

Ex. of print_total() 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 print_descriptions() 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 section of your code, 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 print_menu() function. print_menu() 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 print_menu() 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
r - 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 before using ModifyItem() method. (5 pts)

Ex:

CHANGE ITEM QUANTITY
Enter the item name:
Nike Romaleos
Enter the new quantity:
3
0 0
Add a comment Improve this question Transcribed image text
Answer #1

IF YOU HAVE ANY DOUBTS COMMENT BELOW I WILL BE THERE TO HELP YOU

RATE THUMBSUP PLEASE

ANSWER:

EXPLANATION:

CODE:

ShoppingCart.cpp

#include "ShoppingCart.h"

#include <iostream>

#include<string>

using namespace std;

ShoppingCart::ShoppingCart(string name = "none", string date = "January 1, 2016")

{

this->name=name;

this->date=date;

}

const string& ShoppingCart::getDate() const

{

return date;

}

void ShoppingCart::setDate(const string& date)

{

this->date = date;

}

const string& ShoppingCart::getName() const

{

return name;

}

void ShoppingCart::setName(const string& name)

{

this->name = name;

}

void ShoppingCart::add(ItemToPurchase &newItem)

{

int set = 0;

if(newItem.GetName().compare("none") != 0)

{

//vector<ItemToPurchase>::iterator it;

//for (it = item.begin() ; it != item.end(); ++it)

for(int i=0;i<item.size();i++)

{

if(item.at(i).GetName() == newItem.GetName())

{

cout<<"Item is already in cart. Nothing added."<<endl;

set = 1;

break;

}

}

if(set == 0)

item.push_back(newItem);

}

}

void ShoppingCart::remove(string itemName)

{

//vector<ItemToPurchase>::iterator it;

int check = 0;

//for (it = item.begin() ; it != item.end(); ++it)

for(int i=0;i<item.size();i++)
{

if(item.at(i).GetName() == itemName)
{

cout<<"found...removing "<< item.at(i).GetName() << endl;

item.erase(item.begin()+i);
check = 1;
break;

}

}

if(check == 0)

{

cout<<"Shopping cart is empty."<<endl;

cout<<"No item found with name "<<itemName<<endl;

}

}

void ShoppingCart::update(ItemToPurchase &updateItem)

{

///vector<ItemToPurchase>::iterator it;

int i=0;

//for (it = item.begin() ; it != item.end(); ++it)

for(i=0;i<item.size();i++)

{

if(item.at(i).GetName() == updateItem.GetName())

{

item.at(i).SetName(updateItem.GetName());

item.at(i).SetDescription(updateItem.GetDescription());

item.at(i).SetPrice(updateItem.GetPrice());

item.at(i).SetQuantity(updateItem.GetQuantity());

break;

}

}

if(i == 0)

{

cout<<"Shopping cart is empty."<<endl;

cout<<"No itemfound with name "<<updateItem.GetName()<<endl;

}

}

ShoppingCart::~ShoppingCart()

{

}

void ShoppingCart::showDescription()

{

int j=0;
cout << "OUTPUT ITEMS'S DESCRIPTIONS" << endl;
cout<<""<<name<<"'s Shopping Cart - "<<date<<endl;

//for (it = item.begin() ; it != item.end(); ++it)

for(j=0;j<item.size();j++)

{

cout << item.at(j).GetName() << ": " << item.at(j).GetDescription()<< endl;

}

if(j == 0)

cout<<"Shopping cart is empty."<<endl;

}

void ShoppingCart::showCart()

{

//vector<ItemToPurchase>::iterator it;

int i = 0;

double tot = 0;

cout<<""<<name<<"'s Shopping Cart - "<<date<<endl;

//for (it = item.begin() ; it != item.end(); ++it)

for(i=0;i<item.size();i++)

{

cout << item.at(i).GetName() << " " << item.at(i).GetQuantity()
   << " @ $" << item.at(i).GetPrice() << " = $"
      << item.at(i).GetPrice() * item.at(i).GetQuantity() << endl;

tot = tot + (item.at(i).GetPrice() * item.at(i).GetQuantity());

}

if(i == 0)

{

cout<<"Shopping cart is empty."<<endl;

}
cout<<"Total: $"<<tot<<endl;

}

void ShoppingCart::showOption()

{

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;


}

Main.cpp

#include <iostream>
#include<iomanip>

#include "ItemToPurchase.h"

#include "ShoppingCart.h"

using namespace std;

int main()

{

ItemToPurchase item;

string name, desc;

double price;

int q;

string custName,date;

string option = "";

cout<<"Enter Customer's Name: ";

getline(cin, custName);

cout<<"Enter Today's Date: ";

getline(cin, date);

cout << endl;

cout << "Customer's name: " << custName << endl;
cout << "Today's date: " << date << endl << endl;
ShoppingCart sCart(name, date);

sCart.setName(custName);

sCart.setDate(date);

sCart.showOption();

while(option.compare("q") != 0 )

{

cout<<"Choose an option: ";

getline(cin, option);


if(option.compare("a") == 0)

{

cout << "Enter the item name: ";

getline(cin, name);

cout << "Enter the item description: ";

getline(cin, desc);

cout << "Enter the item price: ";
cin >> price;
cout << "Enter the item quantity: ";
cin >> q;
item.SetName(name);

item.SetDescription(desc);

item.SetPrice(price);

item.SetQuantity(q);

sCart.add(item);

item.clean();

cin.ignore();

}

else if(option.compare("d") == 0)
{

cout << "Enter the item name: ";

getline(cin, name);

sCart.remove(name);

}

else if(option.compare("c") == 0)
{

cout << "Enter the item name: ";

getline(cin, name);

cout << "Enter the item quantity: ";

cin >> q;

item.SetName(name);

item.SetQuantity(q);

sCart.update(item);

item.clean();

cin.ignore();

}

else if(option.compare("i") == 0)

sCart.showDescription();

else if(option.compare("o") == 0)

sCart.showCart();

else
{
   if(option != "q")
   cout << "Error! Invalid Option entered." << endl;
}

cout << endl;

}

cout<<"Program Terminated...";

return 0;

}

RATE THUMBSUP PLEASE

THANKS

Add a comment
Know the answer?
Add Answer to:
Can someone please help me with this Python code? Thank you in advance, Zybooks keeps giving me e...
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
  • 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....

  • 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,...

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

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

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

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

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

  • 11.11 LAB: Warm up: Online shopping cart (1) Build the ItemToPurchase class with the following specifications:...

    11.11 LAB: Warm up: Online shopping cart (1) Build the ItemToPurchase class with the following specifications: Attributes (6 pts) item_name (string) item_price (float) item_quantity (int) Default constructor (2 pt) Initializes item's name = "none", item's price = 0, item's quantity = 0 Method print_item_cost() Ex. of print_item_cost() output: Bottled Water 10 @ $1 = $10 (2) In the main section of your code, prompt the user for two items and create two objects of the ItemToPurchase class. (4 pts) Ex:...

  • Please use C Programming language (Not C++) 7.6 LAB*: Warm up: Online shopping cart (Part 1)...

    Please use C Programming language (Not C++) 7.6 LAB*: Warm up: Online shopping cart (Part 1) (1) Create three files to submit: • Item ToPurchase.h - Struct definition and related function declarations • Item ToPurchase.c-Related function definitions • main.c-main function Build the ItemToPurchase struct with the following specifications: • Data members (3 pts) • char itemName • int itemPrice • int itemQuantity • Related functions • MakeltemBlank0 (2 pts) Has a pointer to an item To Purchase parameter. Sets item's...

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