码迷,mamicode.com
首页 > 编程语言 > 详细

python 改良购物车

时间:2018-06-07 21:48:31      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:模式   用户登录   lse   退出   UI   png   return   []   abs   

Python购物车


一、流程图

技术分享图片

 

二、目录结构

技术分享图片

 

三、代码

1、bin目录:

技术分享图片
1 import sys,os,json
2 dir_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
3 sys.path.append(dir_path)
4 from core import shopping
5 
6 if __name__ == "__main__":
7     shopping.main()
main.py

2、conf目录:

未编写

3、core目录:

技术分享图片
import json,sys,os
dir_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(dir_path)
user_path = dir_path + "/db/user_data.json"
user_infor_path = dir_path + "/db/user_infor.json"
with open(user_path, "r", encoding="UTF-8") as f_load:
    user_list = json.load(f_load)
with open(user_infor_path, "r", encoding="UTF-8") as f_load:
    user_infor_list = json.load(f_load)

def login_check():
    name = input("input your user_name:")
    passwd = input("input your user_passwd:")
    if name in user_list and passwd == user_list[name]:
        print("登陆成功!")
        return name
    else:
        print("wrong uesr_name or password...")
        exit()
def creat_user():
    new_user = input("input user_name:")
    if new_user in user_list:
        print("用户已存在")
        return
    else:
        new_pswd = input("input user_pswd:")
    user_list[new_user] = new_pswd
    with open(user_path, "w", encoding="UTF-8") as f_dump:
        json.dump(user_list, f_dump)
    user_infor_list[new_user] = {"money": 1000, "user_goods": []}
    with open(user_infor_path, "w", encoding="UTF-8") as f_dump:
        json.dump(user_infor_list, f_dump)
    print("用户创建成功!")
login.py
技术分享图片
  1 import sys,os,json
  2 dir_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  3 sys.path.append(dir_path)
  4 from core import login
  5 user_path = dir_path + "/db/user_infor.json"
  6 goods_path = dir_path + "/db/goods.json"
  7 
  8 def buy(who):
  9     with open(user_path, "r", encoding="UTF-8") as f_load:
 10         user_infor = json.load(f_load)
 11     user_goods_1 = user_infor[who]
 12     user_goods = user_goods_1["user_goods"]
 13     while True:
 14         print("--商品列表--")
 15         shop_list = goods_list()
 16         for i in shop_list:
 17             print(i,shop_list[i])
 18         print("------------")
 19         print("账户余额:",user_goods_1["money"])
 20         buy_sonething = input("选择购买的商品,按q退出!->")
 21         if buy_sonething in shop_list:
 22             user_money = user_goods_1["money"]
 23             cost_money = shop_list[buy_sonething]
 24             now_money = user_money - cost_money
 25             if int(now_money) >= 0 :
 26                 print("余额",now_money)
 27                 user_goods.append([buy_sonething,shop_list[buy_sonething]])
 28                 user_goods_1["user_goods"] = user_goods
 29                 user_goods_1["money"] = now_money
 30                 user_infor[who] = user_goods_1
 31                 with open(user_path, "w", encoding="UTF-8") as f_dump:
 32                     json.dump(user_infor, f_dump)
 33                 g = input("按任意键继续,按q退出")
 34                 if g == q:
 35                     print("购物清单:")
 36                     for i in user_goods:
 37                         print(i)
 38                     return now_money
 39                 else:
 40                     continue
 41             else:
 42                 print("余额不足!您的账户余额为:%s"%user_money)
 43                 exit()
 44         elif buy_sonething == "q":
 45             exit()
 46         else:
 47             print("请输入正确的商品名称!")
 48 
 49 def goods_list():
 50     with open(goods_path,"r", encoding="utf-8") as f:
 51         shop_list = json.load(f)
 52         return shop_list
 53 
 54 def type_choice():
 55     choice = input("请选择登陆模式(1.客户/2.商家):")
 56     if choice == "1":
 57         print("顾客模式:")
 58         return 1
 59     elif choice == "2":
 60         print("商家模式:")
 61         return 2
 62     else:
 63         print("输入错误!")
 64         return 0
 65 
 66 def customer_type():
 67     web = ‘‘‘
 68 "-------------"
 69 "用户登录按:1"
 70 "用户注册按:2"
 71 "-------------"
 72 ‘‘‘
 73     while True:
 74         print(web)
 75         i = input("--->:")
 76         if i == "1":
 77             user = login.login_check()
 78             break
 79         elif i == "2":
 80             login.creat_user()
 81         else:
 82             print("请选择正确的编号!")
 83     buy(user)
 84 
 85 def manage_type():
 86     password = "123456"
 87     pswd = input("请输入管理员密码:")
 88     if pswd == password:
 89         while True:
 90             goods_dir = goods_list()
 91             for i in goods_dir:
 92                 print(i,goods_dir[i])
 93             goods_contral = input("1:修改价格、2:添加商品、3:删除商品、q:退出:")
 94             if goods_contral == "1":
 95                 change_goods = input("选择要修改的商品:")
 96                 goods_money = input("输入%s的新价格:"%change_goods)
 97                 goods_dir[change_goods] = int(goods_money)
 98                 with open(goods_path, "w", encoding="UTF-8") as f_dump:
 99                     json.dump(goods_dir, f_dump)
100             elif goods_contral == "2":
101                 new_goods = input("新商品:")
102                 new_price = input("价格:")
103                 goods_dir[new_goods] = int(new_price)
104                 print("添加成功")
105                 with open(goods_path, "w", encoding="UTF-8") as f_dump:
106                     json.dump(goods_dir, f_dump)
107             elif goods_contral == "3":
108                 del_goods = input("选择要删除的商品:")
109                 del goods_dir[del_goods]
110                 with open(goods_path, "w", encoding="UTF-8") as f_dump:
111                     json.dump(goods_dir, f_dump)
112             elif goods_contral == "q":
113                 break
114             else:
115                 print("输入错误!")
116     else:
117         print("密码错误!")
118         exit()
119 
120 def main():
121     user_type = type_choice()
122     if user_type == 1:  # 客户
123         customer_type()
124     elif user_type == 2:  # 商家
125         manage_type()
126     else:  # 错误
127         exit()
128 
129 if __name__ == "__main__":
130     main()
shopping.py

4、db目录:

技术分享图片
{"bike": 200, "fruit": 80, "book": 300, "phone": 2000}
goods.json
技术分享图片
{"aa": "123", "zz": "123", "jj": "123"}
user_data.json
技术分享图片
{"jj": {"money": 919, "user_goods": [["fruit", 80], ["phone", 1]]}, "zz": {"money": 379, "user_goods": [["bike", 300], ["bike", 200], ["bike", 200]]}, "aa": {"money": 1420, "user_goods": [["bike", 300], ["fruit", 80]]}}
user_infor.json

5、doc目录:

readme.doc

 

python 改良购物车

标签:模式   用户登录   lse   退出   UI   png   return   []   abs   

原文地址:https://www.cnblogs.com/hy0822/p/9152758.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!