ReadMe:

注意事项:
1.本程序需要提前安装prettytable模块,在商品展示时使用了prettytable
2.数据库使用json模块,有中文数据,在mac系统上编写,运行没有出现问题,在其他系统上运行时入出现编码问题请转换编码格式一些需求修改的说明:
1.Alex在布置这个商城作业时,选择物品即自动扣款,但在正常的电商购物流程,是需要先加入购物车结算时扣款,所以我使用了结算时扣款的流程,但充值的金额会模拟扣款
2.用户第二次登陆时会读取上次未结账的商品,自动加入购物车,并会提醒用户如结账后的预计金额文件说明:
user_info:json文件,存储相应的用户信息,包括账号,密码,账户余额以及购物历史记录
goods_info:json文件,存储商品的详情,包括名称,库存,价格等
save_cart:json文件,存储信息为:用户加入购物车并未结账退出时的购物车商品列表,以便第二次登陆时继续购物
user_lock:普通文件,保存已被锁定用户账号信息.功能测试账号:cc 密码:123   也可注册一个账号来测试功能介绍:
1.此程序为一个购物电商程序,在命令行运行
2.运行时需要登陆,如果密码三次错误,会将此用户锁定,以后不能再登陆
3.账号注册:运行程序后,输入一个数据库中没有的账号,会提示无此账号,是否注册新用户,当然注册时密码验证超过三次会注册失败
4.充值功能:用户在主菜单可选择充值功能,直接输入金额即可,充值完成后会提示账户余额
5.购买商品:购买商品需要先选择分类,然后再进入商品详情,详情包括商品名称,价格,库存,需要流程为商品分类--》商品详情--》商品编号--》商品数量--》商品购物车
6.商品库存:购买时入超过库存回提示购买失败,加入购物车时也会对库存做相应的修改
7.购物车:购物车可显示购买商品的详情,包括名称,商品数量以及总价,如果需要修改购物车,需要返回主菜单编辑购物车
8.编辑购物车:可编辑商品的购买数量,选择对应的编号,然后输入数量,如果想不想要商品,直接在选择数量时输入0即可
9.购物历史:主菜单可选择显示购物历史,会显示每个时间段的购物详情
10.最后的最后:输入无用的字符会提示输入错误并返回选择页面

流程图:

user_info:

{"123": ["123",0,[]],"shane": ["123",100003,[]],"zc": ["123",0,[]],"cc": ["123",1626,[["2016-05-19 14:23:37",[["小白鞋",1,900],["T恤",1,300]]],["2016-05-19 17:07:06",[["微波炉",1,800]]],["2016-05-19 17:56:19",[["Nudie牛仔裤",1,699],["洗衣机",1,999]]],["2016-05-20 10:07:34",[["IBM X240",1,6999]]]]],"chengc": ["123456",999999999,[]],"zhangxiaodong": ["zhangxiaodong",0,[]]
}

goods_info:

{"家电": {"微波炉": {"price": 800,"belong": "家电","stock": 2995},"热水器": {"price": 600,"belong": "家电","stock": 900},"洗衣机": {"price": 999,"belong": "家电","stock": 598},"彩电": {"price": 5000,"belong": "家电","stock": 399},"冰箱": {"price": 3000,"belong": "家电","stock": 300}},"服装": {"Nudie牛仔裤": {"price": 699,"belong": "服装","stock": 3978},"T恤": {"price": 300,"belong": "服装","stock": 892},"小白鞋": {"price": 900,"belong": "服装","stock": 5964},"驴牌腰带": {"price": 9999,"belong": "服装","stock": 2999}},"电脑": {"IBM X240": {"price": 6999,"belong": "电脑","stock": 3003},"Surface Book": {"price": 8999,"belong": "电脑","stock": 3020},"Mac pro": {"price": 9999,"belong": "电脑","stock": 3995},"Mac Air": {"price": 6999,"belong": "电脑","stock": 6002}},"汽车": {"PASST": {"price": 299999,"belong": "汽车","stock": 297},"BMW X3": {"price": 499999,"belong": "汽车","stock": 399},"Tesla Model S": {"price": 799999,"belong": "汽车","stock": 3000},"Porsche 911": {"price": 2999999,"belong": "汽车","stock": 594}},"手机": {"XiaoMi 4": {"price": 1999,"belong": "手机","stock": 5396},"华为Mate": {"price": 2999,"belong": "手机","stock": 5996},"Iphone": {"price": 5888,"belong": "手机","stock": 2998},"锤子2(这特么真是手机)": {"price": 998,"belong": "手机","stock": 2994}}
}

save_cart:

