栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

商品管理系统

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

商品管理系统

import os

import time

import random

# 存储文件

filename1 = 'goods.txt'

filename2 = 'order.txt'

info1 = {}

info2 = {}

# 启动程序

def main():

    while True:

        menu()

        choice = int(input("请选择功能:"))

        if choice in [0, 1, 2]:

            if choice == 0:

                answer = input("是否确认退出系统? Y/N:n")

                if answer == 'Y' or answer == 'y':

                    print('欢迎使用')

                    break

                else:

                    continue

            elif choice == 1:

                user_land()

            elif choice == 2:

                manager_land()

# 菜单栏

def menu():

    print('======================在线商城管理系统===================')

    print('------------------------功能菜单------------------------')

    print('ttt 1、用户登录')

    print('ttt 2、管理员登陆')

    print('ttt 0、退出系统')

    print('------------------------------------------------------')

# 1、录入信息

#    1、有录入信息的字段  2、录入完成后将数据保存到字典里面(文件中)

def user_land():

    id = input('请输入账号:')

    s_code = input('请输入密码:')

    print("登陆成功!")

    user_main()

def manager_land():

    id = input('请输入账号:')

    s_code = input('请输入密码:')

    print("登陆成功!")

    manager_main()

def user_menu():

    print('========================用户页面=====================')

    print('------------------------功能菜单------------------------')

    print('ttt 1、商品浏览')

    print('ttt 2、用户订单查询')

    print('ttt 0、退出登录')

    print('------------------------------------------------------')

def manager_menu():

    print('========================用户页面=====================')

    print('------------------------功能菜单------------------------')

    print('ttt 1、商品添加')

    print('ttt 2、商品的查询')

    print('ttt 3、商品的删除')

    print('ttt 4、商品的修改')

    print('ttt 5、查询订单')

    print('ttt 0、退出登录')

    print('------------------------------------------------------')

def user_main():

    while True:

        user_menu()

        choice = int(input("请选择功能:"))

        if choice in [0, 1, 2]:

            if choice == 0:

                answer = input("是否确认退出登录? Y/N:n")

                if answer == 'Y' or answer == 'y':

                    print('欢迎使用')

                    return main()

                else:

                    continue

            elif choice == 1:

                browse()

            elif choice == 2:

                user_inquire()

def browse():

    while True:

        print("ttt 1.浏览商品")

        print("ttt 2.查询商品")

        print('ttt 0、退出系统')

        choice = int(input("请选择功能:"))

        if choice in [0, 1, 2]:

            if choice == 0:

                answer = input("是否确认退出系统? Y/N:n")

                if answer == 'Y' or answer == 'y':

                    print('欢迎使用')

                    break

                else:

                    continue

            goods_query = []

            if choice == 1:

                if os.path.exists(filename1):

                    with open(filename1, 'r', encoding='UTF-8') as r_file1:

                        # 文件中的所有信息

                        student_info1 = r_file1.readlines()

                        for item1 in student_info1:

                        # 将遍历出的每一组数据转换为字典格式

                            global info1

                            global info2

                            info1 = dict(eval(item1))

                            print("商品名称:{0:^6}t价格:{1:^8}t库存:{2:^8}".format(

                            info1['GOODS_NAME'],info1['PRICE'],info1['SURPLUS']))

            if choice == 2:

                goods_query = []

                while True:

                    if os.path.exists(filename1):

                        name = input('请输入你想购买的商品名称:')

                        # 查找信息

                        with open(filename1, 'r', encoding='UTF-8') as r_file:

                            # 文件中的所有信息

                            student_info1 = r_file.readlines()

                            for item in student_info1:

                                # 将遍历出的每一组数据转换为字典格式

                                info1 = dict(eval(item))

                                # id的判断 有误

                                if name != "":

                                    if name == info1['GOODS_NAME']:

                                        goods_query.append(info1)

                        # 显示查询出来的数据

                        show_goods(goods_query)

                        # 你想再次查询,清除原来的列表数据

                        goods_query.clear()

                        answer = input('是否购买此商品?Y/N:n')

                        if answer == 'Y' or answer == 'y':

                            purchase()

                        else:

                            break

                    else:

                        print('暂未保存任何信息')

                        return

