使用面向对象的思想实现一个货物进销存系统。
1、属性:
编号、名称、价钱、数量、计量单位、类别
2、功能:
(1)添加货物
(2)修改货物
(3)移除货物
(4)展示货物
(5)按照货物名称展示货物
(6)统计总价
(7)按照货物名称统计总价
class Goods(object):
# 初始化货物属性
def __init__(self, serial, name, price, count, unit, type):
# 编号默认为0
self.serial = serial
# 名称
self.name = name
# 价钱自我检测
self.__price = 0
self.set_price(price)
# 数量默认为0
self.__count = 0
self.set_count(count)
# 单位
self.unit = unit
# 类型
self.type = type
# 货物的价钱要进行自我检测
def set_price(self, price):
if price > 0:
self.__price = price
else:
self.__price = 0
# 获取价钱
def get_price(self):
return self.__price
# 数量进行自我检测
def set_count(self, count):
if count > 0:
self.__count = count
else:
self.__count = 0
# 获取数量
def get_count(self):
return self.__count
# 测试代码
if __name__ == '__main__':
g = Goods("001", "掌心脆", 0.5, 20, "包", "零食")
print(g.__dict__)
if "001" in g.__dict__.values():
print("在")
else:
print("不在")
# 导入货物模块下的货物类
from goods import Goods
class System(object):
# 创建字典属性,用来存储临时数据
def __init__(self):
# 创建数据结构
self.goodsdict = {
"001": {'serial': '001', 'name': '掌心脆', '_Goods__price': 0.5, '_Goods__count': 20, 'unit': '包',
'type': '零食'}}
# 导航栏
def navigation(self):
print("============进销存系统==============")
print("货物录取------------ 1")
print("货物修改------------ 2")
print("货物移除------------ 3")
print("货物展示------------ 4")
print("按名称统计货物--------- 5")
print("统计总价------------ 6")
print("按名称统计总价--------- 7")
print("退出系统------------ 8")
# 货物录入(即写入数据)
def add(self):
"""
该函数主要用于添加货物或商品
:return: None
"""
# 用户输入错误或可以循环重复输入
while True:
serial = input("请输入货物编号:")
# 对编号进行检测,必须是整数
if self.is_type(serial):
# 判断编号是否在数据结构中
if self.exist(serial):
print("该货物编号已被占用,请重新分配...")
continue
# 不在数据结构中
else:
# 输入商品名称
name = input("请输入货物名称:")
# 输入商品价钱
price = input("请输入价钱:")
# 判断商品的价钱是否是浮点数
if self.is_type(price, isint=True):
# 输入商品数量
count = input("请输入数量:")
# 判断商品数量是否是浮点数
if self.is_type(count, isint=True):
# 输入商品计量单位
unit = input("请输入计量单位:")
# 输入商品的类型
type = input("请输入类型:")
# 创建商品对象
commodity = Goods(serial, name, float(price), float(count), unit, type)
# 获取包含商品对象属相的字典,并将其存入数据结构中
self.goodsdict[serial] = commodity.__dict__
print("添加成功!")
a = input("是否继续当前操作(Y/N)?")
if a == "Y" or a == "y":
continue
else:
return
else:
print("格式有误,请重新输入...")
continue
else:
print("格式有误,请重新输入...")
continue
else:
print("格式输入有误,请重新输入...")
continue
# 检测数字的类型
def is_type(self, number, isint=False):
"""
该函数主要对编号、价钱、数量等数字类型进行检测
:param number: 数字类型(int、float)
:param isint: True:不是整数类型,False(默认):是整数类型
:return: 返回一个判断的结果
"""
# 如果不是False,则为True,判断是否是int类型
if not isint:
# 使用异常捕获来确定编号是否可以转换为int类型
try:
int(number)
except:
print("您输入的格式有误,请重新输入...")
return False
else:
return True
# 为False,则判断是否是float类型
else:
# 使用异常捕获来确定数量和价钱是否可以转换为float类型
try:
float(number)
except:
print("您输入的格式有误,请重新输入..")
return False
else:
return True
# 检测元素是否在数据结构中
def exist(self, element):
"""
该函数用于判断某个元素是否在字典中
:param element: 元素
:return: 返回一个布尔类型的判断结果
"""
if element in self.goodsdict:
return True
else:
return False
# 修改货物,本质是覆盖原有数据
def amend(self):
"""
该函数主要通过编号修改货物信息
:return:
"""
while True:
serial = input("请输入货物编号:")
# 判断编号是否在数据结构中
if self.exist(serial):
# 存在则修改
# 输入商品名称
name = input("请输入货物名称:")
# 输入商品价钱
price = input("请输入价钱:")
# 判断商品的价钱是否是浮点数
if self.is_type(price, isint=True):
# 输入商品数量
count = input("请输入数量:")
# 判断商品数量是否是浮点数
if self.is_type(count, isint=True):
# 输入商品计量单位
unit = input("请输入计量单位:")
# 输入商品的类型
type = input("请输入类型:")
# 创建商品对象
commodity = Goods(serial, name, float(price), float(count), unit, type)
# 获取包含商品对象属相的字典,并将其存入数据结构中
self.goodsdict[serial] = commodity.__dict__
print("修改成功!")
a = input("是否继续当前操作(Y/N)?")
if a == "Y" or a == "y":
continue
else:
return
else:
print("格式有误,请重新输入...")
continue
else:
print("格式有误,请重新输入...")
continue
# 不存在则继续输入
else:
print("无该货物信息,请重新输入...")
continue
# 移除货物
def remove(self):
"""
该函数主要通过货物编号来移除货物
:return:
"""
while True:
serial = input("请输入编号:")
# 判断编号是否在数据结构中
if self.exist(serial):
# 存在则删除
del self.goodsdict[serial]
print(self.goodsdict)
print("删除成功!")
a = input("是否继续当前操作(Y/N)?")
if a == "Y" or a == "y":
continue
else:
return
else:
print("无该货物信息,请重新输入...")
continue
# 货物展示
def show_all(self):
"""
该函数主要用于货物展示
:return: None
"""
while True:
for i in self.goodsdict.values():
print(f"编号:%s|名称:%s|价格:%.2f元|数量:%.2f|单位:%s|类别:%s" % (
i["serial"], i["name"], i["_Goods__price"], i["_Goods__count"], i["unit"], i["type"]))
a = input("是否继续当前操作(Y/N)?")
if a == "Y" or a == "y":
continue
else:
return
# 按照货物名称统计货物
def name_show(self):
"""
该函数主要通过货物名称统计货物
:return: None
"""
while True:
name = input("请输入货物名称:")
for element in self.goodsdict.values():
if name == element["name"]:
print(f"编号:%s|名称:%s|价格:%.2f元|数量:%.2f|单位:%s|类别:%s" % (
element["serial"], element["name"], element["_Goods__price"], element["_Goods__count"], element["unit"], element["type"]))
a = input("是否继续当前操作(Y/N)?")
if a == "Y" or a == "y":
continue
else:
return
# 统计总价
def total_price(self):
"""
该函数主要用于统计货物总价
:return:
"""
while True:
sum_price = 0
for element in self.goodsdict.values():
# 货物单价
money = float(element["_Goods__price"])
# 货物数量
count = float(element["_Goods__count"])
# 某个货物的总价
price = money * count
# 全部货物的总价
sum_price += price
print(f"编号:%s|名称:%s|价格:%.2f元|数量:%.2f|单位:%s|类别:%s|总价:%.2f元" % (
element["serial"], element["name"], element["_Goods__price"], element["_Goods__count"], element["unit"],
element["type"], price))
print(f"总价:{sum_price}元")
a = input("是否继续当前操作(Y/N)?")
if a == "Y" or a == "y":
continue
else:
return
# 按照货物名称统计总价
def name_price(self):
"""
该函数主要通过货物名称统计货物总价
:return:
"""
while True:
name = input("请输入货物名称:")
for element in self.goodsdict.values():
if name == element["name"]:
money = element["_Goods__price"]
count = element["_Goods__count"]
price = money * count
print(f"编号:%s|名称:%s|价格:%.2f元|数量:%.2f|单位:%s|类别:%s" % (
element["serial"], element["name"], element["_Goods__price"], element["_Goods__count"],
element["unit"], element["type"]))
print(f"总价:{'{:.2f}'.format(price)}")
a = input("是否继续当前操作(Y/N)?")
if a == "Y" or a == "y":
continue
else:
return
# 数据结构写入文件
def write(self):
with open("goods.txt", "w", encoding="utf-8")as f:
f.write(str(self.goodsdict))
# 将文件中的数据加载到数据结构中
def read(self):
with open("goods.txt", "r", encoding="utf-8")as f:
date = eval(f.read())
self.goodsdict = date
# 入口函数
def run(self):
# 加载文件
self.read()
while True:
self.navigation()
order = input("请输入指令:")
if order == "1":
# 录取货物
self.add()
elif order == "2":
# 修改货物
self.amend()
elif order == "3":
# 移除货物
self.remove()
elif order == "4":
# 展示全部货物
self.show_all()
elif order == "5":
# 通过货物名称统计货物
self.name_show()
elif order == "6":
# 统计总价
self.total_price()
elif order == "7":
# 通过货物名称统计总价
self.name_price()
elif order == "8":
# 写入文件
self.write()
# 退出程序
return
else:
continue
# 测试代码
if __name__ == '__main__':
s = System()
# s.run()
s.name_price()
from system import System
# 程序主入口,主函数
def main():
s = System()
s.run()
if __name__ == '__main__':
main()