Add a new option to the menu - allow the user to checkout and then process anoth
ID: 3778973 • Letter: A
Question
Add a new option to the menu - allow the user to checkout and then process another order.
It is up to you to decide how to handle the new menu option. You elso must document the program using Python comments.
Here is the code:
class KeychainOrder:
def __init__(self, price, num=0):
self.price = price
self.num = num
def addKeychains(self, num):
self.num += num
print("Keychains in order:", self.num)
def removeKeychains(self, num):
if(num<self.num):
self.num -= num
print("Keychains in order:", num)
else:
print("Don't have that many key chains in order.")
def getKeyChains(self):
return self.num
def checkout(self):
print("You have " + str(self.num) + " keychains.")
print("Key chain cost $" + str(self.price) + "each.")
print("Total cost is $" + str(self.price*self.num))
print("Thank you for your order!")
def __str__(self):
return "You have " + str(self.num) + " keychains. The total cost is $" + str(self.num*self.price)
def main():
keychainorder = KeychainOrder(20)
while(True):
choice = int(input("1. Add keychains to order 2. Remove keychains from order 3. View current order 4. Checkout "))
if choice == 1:
num = int(input("Number of keychains: "))
keychainorder.addKeychains(num)
elif choice == 2:
num = int(input("Number of keychains: "))
keychainorder.removeKeychains(num)
elif choice == 3:
print(keychainorder.__str__())
elif choice == 4:
keychainorder.checkout()
break
else:
print("Please enter a number between 1 to 4.")
main()
Explanation / Answer
class KeychainOrder:
def __init__(self, price, num=0):
self.price = price
self.num = num
def addKeychains(self, num):
self.num += num
print("Keychains in order:", self.num)
def removeKeychains(self, num):
if(num<self.num):
self.num -= num
print("Keychains in order:", self.num) #Added self.num instead of num
else:
print("Don't have that many key chains in order.")
def getKeyChains(self):
return self.num
def checkout(self):
print("You have " + str(self.num) + " keychains.")
print("Key chain cost $" + str(self.price) + "each.")
print("Total cost is $" + str(self.price*self.num))
print("Thank you for your order! ")
def __str__(self):
return "You have " + str(self.num) + " keychains. The total cost is $" + str(self.num*self.price)
def main():
keychainorder = KeychainOrder(20) #Price value in the class is intialized to 20$ and assigned the class to keychainorder
while(True): #While loop is set to "true" i.e while is an infinite loop
choice = int(input("1. Add keychains to order 2. Remove keychains from order 3. View current order 4. Checkout 5. Checkout and Start a new order "))
if choice == 1: #Add keychains to cart.
num = int(input("Number of keychains: "))
keychainorder.addKeychains(num)
elif choice == 2: #Remove keychains from existing cart
num = int(input("Number of keychains: "))
keychainorder.removeKeychains(num)
elif choice == 3: #To view the current order summary
print(keychainorder.__str__())
elif choice == 4: #To checkout the order
keychainorder.checkout()
break #Break is set to exit from the loop
elif choice == 5: #+++++ Added this choice to
keychainorder.checkout() #Performs same functionality as choice 4
keychainorder = KeychainOrder(20) #Re-creating the Same object keychainorder to re-intialize num=0
else:
print("Please enter a number between 1 to 5.")
main()
'''In Choice==5 we are re-creating the object as "keychainorder"
When ever we create any object for a class ,the object will be instantiated/initialized according to its __init__ function.
So in the above program we are reusing the same object (in choice 5) to reinitialize as __init__ function of the class KeychainOrder. So variable num will be started with num==0.'''
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.