Question

In: Computer Science

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

Solutions

Expert Solution

#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 = '{}: {}, %d oz.'.format(self.item_name, self.item_description, self.item_quantity)
        print(string)
        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):
        print('ADD ITEM TO CART')
        #prompt the name and description of item,price and Quentity
        item_name = str(input('Enter the item name:'))
        print()
        item_description = str(input('Enter the item description:'))
        print()
        item_price = int(input('Enter the item price:'))
        print()
        item_quantity = int(input('Enter the item quantity:'))
        print()

        #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('REMOVE ITEM FROM CART')

        #prompt the item to remove the list
        string = str(input('Enter name of item to remove:'))
        print()
        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]
                #set the flag value to true
                #break from the list
                flag=True
                break

            #Otherwiese set value to false
            else:
                flag=False
            i += 1

        #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('CHANGE ITEM QUANTITY')

        #Prompt the input item
        name = str(input('Enter the item name:'))
        print()

        #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:'))
                print()
                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.')
        print()

    #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(self):
        total_cost = self.get_cost_of_cart()
        if (total_cost == 0):
            print('SHOPPING CART IS EMPTY')
        else:
            self.output_cart()

    #Implement the method to print_descriptions      
    def print_descriptions(self):
        print('OUTPUT ITEMS\' DESCRIPTIONS')
        print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current_date),end='\n')
        print('\nItem Descriptions')

        for item in self.cart_items:
            print('{}: {}'.format(item.item_name, item.item_description))

    #Implement the method output_cart()
    def output_cart(self):
        print('OUTPUT SHOPPING CART')
        print('{}\'s Shopping Cart - {}'.format(self.customer_name, self.current_date))      
        print('Number of Items:', self.get_num_items_in_cart())
        print()
        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)))
            tc += (item.item_quantity * item.item_price)
        
        if len(self.cart_items) == 0:
            print('SHOPPING CART IS EMPTY')
        print()

        print('Total: ${}'.format(tc))

#Implement the method print_menu
def print_menu(customer_Cart):

    #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'):
        print(menu)

        #Prompt the Command
        command = input('Choose an option:')
        print()

        #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:')
            print()

        #If the input command is a
        if(command == 'a'):
            #call the method to the add elements to the cart
            customer_Cart.add_item()

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

def main():
    # Type main section of code here
    #prompt and read the customers name
    customer_name = str(input('Enter customer\'s name:'))
    print()

    #prompt the date
    current_date = str(input('Enter today\'s date:'))
    print('\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)

if __name__ == '__main__':
    main()

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

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....
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 -...
7.7 Ch 7 Program: Online shopping cart (continued) (C) This program extends the earlier "Online shopping...
7.7 Ch 7 Program: Online shopping cart (continued) (C) This program extends the earlier "Online shopping cart" program. (Consider first saving your earlier program). (1) Extend the ItemToPurchase struct to contain a new data member. (2 pt) char itemDescription[ ] - set to "none" in MakeItemBlank() Implement the following related functions for the ItemToPurchase struct. PrintItemDescription() Has an ItemToPurchase parameter. Ex. of PrintItemDescription() output: Bottled Water: Deer Park, 12 oz. (2) Create three new files: ShoppingCart.h - struct definition and...
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...
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...
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...
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 -...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT