Fruit shop to buy fruit system

Original code:

product_list = [{ 'name': ' apple ', 'price': 10} ,

              { 'name': ' durian ', 'price': 30} ,

              { 'name': ' strawberry ', 'price': 20} ,

              { 'name': ' pineapple ', 'price': 15} ,]

# 1 Create a shopping cart full bloom fruit

shopping_cart = {}

# 2 prompts the user for money

money_str = input ( ' Please show off your money: ')

if money_str.isdigit():

    user_money = int (money_str) # Type Conversion

    # 3 display merchandise

    for index, dic in enumerate(product_list, start=1):

        print ( ' fruit ID: {} , Name: {} , Price: {}'. the format (index, DIC [ 'name'], DIC [ '. price']))

    while True:

        # 4 Enter the serial number

        num_xh_str = input ( ' Enter number: ')

        if num_xh_str.isdigit():

            num_xh = int (num_xh_str) # Type Conversion

            if num_xh > 0 and num_xh <= len(product_list):

                # 5 Enter the number of

                num_sl_str = input ( ' Enter Number: ')

                if num_sl_str.isdigit():

                    num_sl = int (num_sl_str) # Type Conversion

                    # 1 , the total price of the number of required goods * price

                    # To find the serial number of the price of fruit

                    num_dj = product_list [num_xh - 1] [ 'price'] # Note that the index acquisition

                    product_total_money = num_dj * num_sl # purchase total price of a certain kind of fruit

                    # 2 , the total price of fruit and compares the user's money

                    if product_total_money <= user_money:

                        # Add the product to cart

                        # 1. get a corresponding number of trade names

                        product_name = product_list[num_xh - 1]['name']

                        ret = shopping_cart.get (product_name) # go cart to find the corresponding trade names

                        # None

                        if ret:

                            # Get some number of Central Plains cart

                            yysl = shopping_cart[product_name]

                            # Total number of

                            shopping_cart[product_name] = yysl + num_sl

                            print(shopping_cart)

 

                        else:

                            # Add the number of

                            shopping_cart[product_name] = num_sl

                            print(shopping_cart)

                            #   To cart query if you add the number of goods and if not added

                            # Output user remaining money

                        user_money = user_money - product_total_money

                        print ( ' User remaining money: ', user_money)

                    else:

                        print ( ' You are out of balance ')

                        break

                else:

                    print ( ' number is a number oh. ')

            else:

                print ( ' Please look carefully ')

        else:

            print ( ' number is composed of numbers, enter a number ')

else:

print ( ' your money how it is not a number ')

operation result:

Show me what your money: 500

Number of fruit: 1 , Name: Apple, price: 10

Number of fruit: 2 , Name: durian, price: 30

Number of fruit: 3 , Name: strawberry, price: 20

Number of fruit: 4 , Name: pineapple, price: 15

Please enter the number: 1

Please enter the number: 30

{ ' Apple ': 30}

User remaining money: 200

Enter Number: 2

Please enter a quantity: 5

{ ' Apple ': 30 ' durian ': 5}

User remaining money: 50

Enter Number: 3

Please enter the number: 300

Your balance is less than

 

class Goods(object):

 def __init__(self,id,name,price):

 self.id = id

 self.name = name

 self.price = price

  

 def __str__(self):

 info = " Number :% s \ t Product Name :% s \ t \ t Price :% d"% (self.id, self.name, self.price)

 return info

class ShopManager(object):

 

 

def __init__(self,path):

 # Path: means to read a file path shopdic : represents a container storage memory

 self.path = path

 self.shopdic = self.readFileToDic()

 

def readFileToDic(self):

 # Read the file, write to the dictionary

 f = open(self.path, 'r', encoding='utf-8')

 clist = f.readlines()

 f.close()

 index = 0

 shopdic = {}

 while index < len(clist):

 # The string of each line is divided, stored in a new list

 ctlist = clist[index].replace('\n', "").split("|")

 # The contents of each row are stored into one object

 good = Goods(ctlist[0],ctlist[1],int(ctlist[2]))

 # Will have to deposit to the collection

 shopdic[good.id] = good

 index = index + 1

 return shopdic

 

def writeContentFile(self):

 # The memory information is written to the file among them

 str1 = ''

 for key in self.shopdic.keys():

 good = self.shopdic[key]

 ele = good.id+"|"+good.name+"|"+str(good.price)+"\n"

 str1 = str1 + he

 f = open(self.path, 'w', encoding='utf-8')

 f.write(str1)

 f.close()

 

def addGoods(self):

 # Add method of goods

 id = input ( " Please enter the product number is added :>")

 if self.shopdic.get(id):

 print ( " Product ID already exists. Please choose! Re ")

 return

 name = input ( " Please enter the product name to add :>")

 price = int (input ( " Please enter commodity prices added :>"))

 good = Goods(id,name,price)

 self.shopdic[id] = good

 print ( " Add success! ")

 

def deleteGoods(self):

 # Delete item method

 id = input ( " Please enter delete item number :>")

 if self.shopdic.get(id):

 del self.shopdic[id]

 print ( " deleted successfully! ")

 else:

 print ( " product number does not exist! ")

 

def showGoods(self):

 # Show all product info

 print("="*40)

 for key in self.shopdic.keys():

 good = self.shopdic[key]

 print(good)

 print("="*40)

 

def adminWork(self):

 info = """

 ========== Welcome to the sea Oh good shopping malls ==========

 Enter the function number, you can select the following functions:

 Enter " 1 ": Displays information about the products

 Enter " 2 ": Add items

 Enter " 3 ": information deleted items

 Enter " 4 ": Exit the system function

 ==========================================

 """

 print(info)

 while True:

 code = input ( " Please enter the ID function :>")

 if code == "1":

  self.showGoods()

 elif code == "2":

  self.addGoods()

 elif code == "3":

  self.deleteGoods()

 elif code == "4":

  print ( " Thank you for your use, exiting system !! ")

  self.writeContentFile()

  break

 else:

  print ( " Enter the number in error, please re-enter! ")

 

def userWork(self):

 print ( "============== Welcome to Forex rebate shopping mall ==============")

 print ( " You can enter a number and the number of buy buy goods, enter the number n the checkout ")

 self.showGoods()

 total = 0

 while True:

 id = input ( " Please enter the purchase of goods number :>")

 if id == "n":

  print ( " The purchase of goods consumed a total of % d yuan, thank you for coming! "% (Total))

  break

 if self.shopdic.get(id):

  good = self.shopdic[id]

  num = int (input ( " Please enter the number of purchase :>"))

  total = total+good.price*num

 else:

  print ( " Enter the product number is incorrect, please check and re-enter! ")

 

 

 

def login(self):

 # Login feature

 print ( "========== Welcome sea Oh good shopping mall ==========")

 uname = input ( " Please enter a user name :>")

 password = input ( " Please enter your password :>")

 if uname == "admin":

 if password == "123456":

  print ( " Welcome, ADMIN Administrator ")

  self.adminWork()

 else:

  print ( " Administrator password error, login failed! ")

 else:

 print ( " Welcome, % S user "% (uname))

 # Execute the user's purchase function

 self.userWork()

if __name__ == '__main__':

 shopManage = ShopManager("shop.txt")

 shopManage.login()

Guess you like

Origin www.cnblogs.com/benming/p/12059430.html