从0基础学习Python (21)[进销存管理系统[案例]](v2.0-完善版)

从0基础学习Python (Day21)[进销存管理系统[案例]](v2.0-完善版)

进销存管理系统[案例]

前言:

​ 在2020.08.02[进销存管理系统(v1.0版本)],(注:CSDN原创博客“从0基础学习Python(16)”)我们将进销存系统的整体框架实现了出来,明白了系统需要实现的功能,但是因为缺乏面向对象后续的相关知识,无法将现实生活中的问题在程序上解决,使得系统并不完善,这几天通过对“面向对象“完整的学习,我们对面向对象的编程思想有了一个更加系统的了解,今天我们将把这个进销存管理系统用这几天所学进行完善规整。

案例的学习根本目的是为了将这一部分的知识整体的串联起来,
实践是最好的学习方式,认真的完成案例会将自身整体的编程水平提高一个可观的高度

程序主体分析

1.第一步创建包

​ [包将有联系的模块组织在一起,放在同一个文件夹下面,并且在这个文件夹里面创建一个名字为_init_.py文件,那么这个文件夹就称为包]

如图所示:本程序未使用_init_.py文件,图中不做体现
在这里插入图片描述

2.我们需要创建一个模块,来协助我们的功能实现

​ 这个模块的任务是帮我们判断输入的值是否正确,并且返回我们规定范围内的值

​ 我们进销存系统需要录入货物的id:编号 name:名称 count:数量 price:单价 unit:单位 type:种类

​ 编号id:编号必须是数字,而且是整数

​ 名称name:商品的名称不做要求,名字可以是任意的字符

​ 数量count:数量可以是浮点型,因为计量单位有斤,但是不能为负数,数量不能为负,要符合实际

​ 单价price:单价可以是浮点型,但不能为负数,可以为0,商品赠送价钱就可为0 但负数贴钱就不符合实际了

​ 单位unit:单位不做要求根据商品而定

​ 种类type:种类不做要求根据商品而定

[Goods.py]内容

class Good(object):
    def __init__(self, id, name, count, price, unit, type):
        # id:编号     name:名称     count:数量    price:单价    unit:单位     type:种类
        self.id = id
        self.name = name
        self.count = 0
        self.price = 0
        self.unit = unit
        self.type = type
        self.set_count(count)
        self.set_price(price)
	
    #数量的限制,如果输入小于0,则返回0
    def set_count(self, count):
        if count > 0:
            self.count = count
        else:
            self.count = 0

    def get_count(self):
        return self.count

    ##单价的限制,如果输入小于0,则返回0
    def set_price(self, price):
        if price > 0:
            self.price = price
        else:
            self.price = 0

    def get_price(self):
        return self.price

    def __str__(self):
        return f"{self.id},{self.name},{self.price},{self.count},{self.unit},{self.type}"

3.完成程序的主体部分,实现我们所需的功能

​ 以下七个功能为咱们主要功能,每一个功能都使用一个函数来完成,本次完善程序是在上次的程序基础上添了程序的异常检测等功能,使程序更加趋于完善

​ 1.录入货物:增加两个判断函数,第一点是判断你录入的货物编号在不在数据库中,第二点是判断输入的数据类型是否正确,如果不正确就循环输入

​ 2.添加货物 3.修改货物 4.取出货物 5.移除货物 6.查看货物 7.退出系统

[Main.py]内容

from GoodsSystem.Goods import Good
#导入判断数据的模块

