This programming assignment must be do in python 3 and will look at using a clas
ID: 3778534 • Letter: T
Question
This programming assignment must be do in python 3 and will look at using a class you design yourself. You will create a KeychainOrder class and use that class to simulate online ordering.
Defining the KeychainOrder Class
Implement a class to represent a keychain order. Your class should have the following methods:
1- --init--(self, price, num=0)
price is a number indicating the price per keychain and num is an integer indicating the num- ber of keychains currently in the order (the number should default to zero). This constructor should create a KeychainOrder.
2- addKeychains(self, num)
Adds num keychains to the order and prints out the new number of keychains in the order.
3- removeKeychains(self, num)
Subtracts num keychains from the order and prints out the new number of keychains in the order. This method should not allow the user to remove more keychains than are currently part of the order.
Returns the number of keychains currently in the order (accessor method).
Prints out the following information:
The blanks should be filled in appropriately for the current order.
6- --str--(self)
Returns a string giving the following information about the order.
The blanks should be filled in appropriately for the current order. Note that the string should be returned and not be printed inside this method. The method named --str-- is special in Python. If asked to convert an object into a string Python uses this method, if it is present.
Using the KeychainOrder Class
Implement a main function to use and test your KeychainOrder class. The main function should create an instance of the KeychainOrder class and then use an interactive while loop to present the user with different options for their shopping experience. You must use a while loop to repeatedly present the menu and choose the appropriate action to take - calling the main function will cause many problems because we are working with a programmer-defined class and should be avoided. The menu should contain the following information:
Based on the user’s input, the appropriate class method should be called.
If the user enters 1, the program should ask how many keychains the user wants to purchase
and then the addKeychains method should be called.
If the user enters 2, the program should ask how many keychains the user wants to remove
from their order and then the removeKeychains method should be called.
If the user enters 3, the order should be printed (this will implicitly call the --str-- method -we don’t have to explicitly call it by name!).
If the user enters 4, the checkout method should be called and then the break statement can be used to exit the loop.
Your program must meet the following specifications:
1- Create the KeychainOrder class and implement all methods (as described above).
2- Implement a main function to test the KeychainOrder class. The main function must use a while loop to implement the menu.
3- For the pseudocode, clearly indicate what code will be in the KeychainOrder class methods and which code will be in the main function.
4- Document the program using Python comments.
Here is a sample run of the program.
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:", 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()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.