综合训练 --商品管理系统(简单)

大宝的超市开业了,为了更好的管理商品信息,准备开发一个商品管理系统。

系统需要用户先登录,在进行操作,其中包含以下功能菜单:

1.显示商品列表

2.增加商品信息

3.删除商品

4.设置商品折扣

5.修改商品信息

6.退出

a.使用列表嵌套字典的方式保存用户数据(包含用户名,密码,姓名);

b.使用列表嵌套字典的方式保存商品数据(包含编号,名称,价格,折扣);

c.编写用户登录的函数,返回登录结果;

d.循环提示菜单,业务完毕时返回主菜单,退出时回到登录页面;

e.将功能菜单中的业务各自编写到函数中;

f.用户选择不同业务编号时,调用已写好的各种函数;

思路如下:

一、准备数据#用户数据user1={"用户名":"aaa","密码":"123","姓名":"张三"}

user2={"用户名":"bbb","密码":"123","姓名":"李四"}
user3={"用户名":"ccc","密码":"123","姓名":"王五"}
userList=[user1,user2,user3]#用户列表

p1={"编号":"1001","名称":"苹果","价格":"5","折扣":"1"}
p2={"编号":"1002","名称":"香蕉","价格":"3","折扣":"1"}
p3={"编号":"1003","名称":"牛奶","价格":"4","折扣":"1"}
p4={"编号":"1004","名称":"白菜","价格":"1.5","折扣":"1"}
p5={"编号":"1005","名称":"西瓜","价格":"2","折扣":"1"}
productsList=[p1,p2,p3,p4,p5]#商品列表

  

#思考对应的业务
#登录--对userList进行遍历,利用输入的账号密码与遍历的数据做比对
#增加、删除、修改都是对商品列表的增删改

  

#确定业务方法一共有
#登录
def login():
#显示商品列表
def showProducts():
  print("-编号---名称---价格---折扣")
  for product in productsList:
    print(product["编号"]+"---"+product["名称"]+"---"+str(product["价格"])+"---"+str(product["折扣"]))

  

#增加商品信息
def addProduct():
  #生成新的编号
  lista=[]#存放所有商品的编号
  for product in productList:
     lista.append(int(product["编号"]))
  newNum=str(max(lista)+1)
  name=input("请输入商品名称")
  price=float(input("请输入商品单价"))
  discount=1
  newProduct={"编号":newNum,"名称":name,"价格":price,"折扣":1}
  productList.append(newProduct)
  print("-----商品",name,"添加成功")
  showProducts()

  

#删除商品(通过编号删除)
def delProduct():
  while 1==1:
    msg=0#记录商品是否存在
    num=input("请输入要删除的商品编号")
    for product in productList:
      if num==product["编号"]:
        print("---正在删除",product["名称"],"商品····")
        productList.remove(product)#(删除商品)
        print("删除成功")
        msg=1
        break
    if msg==0:
      print("商品编号不存在")
      choice=int(input("重新输入请按2,取消请按1"))
      if choice==1
        break
      else:
        continue
    else:
      showProducts
      break
 

  

#设置商品折扣
def setDiscout():
  while 1==1
    msg==0
    num=input("请输入要删除的商品编号")
    for product in productList:
      if num==product["编号"]:
        newDiscout=float(input("请输入商品新折扣(0-0.1):"))
        product["折扣"]=newDiscount #设置折扣
        print("---商品",product["名称"],"折扣已设置成功",newDiscout*10),"折")
        msg=1
        break
    if msg==0:
      print("商品不存在")
      choice= int(input("取消请按1,重新输入请按2"))
      if choice ==1:
        break
      else:
        continue
     else:
       showProducts
       break  

  

#修改商品价格
def setPrice():
  while 1==1
    msg==0

    num=input("请输入要删除的商品编号")    

    for product in productList      
      if num==product["编号"]:        
        newPrice=float(input("请输入商品新价格:"))        
        product["价格"]=newPrice #设置价格        
        print("---商品",product["名称"],"价格已设置成功",newPrice,"元")        
        msg=1        
        break    
    if msg==0:      
      print("商品不存在")      
      choice= int(input("取消请按1,重新输入请按2"))
      if choice ==1:
        break
      else:
        continue
     else:
       showProducts
       break

  

#根据价格排序显示商品价格
def sort():
  choice=int(input("请选择升序或者降序(1、升序2、降序):"))
  pList=[]
  for proudct in productList:
  pList.append(product["价格"])
  pList=list(set(pList))#去掉重复的价格
  print("-编号---名称---价格---折扣-")
  if choice==1:       
    newList=sortde(pList)
    for price in newList:
      for product in productList:
        if price==product["价格"]:
          print(product["编号"]+"---"+product["名称"]+"---"+str(product["价格"])+"---"+s

   
   else:
    
    newList=sortde(pList,reverse=True)
    for price in newList:
      for product in productList:
        if price==product["价格"]:
          print(product["编号"]+"---"+product["名称"]+"---"+str(product["价格"
 
#----------------------
#显示主菜单,调用已经写好的业务函数

以上就是整个系统的业务基本结构。

  

#首先把所有业务方法写完再把业务联系起来
#登录
def login():
    msg="失败"
    while 1==1:
    uname=input("请输入用户名:")
    upwd=input("请输入密码:")
    for user in userList:
        if uname==user["用户名"]and upwd==user["密码"]:
            print("----验证成功!欢迎你",user["姓名"],"!")
    
            msg="成功"
            break
    if msg=="失败":
           print("用户名密码错误。请重新输入!")
           continue
    else:
            break
    return msg #返回登录结果

#调用登录方法
while 0==0
result=login()
if result=="成功":
    while 2==2:
    print("------主菜单-------")
    print("---1.显示商品列表")
    print("---2.增加商品信息")
    print("---3.删除商品")
    print("---4.设置商品折扣")
    print("---5.修改商品信息")
    print("---6.按照价格排序显示")
    print("---7.退出")
    choice=int(input("请输入业务编号(输入1-6):"))
 

  

 1 #显示商品列表
 2 if choice==1 3   showProducts()
 4 elif choice==2 5   addProduct()
 6 elif choice==3 7   delProduct()
 8 elif choice==4:  
 9   setDiscout()
10 elif choice==5:
11   setPrice()
12 elif choice==6:
13   sort()
14 elif choice==7:
15   print("正在退出")
16       break
17 else:
18   print("请重新选择功能")   

 

  

 

posted @ 2022-08-15 11:37  测试小潘  阅读(140)  评论(0)    收藏  举报