python实现超市商品销售管理系统

yipeiwu_com6年前Python基础

本文实例为大家分享了python超市商品销售管理系统的具体代码,供大家参考,具体内容如下

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()

更多学习资料请关注专题《管理系统开发》。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python使用Turtle库绘制动态钟表

python使用Turtle库绘制动态钟表

Python函数库众多,而且在不断更新,所以学习这些函数库最有效的方法,就是阅读Python官方文档。同时借助Google和百度。 本文介绍的turtle库对应的官方文档地址 绘制动态钟...

利用Python暴力破解zip文件口令的方法详解

利用Python暴力破解zip文件口令的方法详解

前言 通过Python内置的zipfile模块实现对zip文件的解压,加点料完成口令破解 zipfile模块用来做zip格式编码的压缩和解压缩的,zipfile里有两个非常重要的cla...

Python目录和文件处理总结详解

1、判断目录是否存在、判断文件是否存在、创建目录、重命名目录或文件 import os #获取当前目录路径: E:\Work\Projects\python print(os.ge...

python 动态加载的实现方法

脚本语言都有一个优点,就是动态加载。lua语言有这个优点,python也有这个特性。说简单点就是,如果开发者发现自己的代码有bug,那么他可以在不关闭原来代码的基础之上,动态替换模块。替...

python截取两个单词之间的内容方法

1. __init__ 初始化文件路径,关键字1,关键字2; 2. key_match 使用with open 方法,以二进制方式(也可以改成utf-8,GB2312)读取文件内容(支持...