python 编程练习——根据一张单据设计点单收银系统

Python YX ⋅ 于 2020-04-22 15:39:50 ⋅ 最后回复由 热心市民小杨 2020-05-07 22:41:24 ⋅ 369 阅读

日常生活中,我们点外卖,去超市购物,都会收到如下图的单据。
想象一下是你在运营这家超市或者其他店铺,根据你购物收到的单据,设计一款点单收银软件。
具体需求如下:

  • 通过修改txt,读入txt,增添删除店铺销售产品的列表,包括品名,单价,数量等
  • 通过在命令行打印菜单,使用按键选择客户购买的产品,结合下单时间作记录,写入txt保存购买记录
  • 客户确认下单之后可在屏幕打印(格式化输出)如图类似的单据,包括店名、购买商品信息、下单时间、店铺联系方式等
  • 有统计功能,可总结出当日最畅销的产品,当日销售总计,库存等功能,统计结果写入txt
  • 可退出系统
  • 可自行设计其他功能

这是一个开放式习题,大家可以发挥自己的想象力。此系统没有UI设计,只在command line下运行,通过按键和print来做交互。

参考格式化输出,字典,文件读写等章节的内容

file

成为第一个点赞的人吧 :bowtie:
回复数量: 1
  • 热心市民小杨
    2020-05-07 22:41:24
    import time
    import datetime
    
    # # 创建shop_goods.txt文件用于存放商品信息
    # with open('shop_goods.txt', 'w') as goods_flie:
    #     pass
    # # 创建consumption_records.txt文件用于存放用户的购买记录
    # with open('consumption_records.txt', 'w') as cmr_flie:
    #     pass
    # # 创建daily_sale.txt文件用来存放每日销售情况
    # with open('daily_sale.txt', 'w') as ds_file:
    #     pass
    dict = {}  # 用来存放顾客消费记录
    total = 0  # 存放总金额
    dict_save = {}  # 统计当日销售情况
    
    def showGoods():
        with open('shop_goods.txt', 'r', encoding='utf-8') as rstream:
            goodsFile = rstream.read()
            if len(goodsFile) == 0:
                print('请先添加商品!')
            else:
                print('商品信息如下!')
                str = '%s\t%s\t%s\t%s' % ('编号', '名称', '单价', '数量')
                print(str)
                print("*" * 30)
                print(goodsFile)
                print('*' * 30)
    
    def addGoods():
        id = input('请输入商品编号:')
        name = input('请输入商品名称:')
        goods_save = []
        with open('shop_goods.txt', 'r', encoding='utf-8') as rstream:
            while True:
                goodsFile = rstream.readlines()
                if not goodsFile:
                    break
                else:
                    # print(goodsFile)
                    for i in goodsFile:
                        str = i.split()
                        goods_save.extend(str)
        if (id in goods_save) or (name in goods_save):
            print('该商品已存在!无需再次添加!')
        else:
            price = int(input('请输入商品价格:'))
            num = int(input('请输入商品数量:'))
            str = '%s\t%s\t%s\t\t%s' % (id, name, price, num)
            with open('shop_goods.txt', 'a', encoding='utf-8') as wstream:
                wstream.write(str + '\n')
    
    def delGoods():
        goods = []
        with open('shop_goods.txt', 'r', encoding='utf-8') as rstream:
            while True:
                goodsFile = rstream.readlines()
                if not goodsFile:
                    break
                else:
                    # print(goodsFile)
                    for i in goodsFile:
                        str = i.split()
                        goods.append(str)
        del_goods = input('请输入你要删除商品的编号或者名称:')
        for n, i in enumerate(goods):
            if del_goods in i:
                del goods[n]
        # print(goods)
        with open('shop_goods.txt', 'r+', encoding='utf-8') as rstream:
            rstream.truncate(0)
        for good in goods:
            str = '%s\t%s\t%s\t\t%s\n' % (good[0], good[1], good[2], good[3])
            with open('shop_goods.txt', 'a', encoding='utf-8') as wstream:
                wstream.writelines(str)
        print('删除成功!可通过选项1查看删除情况!')
    
    def charge():
        global total
        while True:
            goods = []
            with open('shop_goods.txt', 'r', encoding='utf-8') as rstream:
                while True:
                    goodsFile = rstream.readlines()
                    if not goodsFile:
                        break
                    else:
                        for i in goodsFile:
                            string = i.split()
                            goods.append(string)
            commodity = input('请输入顾客购买的商品编号或者名称:')
            if commodity == 'n':
                print('顾客选购商品结束!')
                break
            else:
                num = int(input('请输入顾客购买的数量:'))
                with open('shop_goods.txt', 'r+', encoding='utf-8') as rstream:
                    rstream.truncate(0)
                for n, i in enumerate(goods):
                    if commodity in i:
                        if num <= int(goods[n][3]):
                            dict_save[commodity] = num
                            price = int(goods[n][2])
                            count = int(goods[n][3]) - num
                            s = price * num
                            total += (price * num)
                            dict[i[1]] = s
                            string1 = '%s\t%s\t%s\t\t%s\n' % (i[0], i[1], i[2], str(count))
                        else:
                            print('该商品数量有限,请确定数量后再买!')
                            string1 = '%s\t%s\t%s\t\t%s\n' % (i[0], i[1], i[2], i[3])
                    else:
                        string1 = '%s\t%s\t%s\t\t%s\n' % (i[0], i[1], i[2], i[3])
                    with open('shop_goods.txt', 'a', encoding='utf-8') as wstream:
                        wstream.writelines(string1)
        print('顾客购买的商品总价格为:{}元'.format(total))
    
    def printInvoice():
        print('该顾客的消费如下!')
        print('商品\t  消费')
        print('$' * 15)
        for k, i in dict.items():
            print(k + '\t\t' + str(i))
        print('$' * 15)
        print('消费总额为:{}元'.format(total))
        print('出票时间:', datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S %A'))
    
    def viewSales():
        if len(dict) == 0:
            print('今日尚无商品出售!')
        else:
            dict_list = sorted(dict_save.items(), key=lambda x: x[1], reverse=True)
            with open('daily_sale.txt', 'w', encoding='utf-8') as wstream:
                str = '今日最畅销产品为:{},当日销售量为:{}'.format(dict_list[0][0], dict_list[0][1])
                wstream.write(str)
            print(str)
            print('信息已写入daily_sale.txt中,请打开查看!')
    
    def login():
        print("=" * 30 + '欢迎登陆XXX超市后台管理系统' + '=' * 30)
        while True:
            username = input('请输入用户名:')
            password = int(input('请输入登陆密码:'))
            if username == 'admin' and password == 111222:
                print('欢迎!请选择你要进行的操作!')
                info = '''
    ====================
       1.显示商品信息
       2.添加商品
       3.删除商品
       4.收银
       5.打印发票
       6.查看当日销售情况
       7.退出系统
    ====================
                '''
                print(info)
                while True:
                    key = int(input('请输入你要进行的操作:'))
                    if key == 1:
                        showGoods()
                    elif key == 2:
                        addGoods()
                    elif key == 3:
                        delGoods()
                    elif key == 4:
                        charge()
                    elif key == 5:
                        printInvoice()
                    elif key == 6:
                        viewSales()
                    elif key == 7:
                        print('正在退出系统...')
                        break
                    else:
                        print('输入错误!请重新选择!')
                time.sleep(2)
                print('退出系统成功!')
                break
    
            else:
                print('对不起,你的用户名或密码错误,请重新输入!')
                time.sleep(1)
    
    if __name__ == '__main__':
        login()
暂无评论~~
  • 请注意单词拼写,以及中英文排版,参考此页
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`, 更多语法请见这里 Markdown 语法
  • 支持表情,使用方法请见 Emoji 自动补全来咯,可用的 Emoji 请见 :metal: :point_right: Emoji 列表 :star: :sparkles:
  • 上传图片, 支持拖拽和剪切板黏贴上传, 格式限制 - jpg, png, gif
  • 发布框支持本地存储功能,会在内容变更时保存,「提交」按钮点击时清空
Ctrl+Enter