python:(面向对象思想)实现进销存系统

python:(面向对象思想)实现进销存系统


要求:

使用面向对象的思想实现一个货物进销存系统。

​ 1、属性:

​ 编号、名称、价钱、数量、计量单位、类别

​ 2、功能:

​ (1)添加货物

​ (2)修改货物

​ (3)移除货物

​ (4)展示货物

​ (5)按照货物名称展示货物

​ (6)统计总价

​ (7)按照货物名称统计总价

1、货物类

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("不在")

2、系统类

# 导入货物模块下的货物类
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()

3、主函数

from system import System

# 程序主入口,主函数
def main():
    s = System()
    s.run()


if __name__ == '__main__':
    main()

  • 6
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

御弟謌謌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值