水果店买水果系统

原代码:

product_list = [{'name': '苹果', 'price': 10},

              {'name': '榴莲', 'price': 30},

              {'name': '草莓', 'price': 20},

              {'name': '菠萝', 'price': 15}, ]

# 1 创建一个购物车盛放水果

shopping_cart = {}

# 2 提示用户输入钱

money_str = input('请展示一下你的钱:')

if money_str.isdigit():

    user_money = int(money_str)  # 类型转换

    # 3 展示商品

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

        print('水果的序号:{},名称:{},价格:{}'.format(index, dic['name'], dic['price']))

    while True:

        # 4 输入序号

        num_xh_str = input('请输入序号:')

        if num_xh_str.isdigit():

            num_xh = int(num_xh_str)  # 类型转换

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

                # 5 输入数量

                num_sl_str = input('请输入数量:')

                if num_sl_str.isdigit():

                    num_sl = int(num_sl_str)  # 类型转换

                    # 1,求商品的总价格  数量*价钱

                    # 根据序号找到水果的价格

                    num_dj = product_list[num_xh - 1]['price']  # 注意索引的获取

                    product_total_money = num_dj * num_sl  # 购买某一种水果的总价钱

                    # 2,水果总价钱和用户的钱进行比较

                    if product_total_money <= user_money:

                        # 将商品添加到购物车

                        # 1.获取序号对应的商品名称

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

                        ret = shopping_cart.get(product_name)  # 去购物车查找对应的商品名称

                        # None

                        if ret:

                            # 获取购物车中原有的数量

                            yysl = shopping_cart[product_name]

                            # 总共的数量

                            shopping_cart[product_name] = yysl + num_sl

                            print(shopping_cart)

 

                        else:

                            #添加数量

                            shopping_cart[product_name] = num_sl

                            print(shopping_cart)

                            #  去购物车进行查询如果有就添加数量 如果没有就添加商品和数量

                            # 输出用户剩余的钱

                        user_money = user_money - product_total_money

                        print('用户剩余的钱:', user_money)

                    else:

                        print('您的余额不足')

                        break

                else:

                    print('数量是数字哦。')

            else:

                print('请仔细看')

        else:

            print('序号是由数字组成,请输入数字')

else:

print('你的钱怎么不是数字呢')

运行结果:

请展示一下你的钱:500

水果的序号:1,名称:苹果,价格:10

水果的序号:2,名称:榴莲,价格:30

水果的序号:3,名称:草莓,价格:20

水果的序号:4,名称:菠萝,价格:15

请输入序号:1

请输入数量:30

{'苹果': 30}

用户剩余的钱: 200

请输入序号:2

请输入数量:5

{'苹果': 30, '榴莲': 5}

用户剩余的钱: 50

请输入序号:3

请输入数量:300

您的余额不足

 

class Goods(object):

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

 self.id = id

 self.name = name

 self.price = price

  

 def __str__(self):

 info = "编号:%s\t商品名称:%s\t\t价格:%d"%(self.id,self.name,self.price)

 return info

class ShopManager(object):

 

 

def __init__(self,path):

 # path:表示读取文件的路径 shopdic:表示存放内存的容器

 self.path = path

 self.shopdic = self.readFileToDic()

 

def readFileToDic(self):

 # 读取文件,写入到字典中

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

 clist = f.readlines()

 f.close()

 index = 0

 shopdic = {}

 while index < len(clist):

 # 将每一行的字符串进行分割,存放到新的列表中

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

 # 将每行的内容存放到一个对象中

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

 # 将对向存放到集合中

 shopdic[good.id] = good

 index = index + 1

 return shopdic

 

def writeContentFile(self):

 # 将内存当中的信息写入到文件当中

 str1 = ''

 for key in self.shopdic.keys():

 good = self.shopdic[key]

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

 str1 = str1 + ele

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

 f.write(str1)

 f.close()

 

def addGoods(self):

 # 添加商品的方法

 id = input("请输入添加商品编号:>")

 if self.shopdic.get(id):

 print("商品编号已存在,请重新选择!")

 return

 name = input("请输入添加商品名称:>")

 price = int(input("请输入添加商品价格:>"))

 good = Goods(id,name,price)

 self.shopdic[id] = good

 print("添加成功!")

 

def deleteGoods(self):

 # 删除商品的方法

 id = input("请输入删除商品编号:>")

 if self.shopdic.get(id):

 del self.shopdic[id]

 print("删除成功!")

 else:

 print("商品编号不存在!")

 

def showGoods(self):

 # 展示所有商品信息

 print("="*40)

 for key in self.shopdic.keys():

 good = self.shopdic[key]

 print(good)

 print("="*40)

 

def adminWork(self):

 info = """

 ==========欢迎进入好海哦购物商场==========

 输入功能编号,您可以选择以下功能:

 输入1”:显示商品的信息

 输入2”:添加商品的信息

 输入3”:删除商品的信息

 输入4”:退出系统功能

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

 """

 print(info)

 while True:

 code = input("请输入功能编号:>")

 if code == "1":

  self.showGoods()

 elif code == "2":

  self.addGoods()

 elif code == "3":

  self.deleteGoods()

 elif code == "4":

  print("感谢您的使用,正在退出系统!!")

  self.writeContentFile()

  break

 else:

  print("输入编号有误,请重新输入!!")

 

def userWork(self):

 print(" ==============欢迎进入外汇返佣购物商场==============")

 print("您可输入编号和购买数量选购商品,输入编号为n则结账")

 self.showGoods()

 total = 0

 while True:

 id = input("请输入购买商品编号:>")

 if id == "n":

  print("本次购买商品共消费%d元,感谢您的光临!"%(total))

  break

 if self.shopdic.get(id):

  good = self.shopdic[id]

  num = int(input("请输入购买数量:>"))

  total = total+good.price*num

 else:

  print("输入商品编号有误,请核对后重新输入!")

 

 

 

def login(self):

 # 登录功能

 print("==========欢迎登录好海哦购物商场==========")

 uname = input("请输入用户名:>")

 password = input("请输入密码:>")

 if uname == "admin":

 if password == "123456":

  print("欢迎您,admin管理员")

  self.adminWork()

 else:

  print("管理员密码错误,登录失败!")

 else:

 print("欢迎你,%s用户"%(uname))

 #执行用户的购买功能

 self.userWork()

if __name__ == '__main__':

 shopManage = ShopManager("shop.txt")

 shopManage.login()

猜你喜欢

转载自www.cnblogs.com/benming/p/12059430.html
今日推荐