# 手机销售系统 # 手机品牌 手机价格 库存数量 # vivoX9 2798 25 # iphone7(32G) 4888 31 # iphone7(128G) 5668 22 # iphone7P(128G) 6616 29 # iphone6(16G) 3858 14 # .... # .... # .... # 功能要求: # 四个选项: # 1.查看所有手机品牌 # 1.vivoX9 # 2.iphone7(32G) # ...... # 分支选项: # 1.选择产品序号查看详情(根据序号输出产品名称,价格,库存) # 1.购买(库存数量-1,库存为0时,删除该产品) # 2.返回 # 2.返回 # 2.更改产品库存信息 # 1.添加新产品(添加新产品,包括产品名称、价格、库存) # 2.修改原有产品 # 输出所有产品信息(将产品的名称 价格 库存) # 1.根据选择序号进行修改 # 2.返回 # 3.移除产品库存信息 # 1.查看所有产品,根据序号移除 # 2.移除所有产品 # 3.返回 # 4.退出程序 import os def save(): file_handle = open('phone.txt', 'w') for phone in phone_list: s = ' '.join(phone) file_handle.write(s) file_handle.write('\n') def read(): rs = os.path.exists('phone.txt') if rs == True: file_handle = open('phone.txt', 'r') contents = file_handle.readlines() for x in contents: x = x.strip('\n') list = x.split(' ') phone_list.append(list) file_handle.close() def search(): if (len(phone_list) == 0): print(' (空) ') for put in range(0, len(phone_list)): search_list = phone_list[put] name = search_list[0] print('编号:%s,手机品牌:%s' % (put, name)) def search_all(): if (len(phone_list) == 0): print(' (列表为空,无法查询!) ') return for put in range(0, len(phone_list)): search_list = phone_list[put] name = search_list[0] price = search_list[1] stock = search_list[2] print('编号:%s,手机品牌:%s,价格:%s,库存:%s' % (put, name, price, stock)) def search_buy(): print('-----------------------------------------') put = int(input('请输入您要购买的产品编号:')) while put not in range(0, len(phone_list)): put = int(input('编号不存在,请重新输入:')) buy_list = phone_list[put] stock = buy_list[2] stock = int(stock) stock -= 1 buy_list[2] = stock phone_list[put] = buy_list print('-----------------------------------------') print('购买的编号为%s的产品成功!' % put) if stock == 0: del phone_list[put] save() def change(): if len(phone_list) == 0: print('-----------------------------------------') print('没有产品信息,无法修改!') search_all() print('-----------------------------------------') put = int(input('请输入您要修改产品的编号:')) while put not in range(0, len(phone_list)): put = int(input('您输入的编号不存在,请重新输入:')) old_list = phone_list[put] name = old_list[0] price = old_list[1] stock = old_list[2] new_name = input('请输入新的品牌名称(%s):' % name) new_price = input('请输入新的产品价格(%s):' % price) new_stock = input('请输入新的产品库存(%s):' % stock) phone_list[put] = [new_name, new_price, new_stock] print() save() def add(): print('-----------------------------------------') name = input('请输入您要添加的产品名称:') price = input('请输入该产品的价格:') stock = input('请输入该产品的库存:') # 将产品信息封装为一个小字典 # phone = {'name':name, 'price':price, 'stock':stock} # phone_list.append(phone) phone_list.append([name, price, stock]) print('添加产品成功!') save() def delete(): if len(phone_list) == 0: print('----------------------------') print('没有学员信息,无法删除!') return search_all() print('-----------------------------------------') put = int(input('请输入您要删除的产品编号:')) while put not in range(0, len(phone_list)): put = int(input('编号不存在,请重新输入:')) del phone_list[put] print('删除成功!') save() def delete_all(): phone_list.clear() print('*****************************************') print("****** 所有产品信息已清空! ******") save() phone_list = [] read() while True: print('-----------------------------------------') print('---------- 1.查看所有手机品牌 ----------') print('---------- 2.更改产品库存信息 ----------') print('---------- 3.移除产品库存信息 ----------') print('---------- 4.退出程序 ----------') print('-----------------------------------------') num = int(input('请输入您要进行的操作:')) while num not in range(1,5): num = int(input('编号不存在,请重新输入:')) if num == 1: search() print('-----------------------------------------') print('1.选择产品编号查看详情') print('2.返回') print('-----------------------------------------') num_1 = int(input('请输入您要进行的操作:')) while num_1 not in range(1, 3): num_1 = int(input('编号不存在,请重新输入:')) if num_1 == 1: put = int(input('请选择查看详情的产品编号:')) while put not in range(0, len(phone_list)): put = int(input('编号不存在,请重新输入:')) search_list = phone_list[put] name = search_list[0] price = search_list[1] stock = search_list[2] print('编号:%s,手机品牌:%s,价格:%s,库存:%s' % (put, name, price, stock)) print('-----------------------------------------') print('1.购买') print('2.返回') print('-----------------------------------------') num_2 = int(input('请输入您要进行的操作:')) while num_2 not in range(1, 3): num_2 = int(input('编号不存在,请重新输入:')) if num_2 == 1: search_buy() else: continue else: continue elif num == 2: print('-----------------------------------------') print('1.添加新产品') print('2.修改原有产品') print('-----------------------------------------') num_3 = int(input('请输入您要进行的操作:')) while num_3 not in range(1, 3): num_3 = int(input('编号不存在,请重新输入:')) if num_3 == 1: add() else: search_all() print('-----------------------------------------') print('1.根据选择序号进行修改') print('2.返回上一级') print('-----------------------------------------') num_4 = int(input('请输入您要进行的操作:')) while num_4 not in range(1, 3): num_4 = int(input('编号不存在,请重新输入:')) if num_4 == 1: change() else: continue elif num == 3: print('-----------------------------------------') print('1.根据编号删除产品') print('2.删除所有产品') print('3.返回') print('-----------------------------------------') num_5 = int(input('请输入您要进行的操作:')) while num_5 not in range(1, 4): num_5 = int(input('编号不存在,请重新输入:')) if num_5 == 1: delete() elif num_5 == 2: sure = input('确定删除所有产品信息?y(确定)/n(取消)') if sure == 'y': delete_all() elif sure == 'n': print('-----------------------------------------') print('清空操作已取消!') else: print('输入有误,请重新输入') else: continue else: print('谢谢使用,再见!') break
基于python的-手机销售系统
最新推荐文章于 2025-04-01 09:15:00 发布