def show_goods(lst):

    if len(lst) == 0:

        print("抱歉!暂无此商品")

        return

    # 定一个表格形式去展示数据 第一个标题,第二个是内容

    show_title = '{:^6}t{:^8}t{:^8}'

    print(show_title.format('商品名称', '价格', '库存'))

    # 定义显示的内容格式

    show_content = '{:^10}t{:^8}t{:^8}'

    for item in lst:

        print(show_content.format(

            item.get('GOODS_NAME'),

            item.get('PRICE'),

            item.get('SURPLUS'),

            int(item.get('PRICE')) + int(item.get('SURPLUS'))

        ))

def delete_order(goods_id):

    global info1

    global info2

    if goods_id != '':

        if os.path.exists(filename2):

            with open(filename2, 'r', encoding='UTF-8') as file:

                goods_info = file.readlines()

        else:

            goods_info = []

        # 进行删除

        flag = False

        if goods_info:

            with open(filename2, 'w', encoding='UTF-8') as w_file:

                for item in goods_info:

                    info = dict(eval(item))

                    if info['商品名称'] != goods_id:

                        w_file.write(str(info) + 'n')

                    else:

                        flag = True

                if flag:

                    print(f'{goods_id}已被删除')

                else:

                    print(f'{goods_id}订单不存在')

        else:

            print('无此订单')

def delete_goods():

    while True:

        global info1

        global info2

        goods_id = input('请输入想要删除的商品名称:')

        if goods_id != '':

            if os.path.exists(filename1):

                with open(filename1, 'r', encoding='UTF-8') as file:

                    goods_info = file.readlines()

            else:

                goods_info = []

            # 进行删除

            flag = False

            if goods_info:

                with open(filename1, 'w', encoding='UTF-8') as w_file:

                    for item in goods_info:

                        info = dict(eval(item))

                        if info['GOODS_NAME'] != goods_id:

                            w_file.write(str(info) + 'n')

                        else:

                            flag = True

                    if flag:

                        print(f'{goods_id}已被删除')

                    else:

                        print(f'{goods_id}商品不存在')

            else:

                print('无此商品')

                break

            # 查询所有信息

            # show()

        answer = input('是否继续删除商品?Y/N:n')

        if answer == 'Y' or answer == 'y':

            continue

        else:

            break


 

def purchase():

    while True:

        good_list = []

        number = int(input("请输入购买商品数量:"))

        global info1

        global info2

        s = int(info1['SURPLUS'])

        if number <= s:

            s = s - number

            if os.path.exists(filename1):

                with open(filename1, 'r', encoding='UTF-8') as r_file1:

                    # 文件中的所有信息

                    student_info1 = r_file1.readlines()

                    for item1 in student_info1:

                    # 将遍历出的每一组数据转换为字典格式

                        info1 = dict(eval(item1))

                        info1['SURPLUS'] = str(s)

                with open(filename1, 'w', encoding='UTF-8') as r_file1:

                    r_file1.write(str(info1) + 'n')

            add_order()

            if os.path.exists(filename2):

                with open(filename2, 'r', encoding='UTF-8') as r_file2:

                    # 文件中的所有信息

                    student_info2 = r_file2.readlines()

                    for item2 in student_info2:

                    # 将遍历出的每一组数据转换为字典格式

                        info2 = dict(eval(item2))

            print("订单编号:{0:^6}t商品名称:{1:^8}t价格:{2:^8}t下单时间:{3:^8}".format(

                info2['订单号'],info2['商品名称'],info1['PRICE'],info2['购买时间']))

            answer = input("购买成功,是否继续购买?Y/N:n")

            if os.path.exists(filename1):

                with open(filename1, 'r', encoding='UTF-8') as r_file1:

                    # 文件中的所有信息

                    student_info = r_file1.readlines()

                    for item1 in student_info:

                    # 将遍历出的每一组数据转换为字典格式

                        info1 = dict(eval(item1))

                        print("商品名称:{0:^6}t价格:{1:^8}t库存:{2:^8}".format(

                        info1['GOODS_NAME'],info1['PRICE'],info1['SURPLUS']))

            if answer == 'Y' or answer == 'y':

                purchase()

            else:

                return user_main()

        else:

            print("库存不足!!!")

            return

def user_inquire():

    global info1, info2

    if os.path.exists(filename2):

        with open(filename2, 'r', encoding='UTF-8') as r_file2:

    # 文件中的所有信息

            student_info2 = r_file2.readlines()

            for item2 in student_info2:

            # 将遍历出的每一组数据转换为字典格式

                info2 = dict(eval(item2))

                print("订单编号:{0:^6}t商品名称:{1:^8}t价格:{2:^8}t下单时间:{3:^8}".format(

                info2['订单号'],info2['商品名称'],info1['PRICE'],info2['购买时间']))

            answer = input("是否删除订单?Y/N:n")

            if answer == 'Y' or answer == 'y':

                name = input("请输入想删除的订单:")

                delete_order(name)

def manager_main():

    while True:

        manager_menu()

        choice = int(input("请选择功能:"))

        if choice in [0, 1, 2, 3, 4, 5]:

            if choice == 0:

                answer = input("是否确认退出登录? Y/N:n")

                if answer == 'Y' or answer == 'y':

                    print('欢迎使用')

                    break

                else:

                    continue

            elif choice == 1:

                add_goods()

            elif choice == 2:

                goods_inquire()

            elif choice == 3:

                delete_goods()

            elif choice == 4:

                modify()

            elif choice == 5:

                manger_inquire()

def add_goods():

    good_list = []

    while True:

        id = input('请输入商品名称:')

        if not id:

            break

        try:

            price = input('请输入商品价格:')

            surplus = int(input("请输入商品数量:"))

        except (ValueError, TypeError):

            print('输入数据有误,请核对后重新输入!!!')

            continue

        # 将录入的数据保存到文件中去,大部分情况保存数据以json格式具多

        good = {'GOODS_NAME': id, 'PRICE': price, 'SURPLUS': surplus}

        good_list.append(good)

        answer = input('是否继续添加商品?Y/N:n')

        if answer == 'Y' or answer == 'y':

            continue

        else:

            break

    # 保存数据

    save(good_list)

    print("商品上架成功!!!")

def add_order():

    order_list = []

    id = random.randint(1, 9999)

    ti = time.ctime()

    order = {"订单号":id,"商品名称": info1['GOODS_NAME'],"价格":info1['PRICE'],"购买时间":ti}

    order_list.append(order)

    save2(order_list)

# 保存信息

def save(lst):

    # 存数据,需要读文件

    try:

        stu_txt = open(filename1, 'a', encoding='UTF-8')

    except:

        stu_txt = open(filename1, 'w', encoding='UTF-8')

    for item in lst:

        stu_txt.write(str(item) + 'n')

    stu_txt.close()

def save2(lst):

    try:

        stu_txt = open(filename2, 'a', encoding='UTF-8')

    except:

        stu_txt = open(filename2, 'w', encoding='UTF-8')

    for item in lst:

        stu_txt.write(str(item) + 'n')

    stu_txt.close()

def save3(lst):#要修改

    try:

        stu_txt = open(filename1, 'a', encoding='UTF-8')

    except:

        stu_txt = open(filename1, 'w', encoding='UTF-8')

        stu_txt.seek(0)

    for item in lst:

       

        stu_txt.write(str(item) + 'n')

    stu_txt.close()

def goods_inquire():

    goods_query = []

    global info1, info2

    while True:

        if os.path.exists(filename1):

            name = input('请输入你想查找的商品名称:')#查找单个,1.1

            # 查找信息

            with open(filename1, 'r', encoding='UTF-8') as r_file:

                # 文件中的所有信息

                goods_info = r_file.readlines()

                for item in goods_info:

                    # 将遍历出的每一组数据转换为字典格式

                    info1 = dict(eval(item))

                    # id的判断 有误

                    if name != "":

                        if name == info1['GOODS_NAME']:

                            goods_query.append(info1)

            # 显示查询出来的数据

            show_goods(goods_query)

            # 你想再次查询,清除原来的列表数据

            goods_query.clear()

            answer = input('是否继续查询?Y/N:n')

            if answer == 'Y' or answer == 'y':

                continue

            else:

                break

        else:

            print('暂未保存任何信息')

            return

def show_goods(lst):

    if len(lst) == 0:

        print("抱歉!暂无此商品")

        return

    # 定一个表格形式去展示数据 第一个标题,第二个是内容

    show_title = '{:^6}t{:^8}t{:^8}'

    print(show_title.format('商品名称', '价格', '库存'))

    # 定义显示的内容格式

    show_content = '{:^10}t{:^8}t{:^8}'

    for item in lst:

        print(show_content.format(

            item.get('GOODS_NAME'),

            item.get('PRICE'),

            item.get('SURPLUS'),

            int(item.get('PRICE')) + int(item.get('SURPLUS'))

        ))

def modify():

    global info1, info2

    if os.path.exists(filename1):

        with open(filename1, 'r', encoding='UTF-8') as r_file:

            goods_infos = r_file.readlines()

    else:

        return

    show()

    goods_id = input('请输入需要修改的商品名称:')

    with open(filename1, 'w', encoding='UTF-8') as w_file:

        a = 0

        for item in goods_infos:

            info = dict(eval(item))

            if info1['GOODS_NAME'] == goods_id:

                print('找到指定的商品,可以进行修改!')

                while True:

                    try:

                        info1['GOODS_NAME'] = input('请输入商品名称:')

                        info1['PRICE'] = int(input('请输入商品价格:'))

                        info1['SURPLUS'] = int(input('请输入商品数量:'))

                    except (ValueError, TypeError):

                        print("修改的格式不正确,请重新修改")

                    else:

                        break

                w_file.write(str(info1) + 'n')

                print('修改成功!')

                # 将修改好的成绩存入文件

                # student = {'ID': id, 'NAME': name, 'Python': python, 'C语言': clang, 'English': english}

                # student_infos.append(student)

                # print('修改成功')

                a = 1

            else:

                # 记得其他未曾修改过的数据,要复写回去

                w_file.write(str(info1) + 'n')

        if a != 1:

            print('未找到指定学生信息,或修改失败')

    answer = input('是否继续修改学生信息?Y/N:n')

    if answer == 'Y' or answer == 'y':

        modify()

    else:

        return

def show():

    goods_list = []

    if os.path.exists(filename1):

        with open(filename1, 'r', encoding='UTF-8') as r_file:

            goods_infos = r_file.readlines()

            for item in goods_infos:

                goods_list.append(eval(item))

            if goods_list:

                show_goods(goods_list)

            else:

                print('无商品信息')

    else:

        print('暂未存储相关信息')

def manger_inquire():

    global info1, info2

    if os.path.exists(filename2):

        with open(filename2, 'r', encoding='UTF-8') as r_file2:

    # 文件中的所有信息

            student_info2 = r_file2.readlines()

            for item2 in student_info2:

            # 将遍历出的每一组数据转换为字典格式

                info2 = dict(eval(item2))

                print("订单编号:{0:^6}t商品名称:{1:^8}t价格:{2:^8}t下单时间:{3:^8}".format(

                info2['订单号'],info2['商品名称'],info1['PRICE'],info2['购买时间']))

if __name__ == '__main__':

    info1 = {}

    info2 = {}

    if os.path.exists(filename1):

        with open(filename1, 'r', encoding='UTF-8') as r_file1:

            # 文件中的所有信息

            student_info1 = r_file1.readlines()

            for item1 in student_info1:

            # 将遍历出的每一组数据转换为字典格式

                info1 = dict(eval(item1))

    if os.path.exists(filename2):

        with open(filename2, 'r', encoding='UTF-8') as r_file2:

            # 文件中的所有信息

            student_info2 = r_file2.readlines()

            for item2 in student_info2:

            # 将遍历出的每一组数据转换为字典格式

                info2 = dict(eval(item2))

    main()

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/989880.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号