本文主要是介绍python控制台 在线商城管理系统,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
1.项目概述
1.1项目主要内容
1.2项目需求分析
2、公共类设计
3.有关接口
4.源代码
5.运行结果
1.项目概述
1.1项目主要内容
1、用户登录
1.1 商品浏览(所有和单独查询)
1.2 商品的购买(数量-1,加上一个时间)
1.3 用户订单查询(不可修改,可以删除)
2、管理员登录
2.1、商品添加
2.2 、商品的查询
2.3、商品的删除
2.4、商品的修改
2.5、可以查询订单(并修改)
1.2项目需求分析
- 实现管理员的登录与用户登录,注册。
- 能够及时对修改后的数据进行存储;
- 对数据中的商品信息处理
- 对商品信息的增加、删除、修改和查询;
- 对商品信息的存储;
2.公共类设计
类的层次采用如下结构,由于pyhton的性质,方法封装到每个类中。
3.有关接口
存储的文件名称,josn中不支持单引号字符串
goodstable_filename="./data/goodstable.json"
ordertable_filename="./data/ordertable.json"
Register_table_filename='./data/Register_table.json'
manager_table_filename='./data/manager_table.json'
宏 字典的键
difine_user_id="difine_user_id"
difine_ordertable="difine_ordertable"
difine_goods_id='difine_goods_id'
difine_goods_name='difine_goods_name'
difine_buytime='difine_buytime'
difine_goods_number='difine_goods_number'
difine_goods_price='difine_goods_price'
difine_user_password='difine_user_password'
difine_user_type='difine_user_type'
管理员初始id和密码: ID:1234 密码:1234
difine_manager_id="1234"
difine_manager_password="1234"
接口示例
1.商品
{'difine_goods_id': '1', 'difine_goods_name': '1', 'difine_goods_price': 1, 'difine_goods_number': 1}
2.订单
{'difine_user_id': '1001', 'difine_ordertable': "[{'difine_goods_id': '2', 'difine_goods_name': '2', 'difine_buytime': '2'}, {'difine_goods_id': '2', 'difine_goods_name': '2', 'difine_buytime': '2'}, {'difine_goods_id': '2', 'difine_goods_name': '2', 'difine_buytime': '2'}, {'difine_goods_id': '2', 'difine_goods_name': '2', 'difine_buytime': '2'}]"}
3.用户注册表
[{"difine_user_id": "1002", "difine_user_password": '1122', 'difine_user_type': 'user'}, {'difine_user_id': '1004', 'difine_user_password': '11', 'difine_user_type': 'user'}]
difine_user_type =字符型
管理员:manager
用户:user
difine_goods_number =整型
difine_goods_price =整型
4.源代码
'''
在线商城管理系统
1、用户登录
1.1 商品浏览(所有和单独查询)
1.2 商品的购买(数量-1,加上一个时间)
1.3 用户订单查询(不可修改,可以删除)
2、管理员登录
2.1、商品添加
2.2 、商品的查询
2.3、商品的删除
2.4、商品的修改
2.5、可以查询订单
最后退出
学号 + 姓名.zip ( 项目 和 报 告 :学号+姓名) 周三晚上演示
''''''
作者:MaofU
关于我:https://space.bilibili.com/500047662?spm_id_from=333.1007.0.0
日期:2022.6.21
'''import os
import datetime#存储的文件名称,josn中不支持单引号字符串
goodstable_filename="./data/goodstable.json"
ordertable_filename="./data/ordertable.json"
Register_table_filename='./data/Register_table.json'
manager_table_filename='./data/manager_table.json'#宏 字典的键
difine_user_id="difine_user_id"
difine_ordertable="difine_ordertable"
difine_goods_id='difine_goods_id'
difine_goods_name='difine_goods_name'
difine_buytime='difine_buytime'
difine_goods_number='difine_goods_number'
difine_goods_price='difine_goods_price'
difine_user_password='difine_user_password'
difine_user_type='difine_user_type'#管理员初始id和密码: ID:1234 密码:1234
difine_manager_id="1234"
difine_manager_password="1234"#接口示例
'''
1.商品
{'difine_goods_id': '1', 'difine_goods_name': '1', 'difine_goods_price': 1, 'difine_goods_number': 1}
2.订单
{'difine_user_id': '1001', 'difine_ordertable': "[{'difine_goods_id': '2', 'difine_goods_name': '2', 'difine_buytime': '2'}, {'difine_goods_id': '2', 'difine_goods_name': '2', 'difine_buytime': '2'}, {'difine_goods_id': '2', 'difine_goods_name': '2', 'difine_buytime': '2'}, {'difine_goods_id': '2', 'difine_goods_name': '2', 'difine_buytime': '2'}]"}
3.用户注册表
[{"difine_user_id": "1002", "difine_user_password": '1122', 'difine_user_type': 'user'}, {'difine_user_id': '1004', 'difine_user_password': '11', 'difine_user_type': 'user'}]difine_user_type =字符型
管理员:manager
用户:userdifine_goods_number =整型
difine_goods_price =整型
'''#文件初始化
def startfile():file_exist = os.path.exists("./data")if file_exist == False:os.mkdir("./data")if os.path.exists(ordertable_filename) == False:with open(ordertable_filename, 'w', encoding='UTF-8') as r_file:r_file.write('')if os.path.exists(goodstable_filename) == False:with open(goodstable_filename, 'w', encoding='UTF-8') as r_file:r_file.write('')if os.path.exists(Register_table_filename) == False:with open(Register_table_filename, 'w', encoding='UTF-8') as r_file:r_file.write('')if os.path.exists(manager_table_filename) == False:with open(manager_table_filename, 'w', encoding='UTF-8') as r_file:r_file.write(str([{difine_user_id:difine_manager_id,difine_user_password:difine_manager_password,difine_user_type:"manager"}]))# 启动程序
def main():while True:print('{:=^40}'.format("在线商城管理系统"))startfile()#用户或管理员try:choice = int(input("请选登录用户:(管理员输入1,普通用户输入2,用户注册输入3,退出程序输入0)"))except:print("输入非数字!")continueuse = user()man = manager()if choice == 1:man.main()elif choice == 2:use.main()elif choice==3:use.Register()elif choice==0:breakelse:print("输入数字错误")print("欢迎下次使用!")return 0#管理员类
class manager():def __init__(self):self.manage_id=str()#入口def main(self):if self.Login():self.choice()else:return# 菜单栏def menu(self):print('{:-^40}'.format("管理员功能菜单"))print('1、商品的添加')print('2、商品的删除')print('3、商品的修改')print('4、查询订单,修改订单 ')print('0、退出管理员系统')print('{:-^40}'.format(""))#功能选择def choice(self):while True:self.menu()cho=int(input("请选择功能(管理员):"))if cho==1:self.addgoods()elif cho==0:breakelif cho==2:self.delete_good()elif cho==3:self.modify()elif cho==4:self.Track_the_order()#商品添加def addgoods(self):goods_list = []while True:good_id = input('请输入商品ID(1001):')if not good_id:breakgood_name = input('请输入商品名称:')if not good_name:breaktry:good_price=int(input("请输入商品价格:"))good_number = int(input('请输入商品数量:'))except TypeError / ValueError:print('输入数据有误,请核对后重新输入!!!')continue# 将录入的数据保存到文件中去,大部分情况保存数据以json格式具多good = {difine_goods_id: good_id, difine_goods_name: good_name,difine_goods_price: good_price, difine_goods_number: good_number,}goods_list.append(good)answer = input('是否继续添加商品信息?Y/N:\n')if answer == 'Y' or answer == 'y':continueelse:break# 保存数据self.goodsadd_save(goods_list)print("商品信息录入成功!!!")def goodsadd_save(self,list):# 存数据,需要读文件try:txt = open(goodstable_filename, 'a', encoding='UTF-8')except:txt = open(goodstable_filename, 'w', encoding='UTF-8')for item in list:txt.write(str(item) + '\n')txt.close()#商品的删除def delete_good(self):while True:good_id = input('请输入想要删除的商品id:')if good_id != '':if os.path.exists(goodstable_filename):with open(goodstable_filename, 'r', encoding='UTF-8') as file:goodinfos = file.readlines()else:goodinfos = []# 进行删除flag = Falseif goodinfos:with open(goodstable_filename, 'w', encoding='UTF-8') as w_file:for item in goodinfos:info = dict(eval(item))if info[difine_goods_id] != good_id:w_file.write(str(info) + '\n')else:flag = Trueif flag:print(f'id为{good_id}的商品已被删除')else:print(f'id为{good_id}的商品不存在')else:print('无商品信息')break# 查询所有信息# show()answer = input('是否继续删除信息?Y/N:\n')if answer == 'Y' or answer == 'y':continueelse:break#商品的修改def modify(self):if os.path.exists(goodstable_filename):with open(goodstable_filename, 'r', encoding='UTF-8') as r_file:student_infos = r_file.readlines()else:return# show()good_id = input('请输入需要修改的商品ID:')with open(goodstable_filename, 'w', encoding='UTF-8') as w_file:a = 0for item in student_infos:info = dict(eval(item))if info[difine_goods_id] == good_id:print('找到指定编号的商品,可以进行修改!')while True:try:info[difine_goods_id]=input("请输入修改后商品ID:")info[difine_goods_name] = input('请输入修改后名称:')info[difine_goods_price] = int(input('请输入修改后商品价格:'))info[difine_goods_number] = int(input('请输入修改后商品数量:'))except ValueError / TypeError:print("修改的格式不正确,请重新修改")else:breakw_file.write(str(info) + '\n')print('修改成功!')a = 1else:# 记得其他未曾修改过的数据,要复写回去w_file.write(str(info) + '\n')if a != 1:print('未找到指定商品信息,或修改失败')answer = input('是否继续修改商品信息?Y/N:\n')if answer == 'Y' or answer == 'y':self.modify()else:return#4、查询订单,修改订单def Track_the_order(self):#bug# if os.path.exists(ordertable_filename):# with open(ordertable_filename, 'r', encoding='UTF-8') as r_file:# infolist = r_file.readlines()# else:# returnwhile True:if os.path.exists(ordertable_filename):with open(ordertable_filename, 'r', encoding='UTF-8') as r_file:infolist = r_file.readlines()else:return#先查询user_id=input('请输入查询的用户ID:')isfind_user=Falsefor item in infolist:item_dict=eval(item)if item_dict[difine_user_id]==user_id:isfind_user=Trueorder_list = list(eval(item_dict[difine_ordertable]))print("该用户的订单情况如下:")for i_dict in order_list:print('商品ID:{} 商品名称:{} 购买时间:{}'.format(i_dict[difine_goods_id],i_dict[difine_goods_name],i_dict[difine_buytime]),end='\n')break# 修改订单if isfind_user != True:print('未找到指定用户信息')else:cho = input('是否要修改该用户的订单信息?(y/n)')new_order_list=[]if cho == 'y' or cho == 'Y':# 更新订单信息for i_dict in order_list:is_delete=Falsegoodsid=input('更改订单中商品ID({}):'.format(i_dict[difine_goods_id]))goodsna = input('更改订单中商品名称({}):'.format(i_dict[difine_goods_name]))buytime = input('更改订单中购买时间({}):'.format(i_dict[difine_buytime]))if (not goodsid) or (not goodsna) or (not buytime):cho=int(input('你的输入中含有空内容,删除该条订单输入1,不更改订单输入2:'))if cho==1:is_delete=Trueelif cho==2:is_delete=Falsegoodsid=i_dict[difine_goods_id]goodsna=i_dict[difine_goods_name]buytime=i_dict[difine_buytime]else:is_delete=Falsegoodsid=i_dict[difine_goods_id]goodsna=i_dict[difine_goods_name]buytime=i_dict[difine_buytime]print('输入有误,不更改该订单')if is_delete==False:new_order_list.append({difine_goods_id:goodsid,difine_goods_name:goodsna,difine_buytime:buytime})print('{:-^40}'.format(''))#写入文件with open(ordertable_filename, 'w', encoding='UTF-8') as r_file:for item in infolist:item_dict = eval(item)if item_dict[difine_user_id] == user_id:item_dict[difine_ordertable]=str(new_order_list)r_file.write(str(item_dict)+"\n")else:r_file.write(str(item_dict) + "\n")print('修改成功')elif cho == 'n' or cho == 'N':print('不修改用户订单')else:print('输入有误')answer = input('是否继续查询,修改用户订单信息?Y/N:\n')if answer == 'Y' or answer == 'y':continueelse:return#管理员登录def Login(self):manager_id=input('请输入登录的管理员ID:')password=input('请输入登录的管理员密码:')# 读取文件with open(manager_table_filename, 'r', encoding='utf-8') as r_file:info = r_file.readline()if len(info) != 0:infolist = list(eval(info))for i_dict in infolist:if i_dict[difine_user_id]==manager_id and i_dict[difine_user_password]==password:print('登录成功')self.manage_id=manager_idreturn Trueprint('管理员id或者密码错误,请从新输入')return Falseelse:print('管理员账号信息表为空')return False#用户类
class user():def __init__(self):self.user_id=str()#入口def main(self):is_ok=self.Login()if is_ok:self.choice()else:return#菜单def menu(self):'''1.1 商品浏览(所有和单独查询)1.2 商品的购买(数量-1,加上一个时间)1.3 用户订单查询(不可修改,可以删除)'''print('{:-^40}'.format("用户功能菜单"))print('1、商品浏览(所有)')print('2、商品浏览(单独查询)')print('3、商品购买')print('4、用户订单查询')print('0、退出用户系统')print('{:-^40}'.format(""))#功能选择def choice(self):while True:self.menu()cho=int(input("请选择功能(用户):"))if cho==1:self.look_goods()elif cho==0:breakelif cho==2:self.find_goods()elif cho==3:self.buy_goods()elif cho==4:self.find_order()else:print("输入有误")#1、商品浏览(所有)def look_goods(self):goods_list = []if os.path.exists(goodstable_filename):with open(goodstable_filename, '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:print("商品ID:\t\t\t商品名称:\t\t\t商品价格:\t\t\t商品剩余数量:")for item in goods_list:print("{}\t\t\t\t{}\t\t\t\t{}\t\t\t\t{}".format(item[difine_goods_id],item[difine_goods_name],item[difine_goods_price],item[difine_goods_number]))else:print('无商品信息')else:print('暂未存储相关信息')#2、商品浏览(单独查询)def find_goods(self):while True:try:cho = int(input("选择查询方式(使用ID输入1,使用名称输入2):"))except:print("输入有误!")continueif cho==1:goods_id=input("请输入商品ID:")if os.path.exists(goodstable_filename):with open(goodstable_filename, 'r', encoding='UTF-8') as r_file:goods_infos = r_file.readlines()isfinded=Falsefor item in goods_infos:item_dict=eval(item)if item_dict[difine_goods_id]==goods_id:isfinded=Trueprint("商品ID:\t\t\t商品名称:\t\t\t商品价格:\t\t\t商品剩余数量:")print("{}\t\t\t\t{}\t\t\t\t{}\t\t\t\t{}".format(item_dict[difine_goods_id],item_dict[difine_goods_name],item_dict[difine_goods_price],item_dict[difine_goods_number]))if isfinded ==False:print('无相关信息')else:print('暂未存储相关信息')elif cho==2:goods_name = input("请输入商品名称:")if os.path.exists(goodstable_filename):with open(goodstable_filename, 'r', encoding='UTF-8') as r_file:goods_infos = r_file.readlines()isfinded = Falsefor item in goods_infos:item_dict = eval(item)if item_dict[difine_goods_name] == goods_name:isfinded = Trueprint("商品ID:\t\t\t商品名称:\t\t\t商品价格:\t\t\t商品剩余数量:")print("{}\t\t\t\t{}\t\t\t\t{}\t\t\t\t{}".format(item_dict[difine_goods_id],item_dict[difine_goods_name],item_dict[difine_goods_price],item_dict[difine_goods_number]))if isfinded == False:print('无相关信息')else:print("输入有误!")continue#是否继续查询cho=input("是否继续查询?(y/n)")if cho=="y" or cho=="Y":continueelse:break#3、商品购买#操作两张表,商品表(数量减1),订单表def buy_goods(self):while True:try:cho = int(input("选择购买方式(使用ID输入1,使用名称输入2):"))except:print("输入有误!")continueif cho==1:#查询goods_id=input("请输入商品ID:")if os.path.exists(goodstable_filename):with open(goodstable_filename, 'r', encoding='UTF-8') as r_file:goods_infos = r_file.readlines()isfinded=Falsefor item in goods_infos:item_dict=eval(item)if item_dict[difine_goods_id]==goods_id:isfinded=Trueprint("商品ID:\t\t\t商品名称:\t\t\t商品价格:\t\t\t商品剩余数量:")print("{}\t\t\t\t{}\t\t\t\t{}\t\t\t\t{}".format(item_dict[difine_goods_id],item_dict[difine_goods_name],item_dict[difine_goods_price],item_dict[difine_goods_number]))#购买cho=input("是否购买该商品?(y/n)")if cho=="n" or cho=="N":breakelse:# buytime=time.strftime('%Y-%m-%d %H:%M:%S',time.gmtime())buytime=datetime.datetime.now()buyinfo = {difine_goods_id: "",difine_goods_name: "", difine_buytime: buytime}#操作两张表,商品表(数量减1),订单表#1.商品表with open(goodstable_filename, 'w', encoding='UTF-8') as r_file:for item in goods_infos:item_dict = eval(item)if item_dict[difine_goods_id]==goods_id:buyinfo[difine_goods_id]=goods_idbuyinfo[difine_goods_name] = item_dict[difine_goods_name]item_dict[difine_goods_number]=int(item_dict[difine_goods_number])-1r_file.write(str(item_dict)+"\n")else:r_file.write(str(item_dict) + "\n")#2.订单表with open(ordertable_filename, 'r', encoding='UTF-8') as r_file:order_list=r_file.readlines()with open(ordertable_filename, 'w', encoding='UTF-8') as r_file:# 有内容if len(order_list) != 0:exist_user=Falsefor item in order_list:dict=eval(item)#是该用户if dict[difine_user_id]==self.user_id:exist_user=Trueorderlist=list(eval(dict[difine_ordertable]))orderlist.append(buyinfo)dict[difine_ordertable]=str(orderlist)r_file.write(str(dict) + "\n")else:r_file.write(str(dict)+"\n")#无该用户if exist_user==False:oed_list=[buyinfo]new_info={difine_user_id:self.user_id,difine_ordertable:str(oed_list)}r_file.write(str(new_info) + "\n")# 无内容else:oed_list = [buyinfo]new_info = {difine_user_id: self.user_id, difine_ordertable: str(oed_list)}r_file.write(str(new_info) + "\n")print("购买成功!")if isfinded ==False:print('无相关信息')else:print('暂未存储相关信息')elif cho==2:# 查询goods_name = input("请输入商品名称:")if os.path.exists(goodstable_filename):with open(goodstable_filename, 'r', encoding='UTF-8') as r_file:goods_infos = r_file.readlines()isfinded = Falsefor item in goods_infos:item_dict = eval(item)if item_dict[difine_goods_name] == goods_name:isfinded = Trueprint("商品ID:\t\t\t商品名称:\t\t\t商品价格:\t\t\t商品剩余数量:")print("{}\t\t\t\t{}\t\t\t\t{}\t\t\t\t{}".format(item_dict[difine_goods_id],item_dict[difine_goods_name],item_dict[difine_goods_price],item_dict[difine_goods_number]))# 购买cho = input("是否购买该商品?(y/n)")if cho == "n" or cho == "N":breakelse:# gmtime=time.gmtime()# buytime = time.strftime('%Y-%m-%d %H:%M:%S',gmtime )buytime = datetime.datetime.now()buyinfo = {}# 操作两张表,商品表(数量减1),订单表# 1.商品表with open(goodstable_filename, 'w', encoding='UTF-8') as r_file:for item in goods_infos:item_dict = eval(item)if item_dict[difine_goods_name] == goods_name:buyinfo = {difine_goods_id: item_dict[difine_goods_id],difine_goods_name: item_dict[difine_goods_name], difine_buytime: buytime}item_dict[difine_goods_number] = int(item_dict[difine_goods_number]) - 1r_file.write(str(item_dict) + "\n")else:r_file.write(str(item_dict) + "\n")# 2.订单表with open(ordertable_filename, 'r', encoding='UTF-8') as r_file:order_list = r_file.readlines()with open(ordertable_filename, 'w', encoding='UTF-8') as r_file:# 有内容if len(order_list) != 0:exist_user = Falsefor item in order_list:dict = eval(item)# 是该用户if dict[difine_user_id] == self.user_id:exist_user = Trueorderlist = list(eval(dict[difine_ordertable]))orderlist.append(buyinfo)dict[difine_ordertable] = str(orderlist)r_file.write(str(dict) + "\n")else:r_file.write(str(dict) + "\n")# 无该用户if exist_user == False:oed_list = [buyinfo]new_info = {difine_user_id: self.user_id,difine_ordertable: str(oed_list)}r_file.write(str(new_info) + "\n")# 无内容else:oed_list = [buyinfo]new_info = {difine_user_id: self.user_id,difine_ordertable: str(oed_list)}r_file.write(str(new_info) + "\n")print("购买成功!")if isfinded == False:print('无相关信息')else:print('暂未存储相关信息')else :print("输入有误")# 是否继续购买cho = input("是否继续购买?(y/n)")if cho == "y" or cho == "Y":continueelse:break#4、用户订单查询def find_order(self):with open(ordertable_filename,'r',encoding='utf-8') as r_file:infolist=r_file.readlines()is_exist=Falsefor item in infolist:item_dict=eval(item)if item_dict[difine_user_id]==self.user_id:is_exist=Trueorder_list = list(eval(item_dict[difine_ordertable]))print("你的订单情况如下:")for i_dict in order_list:print('商品ID:{} 商品名称:{} 购买时间:{}'.format(i_dict[difine_goods_id],i_dict[difine_goods_name],i_dict[difine_buytime]),end='\n')if is_exist==False:print('暂时没有你的订单')#用户注册def Register(self):while True:#读取文件infolist=[]with open(Register_table_filename,'r',encoding='utf-8') as r_file:info=r_file.readline()if len(info) !=0:infolist=list(eval(info))#user_id = input('输入用户id:')user_password = input('输入密码:')user_type='user'#is_exist=Falseif len(infolist)!=0:for item_dict in infolist:if item_dict[difine_user_id]==user_id:is_exist=Trueprint("用户id已存在,请重新输入")breakif is_exist==True:continue#infolist.append({difine_user_id:user_id,difine_user_password:user_password,difine_user_type:user_type})with open(Register_table_filename, 'w', encoding='utf-8') as r_file:r_file.write(str(infolist))print('注册成功')break#用户登录def Login(self):user_id=input('请输入登录的用户ID:')password=input('请输入登录的用户密码:')# 读取文件with open(Register_table_filename, 'r', encoding='utf-8') as r_file:info = r_file.readline()if len(info) != 0:infolist = list(eval(info))for i_dict in infolist:if i_dict[difine_user_id]==user_id and i_dict[difine_user_password]==password:print('登录成功')self.user_id=user_idreturn Trueprint('用户id或者密码错误,请从新输入')return Falseelse:print('用户账号信息表为空')return Falseif __name__ == '__main__':main()
5.运行结果
这篇关于python控制台 在线商城管理系统的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!