Python 7 Levels, L5, Unit 50, Mall Shopping Management System II

Powered By EmbedPress

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("Product information is:")
            for goods in shopdict.values():
                print(goods)
        else:
            print("No product in the inventory.")

    def shopping(self,shopdict):
        print("Hello\n=======Welcome to Python shopping mall==========")
        total_money = 0#reset to 0
        while True:
            self.show_goods(shopdict)
            id = input("Please input ID to purchase product(Input n to check)")
            if id == "n":
                print("Your total cost is:",total_money)
                break
            elif shopdict.get(id):
                num = int(input("Please input purchase quality:"))
                total_money = total_money + shopdict.get(id).price*num
                print("Product is added to shopping cart,total cost is:",total_money)
            else:
                print("Sorry,the ID you input does not exist.")

class SellerAdmin():

    def admin_work(self,shopdict):
        while True:
            num = input("1.Show product\n2.Add product\n3.Delete product\n4.Search Product\n5.Update product\n99.Quit\n"\
                        "Please pick an operation:")
            if num == "1":
                self.show_goods(shopdict)
            elif num == "2":
                self.add_goods(shopdict)
            elif num == "3":
                self.delet_goods(shopdict)
            elif num == "4":
                self.seek_goods(shopdict)
            elif num == "5":
                self.update_goods(shopdict)
            elif num == "99":
                print("Abort seller management system")
                break

    def show_goods(self,shopdict):
        if len(shopdict) > 0:
            print("Product information is:")
            for goods in shopdict.values():
                print(goods)
        else:
            print("No product in the inventory")

    def add_goods(self,shopdict):
        id = input("Please input product ID:")
        if shopdict.get(id):
            print("Sorry, input ID does not exist.")
        else:
            name = input("Please input product name")
            price = int(input("Please input product price"))
            goods = Goods(id, name, price)
            shopdict[id] = goods
            print("Add successfully")

    def delet_goods(self,shopdict):
        id = input("Please delete product ID to be deleted:")
        if shopdict.get(id):
            del(shopdict[id])
            print("Delete successfully")
        else:
            print("The input ID is invalid")

    def seek_goods(self,shopdict):
        name = input("Please input product ID to look for:")
        for i in shopdict.keys():
            if shopdict.get(i).name == name:
                print(shopdict.get(i))
                return
        else:
            print("The input product ID does not exist.")

    def update_goods(self,shopdict):
        id = input("Please input product ID to be updated:")
        if shopdict.get(id):
            name = input("Please update product name:")
            price = int(input("Please update product price:"))
            shopdict.get(id).name = name
            shopdict.get(id).price = price
            print("Update successfully")
        else:
            print("The input product ID does not exist.")
goods1 = Goods("001","Banana",15)
goods2 = Goods("002","Watermelon",20)
shopdict = {goods1.id:goods1,goods2.id:goods2}
sellerAdmin = SellerAdmin()
buyerAdmin = BuyerAdmin()
print("==========Shopping Mall Management System==========")
user = input("Please input admin name:")
if user == "admin":
    password = input("Please input admin password:")
    if password  == "123456":
        sellerAdmin.admin_work(shopdict)
    else:
        print("Unmatched user and password")
else:
    buyerAdmin.shopping(shopdict)

dahan1999

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts