Day2作业:购物商城

Posted ccorz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Day2作业:购物商城相关的知识,希望对你有一定的参考价值。

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/ccorzorz

import 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=0
    while 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=1
                    break
                elif i==2:
                    print(\'您输入的密码次数超过三次,注册关闭!\')
                    exit_flag=1
                else:
                    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)
            break
        elif 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]=balance
        refresh_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(\'修改商品数量成功\')
                            break
                        elif 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_price
                                goods_stock+=p_amount
                                goods_stock-=choice_num_d
                            else:
                                print(\'余额不足,修改失败,请充值!\')
                                break
                        else:
                            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_price
                            goods_stock+=p_amount
                            goods_stock-=choice_num_d
                            print(\'修改成功.\')
                            break
                    else:
                        print(\'输入数量有误,请合适商品的库存...\')
                        break
                else:
                    print(\'输入类型有误请重新输入...\')
                    break
        elif choice_num == \'q\' or choice_num == \'quit\':
            print(\'退出编辑购物车...\')
            break
        else:
            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:
            break
        else:
            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]-python day2  购物车作业

day2购物商城

day2_作业1(购物车)

Python学习day2作业总结

Python3.5 Day2作业:购物车程序

老男孩Day2作业:购物车程序