Exercise:
class Goods():
def __init__(self,id,name,price):
self.id = id
self.name = name
self.price = price
def __str__(self):
msg = "ID:{0},Name:{1},Price:{2}".format(self.id,self.name,self.price)
return msg
class BuyerAdmin():
def show_goods(self,shopdict):
if len(shopdict) > 0:
print("Goods Information is:")
for goods in shopdict.values():
print(goods)
else:
print("No goods in inventory")
def shopping(self,shopdict):
print("Hello\n=======Welcome to Python shopping mall==========")
total_money = 0#initialized to 0
while True:
self.show_goods(shopdict)
id = input("Please input goods ID to purchase(input n to check)")
if id == "n":
print("You spend :",total_money)
elif shopdict.get(id):
num = int(input("Please input purchase quality: "))
total_money = total_money + shopdict.get(id).price*num
print("Goods has been added to shopping cart, tota cost is: ",total_money)
else:
print("Sorry, the ID you input does not exist.")
goods1 = Goods("001","Banana",15)
goods2 = Goods("002","Watermelon",20)
shopdict = {goods1.id:goods1,goods2.id:goods2}
buyerAdmin = BuyerAdmin()
buyerAdmin.shopping(shopdict)
[…] 47 Object-oriented Example Exercises| |____Unit 48 Student Information Management System| |____Unit 49 Mall Shopping Management System (Part 1)| |____Unit 50 Mall Shopping Management System (Part 2)| |____Unit 51 Object-oriented advanced| […]