PYTHON PLEASE PART 1: (1) Build the ItemToPurchase class with the following spec
ID: 3731171 • Letter: P
Question
PYTHON PLEASE PART 1:(1) Build the ItemToPurchase class with the following specifications: Attributes item_name (srting) item_price (float) item_quantity (int) Default constructor Initialize item’s name = “none”, item’s price =0, itme’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 2 items and create 2 objects of the ItemToPurchase class. (3) Add the costs of the2 items together and output the total cost. PART 2: (1) Extend the ItemToPurchase class to contain a new attribute: item_description (string) set to none in the constructor Implement the following method for the ItemToPurchase class: print_item_description()-Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter. (2) Build the ShoppingCart clasd 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 consumer name and date as parameters Attributes customer_name (string) set to none in constructor current_date (string) initialized in constructor to January 1, 2016 cart_items (list) Methods add_item() -Adds items 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’s name cannot be found, output this: “Item not found in cart. Nothing removed.” modify_item() -Modifys 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: “Item not found in cart. Nothing modified.” get_num_items_in_cart() -Return quantity of all items in cart. Has no parameters get_cost_of_cart() -Determines and returns the total cost of items in cart. Has no parameters. print_total() -Output of total objects in cart -If cart is empty, output: “Shopping cart is empty.” print_description() -Output each item’s description. (3) In main section of you code, prompt the user for a customer’s name and todays date. Output the name and date. Create an object of type ShoppingCart. (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 an invalid character is entered, continue to prompt the user for a valid one-Implement Quit before implementing other options. Call print_menu() in the main() function. Continue to execute the menu until the user enters q for Quit. (5) Implement Output shopping cart menu option. (6) Implement Output item’s description menu option. (7) Implement Add item to cart menu option. (8) Implement Remove item menu option. (9) Implement Change item quantity menu option. Make new ItemToPurchase object before using Modifyitem() method. PYTHON PLEASE PART 1:
(1) Build the ItemToPurchase class with the following specifications: Attributes item_name (srting) item_price (float) item_quantity (int) Default constructor Initialize item’s name = “none”, item’s price =0, itme’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 2 items and create 2 objects of the ItemToPurchase class. (3) Add the costs of the2 items together and output the total cost. PART 2: (1) Extend the ItemToPurchase class to contain a new attribute: item_description (string) set to none in the constructor Implement the following method for the ItemToPurchase class: print_item_description()-Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter. (2) Build the ShoppingCart clasd 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 consumer name and date as parameters Attributes customer_name (string) set to none in constructor current_date (string) initialized in constructor to January 1, 2016 cart_items (list) Methods add_item() -Adds items 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’s name cannot be found, output this: “Item not found in cart. Nothing removed.” modify_item() -Modifys 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: “Item not found in cart. Nothing modified.” get_num_items_in_cart() -Return quantity of all items in cart. Has no parameters get_cost_of_cart() -Determines and returns the total cost of items in cart. Has no parameters. print_total() -Output of total objects in cart -If cart is empty, output: “Shopping cart is empty.” print_description() -Output each item’s description. (3) In main section of you code, prompt the user for a customer’s name and todays date. Output the name and date. Create an object of type ShoppingCart. (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 an invalid character is entered, continue to prompt the user for a valid one-Implement Quit before implementing other options. Call print_menu() in the main() function. Continue to execute the menu until the user enters q for Quit. (5) Implement Output shopping cart menu option. (6) Implement Output item’s description menu option. (7) Implement Add item to cart menu option. (8) Implement Remove item menu option. (9) Implement Change item quantity menu option. Make new ItemToPurchase object before using Modifyitem() method. PART 1:
(1) Build the ItemToPurchase class with the following specifications: Attributes item_name (srting) item_price (float) item_quantity (int) Default constructor Initialize item’s name = “none”, item’s price =0, itme’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 2 items and create 2 objects of the ItemToPurchase class. (3) Add the costs of the2 items together and output the total cost. (1) Build the ItemToPurchase class with the following specifications: Attributes item_name (srting) item_price (float) item_quantity (int) Default constructor Initialize item’s name = “none”, item’s price =0, itme’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 2 items and create 2 objects of the ItemToPurchase class. (3) Add the costs of the2 items together and output the total cost. PART 2: (1) Extend the ItemToPurchase class to contain a new attribute: item_description (string) set to none in the constructor Implement the following method for the ItemToPurchase class: print_item_description()-Prints item_description attribute for an ItemToPurchase object. Has an ItemToPurchase parameter. (2) Build the ShoppingCart clasd 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 consumer name and date as parameters Attributes customer_name (string) set to none in constructor current_date (string) initialized in constructor to January 1, 2016 cart_items (list) Methods add_item() -Adds items 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’s name cannot be found, output this: “Item not found in cart. Nothing removed.” modify_item() -Modifys 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: “Item not found in cart. Nothing modified.” get_num_items_in_cart() -Return quantity of all items in cart. Has no parameters get_cost_of_cart() -Determines and returns the total cost of items in cart. Has no parameters. print_total() -Output of total objects in cart -If cart is empty, output: “Shopping cart is empty.” print_description() -Output each item’s description. (3) In main section of you code, prompt the user for a customer’s name and todays date. Output the name and date. Create an object of type ShoppingCart. (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 an invalid character is entered, continue to prompt the user for a valid one-Implement Quit before implementing other options. Call print_menu() in the main() function. Continue to execute the menu until the user enters q for Quit. (5) Implement Output shopping cart menu option. (6) Implement Output item’s description menu option. (7) Implement Add item to cart menu option. (8) Implement Remove item menu option. (9) Implement Change item quantity menu option. Make new ItemToPurchase object before using Modifyitem() method.
Explanation / Answer
Part A ::::::::::::::::::::
class ItemToPurchase:
def __init__(self):
self.item_name = "none"
self.item_price = 0.0
self.item_quantity = 0
def set_item(self,item_name,item_price,item_quantity):
self.item_name = item_name
self.item_price = item_price
self.item_quantity = item_quantity
def item_cost(self) :
return float(self.item_quantity)*self.item_price
def print_item_cost(self) :
print(self.item_name + " " + self.item_quantity +"@$"+self.item_price + " = " + str(item_cost()))
Item1 = ItemToPurchase()
Item2 = ItemToPurchase()
print("Enter name,price,quantiy for 1st item" )
item_name = raw_input("item_name : " )
item_price = raw_input("item_price : " )
item_quantity = raw_input("item_quantity : " )
Item1.set_item(item_name,item_price,item_quantity)
print("Enter name,price,quantiy for 2nd item" )
item_name = raw_input("item_name : " )
item_price = raw_input("item_price : " )
item_quantity = raw_input("item_quantity : " )
Item2.set_item(item_name,item_price,item_quantity)
print("Total for both items : " + str(Item1.item_cost() + Item2.item_cost()))
PartB :::::::::::::::::::::::::::
class ItemToPurchase:
def __init__(self):
self.item_name = "none"
self.item_price = 0.0
self.item_quantity = 0
self.item_description = "none"
def set_item(self):
print("Enter name,price,quantiy for item" )
item_name = raw_input("item_name : " )
item_price = raw_input("item_price : " )
item_quantity = raw_input("item_quantity : " )
item_description = raw_input("item_description : " )
self.item_name = item_name
self.item_price = float(item_price)
self.item_quantity = int(item_quantity)
self.item_description = item_description
def item_cost(self) :
return float(self.item_quantity)*float(self.item_price)
def print_item_cost(self) :
print(self.item_name + " " + self.item_quantity +"@$"+self.item_price + " = " + str(item_cost()))
def print_item_description(item) :
print(item.description)
class ShoppingCart :
def __init__(self,customer_name="none",current_date="January 1 ,2016") :
self.current_date = current_date
self.customer_name = customer_name
self.cart_items = []
def add_item(self,item) :
self.cart_items.append(item)
def find_item_index(self,name) :
for i in range(len(self.cart_items)) :
if(self.cart_items[i].item_name == name) :
return i
return -1
def remove_item(self,name) :
index = self.find_item_index(name)
if(index!=-1) :
del self.cart_items[index]
else :
print("Item not found in cart. Nothing removed.")
def modify_item(self,item) :
index = self.find_item_index(item.item_name)
if(index!=-1) :
self.cart_items[index] = item
else :
print("Item not found in cart. Nothing modified.")
def get_num_items_in_cart(self) :
total = 0
for item in cart_items :
total = total + item.item_qunatity
return total
def get_cost_of_cart(self) :
total = 0.0
for item in self.cart_items :
total = total + item.item_cost()
return total
def print_total(self) :
items = len(cart_items)
if(items == 0) :
print("Shopping cart is empty " )
else :
print("total items " + str(items))
def print_description(self) :
for item in self.cart_items :
print(item.item_name + " " +item.item_description)
if(len(self.cart_items) == 0) :
print("Cart is Empty ")
shopcart = ShoppingCart("manish","12 aug")
def print_menu() :
print("Enter a to output shopping cart ")
print("Enter b to add item to shopping cart ")
print("Enter c to remove output shopping cart ")
print("Enter d to modify shopping cart ")
print("Enter q quit")
while(True) :
print_menu()
in1 = raw_input()
if(in1 == "q" ) :
break
if(in1 == "a") :
shopcart.print_description()
if(in1 == "b") :
item = ItemToPurchase()
item.set_item()
shopcart.add_item(item)
if(in1 == "c") :
name = raw_input("name of item to remove : ")
shopcart.remove_item(name)
if(in1 == "d") :
item = ItemToPurchase()
item.set_item()
shopcart.modify_item(item)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.