{"cc": [["锤子2(这特么真是手机)",998,"手机"]],"shane": [["微波炉",800,"家电"],["微波炉",800,"家电"],["洗衣机",999,"家电"]]
}

user_lock:

123

程序代码:

#!/usr/bin/env python
# -*-coding=utf-8-*-
# Auther:ccorz Mail:ccniubi@163.com Blog:http://www.cnblogs.com/ccorz/
# GitHub:https://github.com/ccorzorzimport json,prettytable,time,collections
user_info=json.load(open('user_info','r'))
goods=json.load(open('goods_info','r'))
save_cart=json.load(open('save_cart','r'))
time_now=time.strftime('%Y-%m-%d %H:%M:%S')def refresh_goods():json.dump(goods,open('goods_info','w'),ensure_ascii=False,indent=1)def refresh_user():json.dump(user_info,open('user_info','w'),ensure_ascii=False,indent=1)def cache_cart(user_name):save_cart[user_name]=cart# print('save_cart[user_name]',save_cart[user_name])json.dump(save_cart,open('save_cart','w'),ensure_ascii=False,indent=1)def regis():exit_flag=0while exit_flag==0:user_name=input('请输入您的用户名:')if user_name in user_info.keys():print('此用户已被注册,请重新输入.')else:user_pwd=input('请输入您的密码:')for i in range(3):user_pwd_again=input('请再次确认您的密码:')if user_pwd_again==user_pwd:user_info[user_name]=[user_pwd_again,0,[]]refresh_user()print('用户名%s已注册成功,请登录购买商品...'%user_name)exit_flag=1breakelif i==2:print('您输入的密码次数超过三次,注册关闭!')exit_flag=1else:print('您输入的密码和上次输入的密码不匹配,请重新输入,还有%s次机会.'%(2-i))
def refill(user_name):for i in range(3):amount=input('请输入您要充值的金额,请输入数字:')if amount.isdigit():user_info[user_name][1]+=int(amount)refresh_user()print('\033[32;1m土豪,请保养程序员!!!\033[0m充值成功,您的余额为\033[31;1m%s\033[0m'%user_info[user_name][1])balance=user_info[user_name][1]print(balance)breakelif i==2:exit('你在坑我么?告诉你要输入数字的,程序关闭...')else:print('您输入的不是数字,请重新输入..')def show_vcart(cart):# print(cart)if len(cart)>0:v_cart = collections.Counter(cart)dv_cart=dict(v_cart)row=prettytable.PrettyTable()row.field_names=['序列号','商品名称','商品数量','商品总价']for i in enumerate(dv_cart):index=i[0]item=i[1][0]totle_price = i[1][1] * dv_cart[i[1]]item_amount = dv_cart[i[1]]row.add_row([index,item,item_amount,totle_price])print(row)else:print('\033[31;1m购物车为空\033[0m'.center(50,'*'))time.sleep(1)def check_cart():if len(cart)>0:v_cart=collections.Counter(cart)dv_cart=dict(v_cart)ddv_cart=[]for i in enumerate(dv_cart):index=i[0]item=i[1][0]totle_price = i[1][1] * dv_cart[i[1]]item_amount = dv_cart[i[1]]ddv_cart.append([item,item_amount,totle_price])user_info[user_name][2].append([time_now,ddv_cart])user_info[user_name][1]=balancerefresh_user()refresh_goods()cart.clear()# print('cart:',cart)
        cache_cart(user_name)print('\033[31;1m结账成功,波多野结衣即将为您送货,请准备收货...\033[0m')else:print('\033[31;1m购物车是空的...\033[0m')cache_cart(user_name)time.sleep(1)def show_his(user_name):user_info=json.load(open('user_info','r'))his_list=user_info[user_name][2]if len(his_list)==0:print('无购物历史...')else:for his in his_list:dt=his[0]print('\033[31;1m购物时间:%s\033[0m'.center(50,'*')%dt)row=prettytable.PrettyTable()row.field_names=['商品名称','数量','总额']for item in his[1]:p_name=item[0]p_amount=item[1]p_totle=item[2]row.add_row([p_name,p_amount,p_totle])print(row)def edit_cart(user_name):e_cart=list(set(cart))e_vcart=collections.Counter(cart)e_vcart=dict(e_vcart)# print('e_vcart',e_vcart)
    e_cart.sort()# print('e_cart',e_cart)row=prettytable.PrettyTable()row.field_names=['商品序列号','商品名称','商品数量','总价']for i in enumerate(e_cart):index=i[0]p_name=i[1][0]p_price=i[1][1]p_amount=e_vcart[i[1]]p_totle=i[1][1]*e_vcart[i[1]]p_belong=i[1][2]# print(index,p_name,p_price,p_amount,p_totle,p_belong)
        row.add_row([index,p_name,p_amount,p_totle])print(row)while True:choice_num=input('请输入要编辑的商品序列号,输入q或quit为退出编辑购物车:')if choice_num.isdigit() and  int(choice_num)<len(e_cart):choice_num=int(choice_num)goods_stock=goods[e_cart[choice_num][2]][e_cart[choice_num][0]]['stock']p_amount=e_vcart[e_cart[choice_num]]balance=user_info[user_name][1]print(goods_stock)while True:choice_num_d=input('输入要购买的商品数量:')if choice_num_d.isdigit():choice_num_d=int(choice_num_d)if choice_num_d<=goods_stock:if choice_num_d==p_amount:print('修改商品数量成功')breakelif choice_num_d>p_amount:d_price=int(choice_num_d-p_amount)*int(e_vcart[e_cart[choice_num]])if balance>=d_price:for i in range(choice_num_d-p_amount):cart.append(e_cart[choice_num])balance-=d_pricegoods_stock+=p_amountgoods_stock-=choice_num_delse:print('余额不足,修改失败,请充值!')breakelse:d_price=int(abs(choice_num_d-p_amount))*(e_vcart[e_cart[choice_num]])for i in range(abs(choice_num_d-p_amount)):cart.remove(e_cart[choice_num])balance+=d_pricegoods_stock+=p_amountgoods_stock-=choice_num_dprint('修改成功.')breakelse:print('输入数量有误,请合适商品的库存...')breakelse:print('输入类型有误请重新输入...')breakelif choice_num == 'q' or choice_num == 'quit':print('退出编辑购物车...')breakelse:print('输入有误,请重新输入...')break_flag=0
user_lock=open('user_lock','r+')
locker=user_lock.readlines()user_name=input('请输入\033[31;1m壕\033[0m的用户名:')
if  user_name in locker:exit('用户已被锁定')
if user_name in json.load(open('user_info','r')).keys() and user_info not in locker:for i in range(3):if break_flag==1:breakelse:pwd=input('请输入%s的密码:'%user_name)if pwd==user_info[user_name][0]:if save_cart.get(user_name)==None:cart=[]else:cart=save_cart[user_name]for i in range(len(cart)):cart[i]=tuple(cart[i])# print('转换后cart:',cart)print('登陆成功...')print('欢迎来到大牛逼商城,走过路过,不要错过...'.center(50,'*'))while break_flag==0:if save_cart.get(user_name)==None or len(save_cart[user_name])<=0:balance = user_info[user_name][1]print('壕,您的账户余额为:\033[31;1m%s\033[0m,\033[32;1m(钱不够?账户充值请输入r,回车继续购物)\033[0m:'%balance)else:cart_price_list=[]for i in cart:cart_price_list.append(i[1])balance=user_info[user_name][1]-sum(cart_price_list)print('\033[31;1m您的购物车中还有您上次购物时未结算的商品,如减去购物车中的商品总价,您的余额为\033[0m\033[31;1m%s\033[0m'%balance)time.sleep(1)mrow=prettytable.PrettyTable()mrow.field_names=['功能','购物','查看购物车','查看购物历史','余额充值','退出购物商城','确认购买','编辑购物车']mrow.add_row(['快捷键','回车','S或showcart','H或history','R或refill','Q或quit','C或check','E或者edit'])print(mrow)menu=input('''\033[32;1m选择菜单:\033[0m''')if menu.lower()=='r':refill(user_name)elif menu.lower()=='h' or menu.lower()=='history':time.sleep(1)show_his(user_name)time.sleep(1)elif menu.lower()=='s' or menu.lower()=='showcart' :time.sleep(1)show_vcart(cart)elif menu.lower()=='c' or menu.lower()=='check':check_cart()elif menu.lower()=='q' or menu.lower()=='quit':break_flag=1cache_cart(user_name)elif menu.lower()=='e' or menu.lower()=='edit':edit_cart(user_name)elif len(menu)==0:while break_flag==0:print('壕,您的扣除购物车中的钱款预计账户余额为:\033[31;1m%s\033[0m:'%balance)print('请选择商品的类型编号'.center(50,'='))# print(list(goods.keys()))cla_list=list(goods.keys())for i in cla_list:print(cla_list.index(i),i)choice_cla_num=input('''\033[32;1m请选择您要购买物品类型所对应的序列号(返回主菜单输入'b'或'back',查看购物车输入s,确认付款输入c,退出输入q或quit):\033[0m''')if choice_cla_num.isdigit() and int(choice_cla_num)<len(cla_list):choice_cla_num=int(choice_cla_num)cla=cla_list[choice_cla_num]goods_list=list(goods[cla])while break_flag==0:if len(cart)==0:print('壕,您的目前账户余额为:\033[31;1m%s\033[0m'%balance)else:print('壕,您购买购物车中商品后,预计账户余额为:\033[31;1m%s\033[0m'%balance)row=prettytable.PrettyTable()row.field_names=['序列号','商品名称','商品价格','商品库存']for p in goods_list:p_num=goods_list.index(p)p_name=pp_price=goods[cla][p]['price']p_stock=goods[cla][p]['stock']row.add_row([p_num,p_name,p_price,p_stock])print(row)choice_p_num=input('\033[32;1m输入您要购买的商品序列号,返回商品分类请输入b或back,查看购物车输入s,确认付款输入c,退出系统输入q或quit:\033[0m')if choice_p_num.isdigit() and int(choice_p_num)<len(goods_list):p_name=goods_list[int(choice_p_num)]p_price=goods[cla][p_name]['price']p_stock=goods[cla][p_name]['stock']p_belong=goods[cla][p_name]['belong']while break_flag==0:p_count=input('\033[32;1m输入您要购买的商品数量,直接回车系统默认数量默1:\033[0m')if len(p_count)==0:p_count=1elif p_count.isdigit():if int(p_count) <= p_stock:p_count = int(p_count)else:print('库存数量有限,最大购买数量为%s' % p_stock)breakif balance >= p_count*p_price:p_stock-=p_countgoods[cla][p_name]['stock']=p_stockfor i in range(p_count):cart.append((p_name,p_price,p_belong))# print(cart)print('商品\033[32;1m%s\033[0m已加入购物车'%p_name)v_cart=collections.Counter(cart)print('\033[31;1m未付款商品\033[0m'.center(50,'*'))show_vcart(v_cart)balance-=p_count*p_pricebreakelse:print('您的预计余额已不足购买\033[31;1m%s件%s\033[0m,请重新确认商品价格以及数量,也可返回主菜单编辑购物车.'%(p_count,p_name))print('\033[31;1m您可返回主菜单进入充值系统充值,8亿10亿不是事儿...\033[0m')time.sleep(1)breakelif choice_p_num.lower()=='b' or choice_p_num.lower=='back':breakelif choice_p_num.lower()=='s':show_vcart(cart)elif choice_p_num.lower()=='c' or choice_p_num.lower()=='check':check_cart()elif choice_p_num.lower()=='q' or choice_p_num.lower()=='quit':break_flag=1cache_cart(user_name)else:print('输入类型错误,请重新输入')time.sleep(1)elif choice_cla_num.lower()=='s'or choice_cla_num.lower()=='showcart':show_vcart(cart)elif choice_cla_num.lower()=='c' or choice_cla_num.lower()=='check':check_cart()elif choice_cla_num.lower()=='b'or choice_cla_num.lower()=='back':breakelif choice_cla_num.lower()=='q' or choice_cla_num.lower()=='quit':break_flag=1cache_cart(user_name)elif choice_cla_num.lower()=='e' or choice_cla_num.lower()=='edit':edit_cart(user_name)else:print('输入有误,请重新输入')elif i==2:user_lock.write('\n%s'%user_name)user_lock.close()exit('三次密码错误,账户已被锁定')else:print('密码错误,请重新输入...')
else:y_or_n=input('没有此用户名,需要注册才能进入商城!!!是否要注册?\033[31;1m输入y或者回车为注册,n或者q退出\033[0m:')if len(y_or_n)==0 or y_or_n=='y':regis()elif y_or_n=='n' or y_or_n=='q':exit('程序退出...')else:exit('输入错误,程序退出...')

以下为运行截图:

开始运行:

购物历史记录:

选择商品分类:

商品明细:

加入购物车:

购买时余额不足:

查看购物车:

编辑购物车:

确认付款:

Day2作业:购物商城相关推荐

  1. 微信小程序期末作业-购物商城

    微信小程序期末作业-购物商城 小程序有搜索栏,底部导航栏.轮播图,各种商品标签,有首页,分类,购物车,个人中心四个底部导航,导入即可运行,适合初学者学习使用,项目详情请看下图:(资源链接在文章末尾) ...

  2. day2编写购物商城(1)

    作业:购物商城 商品展示,价格 买,加入购物车 付款,钱不够     具体实现了如下功能: 1.可购买的商品信息显示 2.显示购物车内的商品信息.数量.总金额 3.购物车内的商品数量进行增加.减少和商 ...

  3. 安卓期末大作业——购物商城(源码+18页报告)

    Android系统原理及应用报告 题    目:                         学    号: 班    级: 姓    名: 完成时间   报告要求须知 项目报告按照实践开发实际情 ...

  4. Python学习day5作业-ATM和购物商城

    Python学习day5作业 Python学习day5作业 ATM和购物商城 作业需求 ATM: 指定最大透支额度 可取款 定期还款(每月指定日期还款,如15号) 可存款 定期出账单 支持多用户登陆, ...

  5. python atm作业详解_Python学习day5作业-ATM和购物商城

    Python学习day5作业 Python学习day5作业 ATM和购物商城 作业需求 ATM:指定最大透支额度 可取款 定期还款(每月指定日期还款,如15号) 可存款 定期出账单 支持多用户登陆,用 ...

  6. 静态HTML网页设计作品 仿唯品会购物商城(5页) HTML+CSS+JavaScript 学生DW网页设计作业成品 商城网站设计

    HTML5期末大作业:仿唯品会购物网站设计--仿唯品会购物商城(5页) HTML+CSS+JavaScript 学生DW网页设计作业成品 商城网站设计 常见网页设计作业题材有 个人. 美食. 公司. ...

  7. HTML5期末大作业:商城网站设计——仿唯品会购物商城(5页) 纯手写-高质量 HTML+CSS+JavaScript

    HTML5期末大作业:商城购物网站设计--仿唯品会商城(5页) 纯手写 HTML+CSS+JavaScript 期末作业HTML代码 学生网页课程设计期末作业下载 web网页设计制作成品 常见网页设计 ...

  8. HTML5期末大作业:仿唯品会购物网站设计——仿唯品会购物商城(5页) HTML+CSS+JavaScript 学生DW网页设计作业成品 商城网站设计

    HTML5期末大作业:仿唯品会购物网站设计--仿唯品会购物商城(5页) HTML+CSS+JavaScript 学生DW网页设计作业成品 商城网站设计 常见网页设计作业题材有 个人. 美食. 公司. ...

  9. HTML5期末大作业:时尚服装购物网站设计——时尚服装购物商城(16页) 关于时尚购物HTML网页设计-----服装

    HTML5期末大作业:时尚服装购物网站设计--时尚服装购物商城(16页) 关于时尚购物HTML网页设计-----服装 常见网页设计作业题材有 个人. 美食. 公司. 学校. 旅游. 电商. 宠物. 电 ...

