Question

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

LAB

ACTIVITY

11.12.1: LAB*: Program: Online shopping cart (continued)

Python 3 please

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

#Declare the class ItemToPurchase

class ItemToPurchase:

    #Parameter Constructor

    def __init__(self, item_name='none', item_price=0, item_quantity=0, item_description = 'none'):

        self.item_name = item_name

        self.item_price = item_price

        self.item_quantity = item_quantity

        self.item_description = item_description

     

    #Implement the method

    def print_item_cost(self):

        #print the output in a specifed format

        string = '{} {} @ ${} = ${}'.format(self.item_name, self.item_quantity, self.item_price,

                                                (self.item_quantity * self.item_price))

        cost = self.item_quantity * self.item_price

        return string, cost

    #Implement the method print_item_description

    def print_item_description(self):

        string = '{}: {}'.format(self.item_name, self.item_description)

        print(string, end='\n')

        return string

#Declare the class ShoppingCart

class ShoppingCart:

    #Parameter Constructor

    def __init__(self, customer_name = 'none', current_date = 'January 1, 2016', cart_items = []):

        self.customer_name = customer_name

        self.current_date = current_date

        self.cart_items = cart_items

    #Implement method to add item in the shopping cart

    def add_item(self, string):

        print('\nADD ITEM TO CART', end='\n')

        #prompt the name and description of item,price and Quentity

        item_name = str(input('Enter the item name: '))

        item_description = str(input('\nEnter the item description: '))

        item_price = int(input('\nEnter the item price: '))

        item_quantity = int(input('\nEnter the item quantity: '))

        #Append the above values in to the list

        self.cart_items.append(ItemToPurchase(item_name, item_price, item_quantity, item_description))

    #Implement the method to delete the item in the cart

    def remove_item(self):

        print('\nREMOVE ITEM FROM CART', end='\n')

        #prompt the item to remove the list

        string = str(input('Enter name of item to remove: '))

        i = 0

        #Using for-loop to iterate every item

        for item in self.cart_items:

            #If item found delete in the list

            if(item.item_name == string):             

                del self.cart_items[i]

                i += 1

                #set the flag value to true

                #break from the list

                flag=True

                break

            #Otherwiese set value to false

            else:

                flag=False

        #IF the value not found

        if(flag==False):

            #print the message

            print('Item not found in cart. Nothing removed')

    #Implement the method modifyitem to chane the Quantity

    def modify_item(self):

        print('\nCHANGE ITEM QUANTITY', end='\n')

        #Prompt the input item

        name = str(input('Enter the item name: '))     

        #Using for-loop to iterate every item

        for item in self.cart_items:

            #If item found update Quantity in the list

            if(item.item_name == name):

                quantity = int(input('Enter the new quantity: '))

                item.item_quantity = quantity

                #set the flag value to true

                #break from the list

                flag=True

                break

            #Otherwiese set value to false

            else:

                flag=False

        #IF the value not found

        if(flag==False):

            #print the message

            print('Item not found in cart. Nothing modified')

    #implement method to compute total number of items in the cart

    def get_num_items_in_cart(self):

        num_items=0

        #Using for-loop to iterate the cart

        for item in self.cart_items:

            #ADD the Quantities

            num_items= num_items+item.item_quantity

        #return the num_Items

        return num_items

    #Implement the method

    def get_cost_of_cart(self):

        total_cost = 0

        cost = 0

        #Using for-loop to iterate the list

        #mulitply the price and Quantity

        #add value to the Total_Cost

        for item in self.cart_items:

            cost = (item.item_quantity * item.item_price)

            total_cost += cost

        #return the value

        return total_cost

    #Implement the method to print the total

    def print_total():

        total_cost = get_cost_of_cart()

        if (total_cost == 0):

            print('SHOPPING CART IS EMPTY')

        else:

            output_cart()

    #Implement the method to print_descriptions    

    def print_descriptions(self):

        print('\nOUTPUT ITEMS\' DESCRIPTIONS')

        print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current_date),end='\n')

        print('\nItem Descriptions', end='\n')

        for item in self.cart_items:

            print('{}: {}'.format(item.item_name, item.item_description), end='\n')

    #Implement the method output_cart()

    def output_cart(self):

        new=ShoppingCart()

        print('\nOUTPUT SHOPPING CART', end='\n')

        print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current_date),end='\n')    

        print('Number of Items:', new.get_num_items_in_cart(), end='\n\n')

        tc = 0

        for item in self.cart_items:

            print('{} {} @ ${} = ${}'.format(item.item_name, item.item_quantity,

                                             item.item_price, (item.item_quantity * item.item_price)), end='\n')

            tc += (item.item_quantity * item.item_price)

        print('\nTotal: ${}'.format(tc), end='\n')

#Implement the method print_menu

def print_menu(ShoppingCart):

    customer_Cart = newCart

    string=''

    #declare the string menu

    menu = ('\nMENU\n'

    'a - Add item to cart\n'

    'r - Remove item from cart\n'

    'c - Change item quantity\n'

    'i - Output items\' descriptions\n'

    'o - Output shopping cart\n'

    'q - Quit\n')

    command = ''

    #Using while loop

    #to iterate until user enters q

    while(command != 'q'):

        string=''

        print(menu, end='\n')

        #Prompt the Command

        command = input('Choose an option: ')

        #repeat the loop until user enters a,i,r,c,q commands

        while(command != 'a' and command != 'o' and command != 'i' and command != 'r'

              and command != 'c' and command != 'q'):

            command = input('Choose an option: ')

        #If the input command is a

        if(command == 'a'):

            #call the method to the add elements to the cart

            customer_Cart.add_item(string)

        #If the input command is o

        if(command == 'o'):

            #call the method to the display the elements in the cart

            customer_Cart.output_cart()

        #If the input command is i

        if(command == 'i'):

            #call the method to the display the elements in the cart

            customer_Cart.print_descriptions()

        #If the input command is i

        if(command == 'r'):

            customer_Cart.remove_item()

        if(command == 'c'):

            customer_Cart.modify_item()

# Type main section of code here

#prompt and read the customers name

customer_name = str(input('Enter customer\'s name: \n'))

#prompt the date

current_date = str(input('\nEnter today\'s date: \n'))

#print the name and date

print('Customer name:', customer_name, end='\n')

print('Today\'s date:', current_date, end='\n')

#call the class with the parameters

newCart = ShoppingCart(customer_name, current_date)

#print the details.

print_menu(newCart)

Add a comment
Know the answer?
Add Answer to:
11.12 LAB*: Program: Online shopping cart (continued) This program extends the earlier "Online shopping cart" pr...
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
  • 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,...

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

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

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

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

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

  • 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