class Wms(object):
    #[1]程序的准备工作,创建列表,输出程序功能框,将程序可以实现的功能展示给用户
    def __init__(self):
        self.good_data = []

    def sendout(self):
        print("=================================================")
        print("---------------阿里巴巴·进销存系统------------------")
        print("------------------1.录入货物----------------------")
        print("------------------2.添加货物----------------------")
        print("------------------3.修改货物----------------------")
        print("------------------4.取出货物----------------------")
        print("------------------5.移除货物----------------------")
        print("------------------6.查看货物----------------------")
        print("------------------7.退出系统----------------------")
    
    # aaa,bbb,ccc 完成了录入货物 [bbb,ccc为aaa的判断条件]
    def aaa_good(self):
        good_id = input("请输入货物编号:")
        while (not self.ccc_good(good_id, isint=True) or self.bbb_good(good_id)):
            if self.bbb_good(good_id):
                print("该编号已占用")
            good_id = input("请输入货物编号:")
        good_name = input("请输入货物名称:")
        good_pirce = input("请输入货物单价:")
        while not self.ccc_good(good_pirce):
            good_pirce = input("请输入货物单价:")
        good_count = input("请输入货物数量:")
        while not self.ccc_good(good_count):
            good_count = input("请输入货物数量:")
        good_unit = input("请输入货物计量单位:")
        good_type = input("请输入货物类型:")
        good = Good(good_id, good_name, float(good_pirce), int(good_count), good_unit, good_type)
        self.good_data.append(good)
        print("添加货物成功!")

    # 判断条件,判断数据库中有没有你要添加ID
    def bbb_good(self, id):
        for item in self.good_data:
            if item.id == id:
                return True
            else:
                return False

    # 判断条件,判断输入的格式是否正确
    def ccc_good(self, str, isint=False):
        if isint:
            try:
                res = int(str)
            except:
                print("输入数据格式有误请从新输入")
                return False
            else:
                return True
        else:
            try:
                res = float(str)
            except:
                print("输入数据格式有误请从新输入")
                return False
            else:
                return True

    # 实现添加功能
    def ddd_good(self):
        while True:
            good_id = input("请输入货物编号:")
            for item in self.good_data:
                if item.id == good_id:
                    aa = float(input("请输入添加的数量:"))
                    item.count += aa
                    print("添加成功")
                    print(item.count)
                    return
                else:
                    pass

    # 实现修改功能
    def eee_good(self):
        while True:
            good_id = input("请输入货物编号:")
            for item in self.good_data:
                if item.id == good_id:
                    good_name = input("请输入货物名称:")
                    item.name = good_name
                    good_pirce = input("请输入货物单价:")
                    while not self.ccc_good(good_pirce):
                        good_pirce = input("请输入货物单价:")
                    item.price = good_pirce
                    good_count = input("请输入货物数量:")
                    while not self.ccc_good(good_count):
                        good_count = input("请输入货物数量:")
                    item.count = good_count
                    good_unit = input("请输入货物计量单位:")
                    item.unit = good_unit
                    good_type = input("请输入货物类型:")
                    item.type = good_type

                    print("添加成功")
                    return
                else:
                    pass

    # 实现取出功能
    def fff_good(self):
        while True:
            good_id = input("请输入货物编号:")
            for item in self.good_data:
                if item.id == good_id:
                    aa = float(input("请输入取出货物的数量:"))
                    item.count -= aa
                    print("取出成功")
                    print(item.count)
                    return
                else:
                    pass

    # 实现移除功能
    def ggg_good(self):
        while True:
            good_id = input("请输入货物编号")
            for item in self.good_data:
                if item.id == good_id:
                    self.good_data.remove(item)
                    print("移除成功")
                    return
                else:
                    pass

    # 查看货物
    def hhh_good(self):
        print("----货物信息如下---")
        for item in self.good_data:
            print(f"编号{item.id}  名称{item.name}    数量{item.count}    单价{item.price}    单位 {item.unit}    种类{item.type}")

    # 主循环【主程序】
    def run(self):
        while True:
            self.sendout()
            index = input("$$请输入操作指令$$:")
            if index == "1":
                self.aaa_good()
            elif index == "2":
                self.ddd_good()
            elif index == "3":
                self.eee_good()
            elif index == "4":
                self.fff_good()
            elif index == "5":
                self.ggg_good()
            elif index == "6":
                self.hhh_good()
            elif index == "7":
                print("拜拜了您嘞奥里给")
                return
            else:
                print("你输的啥鬼玩意,再来一遍")

4.调用整个程序,执行!

[Wms.py]内容

from GoodsSystem.Wms import Wms

re=Wms()
re.run()

总结:

​ @通过案例的学习,我们对面向对象的思想有了更加深入的了解,也明白了面向对象和面向过程最本质的区别如下,学习程序要认真的去刨析,能坚持别人不能坚持的,才能拥有别人不能拥有的


面向过程:—侧重于怎么做?
1.把完的成某一个需求的 所有步骤 从头到尾 逐步实现
2.根据开发要求,将某些功能独立的代码封装成一个又一个函数
3.最后完成的代码,就是顺序的调用不同的函数
【特点】:
1.注重步骤与过程,不注重职责分工
2.如果需求复杂,代码会变得很复杂
3.开发复杂项目,没有固定的套路,开发难度很大
面向对象:–谁来做?
相比较函数,面向对象是更大的封装,根据职责在一个对象中封装多个方法
1.在完成某一个需求前,首先确定职责–要做的事(方法)
2.根据职责确定不同的对象,在对象内部封装不同的方法(多个)
3.最后完成代码,就是顺序的让不同的对象调用不同的方法
【特点】:
1.注重对象和职责,不同的对象承担不同的职责
2.更加适合对复杂的需求变化,是专门应对复杂项目的开发,提供的固定套路
3.需要在面向过程的基础上,再学习一些面向对象的语法

Day21-------END

猜你喜欢

转载自blog.csdn.net/weixin_47766667/article/details/107870998