最新文章

  1. C# http 性能优化500毫秒到 60 毫秒
  2. 什么?你还在使用fastjson,性能太差了
  3. 【Boost】boost库asio详解2——io_service::run函数无任务时退出的问题
  4. [总结篇3]Provider_network的含义
  5. hibernate详解
  6. 【Android 逆向】IDA 工具使用 ( 重命名函数 | 添加注释 | 添加标签 / 跳转标签 | 代码跳转前进 / 后退 )
  7. VS2015下OpenGL库的配置
  8. django-oscar的订单状态总是Pending
  9. 结对-贪吃蛇游戏-开发环境搭建过程
  10. 如何通过Rultor将Maven工件部署到CloudRepo
  11. django加载本地html
  12. mybatis源码学习篇之——执行流程分析
  13. scp命令不会复制隐藏文件
  14. python零基础能学吗-零基础小白多久能学会python
  15. eclp物流是什么意思_小票零担,大票零担及整车怎么选择物流运输方式?
  16. Datawhale团队第五期录取名单!
  17. java配置文件起名规范_Mybatis注解和配置文件命名规范所引发的问题
  18. linux fuse安装脚本,Linux FUSE(用户态文件系统)的使用:用libfuse创建FUSE文件系统...
  19. Redis高可用架构
  20. Win32 汇编 - 移位: SHL、SHR、SAL、SAR、ROL、ROR、RCL、RCR、SHLD、SHRD

热门文章

  1. Flutter:WebView和H5通信
  2. 安卓开发----错误汇总贴
  3. 2022-2028全球药品泡罩包装机行业调研及趋势分析报告
  4. html在电脑上转换字体怎么变了,电脑网页字体怎么变换大小
  5. [深度学习] OpenVINO开发:Win 10 安装及卸载
  6. Android面试题(25)-Bundle机制
  7. 人工智能 | ShowMeAI资讯日报 #2022.06.22
  8. 单目相机内参标定注意事项
  9. [记录] JavaScript 中的正则表达式
  10. 百果园“两驾马车、三根支柱”商业模式大揭秘