标签:姓名 tran style prim create view inpu put rate
README:
模拟实现一个ATM + 购物商城程序
二、初步作业规划
1、此作业以ATM功能为主,购物商城为辅(作为一个功能添加在主菜单中)
主菜单:
[1]、用户登录 --- 1、(多)用户登录 2、切换主用户 注:每次可登录多个用户,但是能进行操作的只有主用户
[2]、信用卡中心---------1、我的信用卡 2、转账 3、提现取款 4、还款
[3]、购物商城-----------1、进入商城 2、购物车 3、我的购买记录(商城消费流水日志)
[4]、后台管理(仅限管理员)--1、添加用户 2、用户额度管理 3、冻结账户
2、[2]、[3]选项需进行用户验证、[4]选项需进行管理员验证
3、日志记录(商城消费流水,信用卡操作记录)
4、管理接口,只有管理员能登陆(添加用户、用户额度、冻结账户等)
5、简易效果图:
三、目录结构
├── atm_shoppingmall
├── README
├── bin #入口程序目录
│ ├── __init__.py
│ └── start.py #入口程序(启动程序)
│
│
├── modules #程序核心目录
│ ├── __init__.py
│ ├── Login.py #用户(管理员)登录、用户(管理员)认证模块
│ ├── card_center.py #信用卡中心模块
│ ├── creditcard.py #信用卡模块
│ ├── shopping_center.py #购物商城模块
│ ├── management.py #后台管理员模块
│ ├── public_mod.py #公用模块
│ └── log.py #日志模块
│
├── database #程序数据库
│ ├── Admin_data #管理员账户信息目录 -- admin.json
│ ├── Goods_data #商品信息目录 -- goods.json
│ ├── shoppingcar #购物车信息目录 -- 用id.json * 用户数
│ └── User_data #用户信息目录 -- 用户id.json * 用户数
│
└── logs #日志目录
├── atm_log #atm日志目录 -- 用户id.log * 用户数
└── shopping_log #购物日日志目录 -- 用户id.log * 用户数
三、数据样式
管理员账户信息:admin.json --> {"User_name": "admin", "Password": "202cb962ac59075b964b07152d234b70",
"Login_status": 0}
商品信息: goods.json --> {"\u7535\u5b50\u4ea7\u54c1": [{"iphoneX": 8000}, {"\u5c0f\u7c73MAX": 3000},
{"\u534e\u4e3amate": 4000}, {"macpro": 14000}, {"TF": 10000}, {"Lenovo": 5000}], "\u670d\u88c5": [{"\u77ed\u8896": 100},
{"\u77ed\u88e4": 150}, {"\u7fbd\u7ed2\u670d": 500}, {"\u88d9\u5b50": 800}, {"\u5973\u6b3e\u5927\u8863": 1000},
{"\u5973\u6b3e\u7fbd\u7ed2\u670d": 1000}], "\u7535\u5668": [{"\u6d77\u5c14\u51b0\u7bb1": 4000},
{"\u7f8e\u7684\u51b0\u7bb1": 3000}, {"\u897f\u95e8\u5b50\u51b0\u7bb1": 6000}, {"\u6d77\u4fe1\u7535\u89c6": 3500},
{"\u521b\u7ef4\u7535\u89c6": 3200}, {"TCL\u7535\u89c6": 4000}], "\u5bb6\u5177": [{"\u771f\u76ae\u6c99\u53d1": 5000},
{"\u5b9e\u6728\u5e8a": 2000}, {"\u5b9e\u6728\u9910\u684c": 3000}, {"\u6905\u5b50": 400}, {"\u4e66\u684c": 500}, {"\u8863\u67dc": 600}]}
购物车信息:1001_shoppingcar.json --> {"shoppingcar": []}
1002_shoppingcar.json --> {"shoppingcar": []}
......
......
......
用户信息:1001.json --> {"Card_num": "1001", "User_name": "luffy", "Password": "202cb962ac59075b964b07152d234b70", "Login_status": 0,
"Card_status": 1, "Limit": 15000, "Available_limit": 14000, "balance": 47000}
......
......
用户信息:“卡号”,“姓名”,“密码”,“登陆状态”,“信用卡状态”(是否被冻结),“额度”,“可用额度”,“余额”
四、主要函数接口
1、authentication(func) # 用户认证装饰器
2、def authentication_admin(func) # 管理员认证装饰器
3、login() # 信用卡用户登录
4、admin_login() # 管理员用户登录
5、show_cardinfo() # 显示信用卡信息
6、withdraw_cash() # 提现到余额
7、transfer_accounts() # 转账
8、repayment() # 还款
9、balance_withhold(total) # 从余额扣款(支付商品),传入参数:应付金额
10、Limit_withhold(total) # 信用卡扣款(支付商品),传入参数:应付金额
11、shoppingcar_goods(dir,filename) # 获取商品or购物车数据,传入参数:文件目录、文件名
11、update_shoppingcar(goods) # 更新购物车
12、shoppingcar_list() # 获取当前购物车列表
11、shopping() # 购物:添加商品到购物车
12、shoppingcar() # 打印购物车、减少购物车商品,合并支付
13、show_history() # 查看历史购买记录
14、adduser() # 添加用户
15、update_limit() # 更改额度
16、show_freeze() # 打印当前被冻结的信用卡用户
17、freeze() # 冻结信用卡账户
18、unfreeze() # 解冻用户
19、check_card(file_name) # 校验用户名是否已注册,传入参数为:文件名
20、password_hash(passwd) # 将password 进行mod5加密,传入参数为:密码
21、get_userinfo(card) # 获取相应信用卡号的用户信息,传入参数为:信用卡号
22、get_admininfo() # 获取管理员账户信息
23、create_userfile(user,user_file) # 生成用户文件,传入参数为:文件内容、文件名
24、create_adminfile(admin_info) # 生成新的管理员文件,传入参数:文件内容
25、get_activeuser(dir) # 获取已登录的用户列表
26、login_out(dir) # 当系统退出时,注销所有在线用户,登录状态改为:0
27、get_freeze() # 获被冻结的用户id列表
29、get_primaryid() # 获取主用户id
30、update_status(id,status) # 修改账号登录状态,传入参数:信用卡ID
五、运行说明
1、试用信用卡账户: 信用卡号:1001 密码:123(数据文件中加密)
信用卡号:1002 密码:123(数据文件中加密)
2、管理员账号: 用户名:admin 密码:123(数据文件中加密)
3、数据文件说明: 用户数据、购物车数据、日志数据,都是单独生成一个对应的json文件(文件名包含用户id方便查找)
4、程序开始: 运行 bin目录下的 start.py文件
六、代码
/bin/start.py # 程序启动
1 #!/usr/bin/env python 2 # _*_coding:utf-8_*_ 3 # @Author : Wenjie 4 import sys,os 5 6 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 7 sys.path.append(BASE_DIR) 8 DB_DIR = os.path.join(BASE_DIR,‘database‘) 9 UserData_DIR = os.path.join(DB_DIR,‘User_data‘) 10 from modules import card_center,Login,public_mod,management,shopping_center 11 12 13 def main(): # 主函数 14 while True: 15 print("=========XX银行动卡空间=========") 16 print(" [1] 信用卡账户登陆管理") 17 print(" [2] 信用卡中心") 18 print(" [3] 购物商城") 19 print(" [4] 后台管理(仅限管理员)") 20 print(" [q] 退出系统") 21 result = input("请输入功能号(1,2,3,4,q):").strip() 22 if result == "1": 23 while True: 24 print("*=+"*10) 25 print("[1]、 (多)用户登录") 26 print("[2]、 切换主用户") 27 print("[q]、 退出") 28 choice1 = input(‘> ‘) 29 if choice1 == ‘1‘: 30 active_users = public_mod.get_activeuser(UserData_DIR) 31 if len(active_users) == 0: 32 print("当前无任何用户登录") 33 user_id = Login.login() 34 if type(user_id) == list: 35 public_mod.update_status(user_id[0],2) 36 public_mod.show_onlineusers() 37 else: 38 public_mod.show_onlineusers() 39 print("请勿重复登录!") 40 user_id = Login.login() 41 if type(user_id) == list: 42 public_mod.update_status(user_id[0],1) 43 public_mod.show_onlineusers() 44 elif choice1 == ‘2‘: 45 active_users = public_mod.get_activeuser(UserData_DIR) 46 if len(active_users) == 1: 47 public_mod.show_onlineusers() 48 print("当前登录账号有且为1,该账号为主账号!") 49 elif len(active_users) > 1: 50 public_mod.show_onlineusers() 51 change_to = input("输入更改后的主账号(账号必须已登录!):") 52 ID_list = [] 53 for i in active_users: 54 ID_list.append(i[0]) 55 if change_to in ID_list: 56 for i in active_users: 57 if change_to == i[0]: 58 i[1] = 2 59 public_mod.update_status(i[0],2) 60 else: 61 i[1] = 1 62 public_mod.update_status(i[0],1) 63 public_mod.show_onlineusers() 64 else: 65 print("无效的输入!") 66 else: 67 print("当前无用户登录!") 68 elif choice1 == ‘q‘: 69 break 70 else: 71 print("无效的输入") 72 elif result == "2": 73 while True: 74 print("*=*"*10) 75 print("[1]、 我的信用卡 ") 76 print("[2]、 提现取款 ") 77 print("[3]、 转账 ") 78 print("[4]、 还款 ") 79 print("[q]、 退出信用卡中心 ") 80 choice2 = input("请输入功能号(1,2,3,4,q):").strip() 81 if choice2 == "1": 82 card_center.show_cardinfo() 83 elif choice2 == "2": 84 card_center.withdraw_cash() 85 elif choice2 == "3": 86 card_center.transfer_accounts() 87 elif choice2 == "4": 88 card_center.repayment() 89 elif choice2 == "q": 90 break 91 else: 92 print("无效的输入...") 93 elif result == "3": 94 while True: 95 print("*=+" * 10) 96 print("[1]、 进入商城") 97 print("[2]、 我的购物车") 98 print("[3]、 我的购买记录") 99 print("[q]、 退出商城") 100 choice3 = input(">") 101 if choice3 == ‘1‘: 102 shopping_center.shopping() 103 elif choice3 == ‘2‘: 104 shopping_center.shoppingcar() 105 elif choice3 == ‘3‘: 106 shopping_center.show_history() 107 elif choice3 == ‘q‘: 108 break 109 else: 110 print("无效的输入!") 111 elif result == "4": 112 while True: 113 print("*=*" * 10) 114 print("[1]、 添加信用卡用户 ") 115 print("[2]、 更改信用卡额度") 116 print("[3]、 冻结信用卡账户 ") 117 print("[4]、 解冻信用卡账户 ") 118 print("[q]、 退出 ") 119 choice4 = input("请输入功能号(1,2,3,4,q):").strip() 120 if choice4 == ‘1‘: 121 management.adduser() 122 elif choice4 == ‘2‘: 123 management.update_limit() 124 elif choice4 == ‘3‘: 125 management.freeze() 126 elif choice4 == ‘4‘: 127 management.unfreeze() 128 elif choice4 == ‘q‘: 129 public_mod.admin_loginout() 130 break 131 else: 132 print("无效的输入!") 133 elif result == "q": 134 public_mod.login_out(UserData_DIR) 135 sys.exit("正在退出系统...") 136 else: 137 print("*=*"*10) 138 print("无效的输入:%s"%result) 139 main()
/modules/card_center.py # 信用卡中心模块
1 #!/usr/bin/env python 2 # _*_coding:utf-8_*_ 3 # @Author : Wenjie 4 import os,sys 5 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 6 sys.path.append(BASE_DIR) 7 DB_DIR = os.path.join(BASE_DIR,‘database‘) 8 UserData_DIR = os.path.join(DB_DIR,‘User_data‘) 9 from modules import public_mod,Login,log 10 #1、我的信用卡 2、转账 3、提现取款 4、还款 11 12 #显示信用卡信息 13 @Login.authentication # 验证用户是否登录 14 def show_cardinfo(): # 显示主信用卡账号信息 15 primary_id = public_mod.get_primaryid() 16 user_dic = public_mod.get_userinfo(primary_id) 17 Card_num = user_dic["Card_num"] 18 User_name = user_dic["User_name"] 19 Limit = user_dic["Limit"] 20 Available_limit = user_dic["Available_limit"] 21 balance = user_dic["balance"] 22 info = ‘‘‘ 23 ------------------------------------- 24 信用卡号: %s 25 用户名 : %s 26 我的额度: %d 27 可用额度: %d 28 我的存款: %d 29 30 ‘‘‘%(Card_num,User_name,Limit,Available_limit,balance) 31 print(info) 32 33 def show_money(): # 获取当前账户的财产信息,以列表返回 34 primary_id = public_mod.get_primaryid() 35 card_info = public_mod.get_userinfo(primary_id) 36 Limit = card_info["Limit"] 37 Available_limit = card_info["Available_limit"] 38 balance = card_info["balance"] 39 return_money = Limit - Available_limit 40 money_info = [Limit,Available_limit,balance] 41 print("-----------------------------------") 42 print(" 总额度 : %d "%Limit) 43 print(" 当前可用额度 : %d "%Available_limit) 44 print(" 当前余额 : %d "%balance) 45 print(" 当前待还 : %d "%return_money) 46 47 # 提现到余额 48 @Login.authentication # 验证用户是否登录 49 def withdraw_cash(): 50 show_money() 51 primary_id = public_mod.get_primaryid() 52 card_info = public_mod.get_userinfo(primary_id) 53 Card_num = card_info["Card_num"] 54 Limit = card_info["Limit"] 55 passwd = card_info["Password"] 56 Available_limit = card_info["Available_limit"] 57 balance = card_info["balance"] 58 money = input("输入提现到余额的大小:") 59 if money.isdigit(): 60 money = int(money) 61 if money <= 0: 62 print("请输入大于0的数字!") 63 elif money < Available_limit: 64 password = input("请输入密码:") 65 hash_password = public_mod.password_hash(password) 66 if hash_password == passwd: 67 message = "提现到余额%d,手续费%d"%(money,money*0.05) 68 log.atm_log(message) # 打印提现日志 69 new_balance = balance + money - money*0.05 70 new_Available = Available_limit - money 71 card_info["Available_limit"] = new_Available 72 card_info["balance"] = new_balance 73 filename = Card_num + ‘.json‘ 74 public_mod.create_userfile(card_info,filename) 75 print("提现到余额%d,手续费%d"%(money,money*0.05)) 76 show_money() 77 else: 78 print("密码错误!") 79 else: 80 print("可用额度不足!") 81 else: 82 print("无效的输入...") 83 # 转账 84 @Login.authentication 85 def transfer_accounts(): 86 show_money() 87 print("友情提示:转账功能仅针对账号余额之间进行!") 88 primary_id = public_mod.get_primaryid() 89 card_info = public_mod.get_userinfo(primary_id) # 当前用户的信息{} 90 from_balance = card_info["balance"] # 当前用户余额 91 password = card_info["Password"] 92 otherside_id = input("收款ID:") 93 file_name = otherside_id + ‘.json‘ 94 if public_mod.check_card(file_name): # 判断收款用户id是否存在 95 transfer_num = input("请输入转账金额:") 96 if transfer_num.isdigit(): 97 transfer_num = int(transfer_num) 98 if transfer_num <= 0: 99 print("请输入大于0的数字!") 100 elif transfer_num < from_balance: 101 passwd = input("请输入密码:") 102 hash_passwd = public_mod.password_hash(passwd) 103 if hash_passwd == password: 104 message = "转账给%s用户%d"%(otherside_id,transfer_num) 105 log.atm_log(message) # 打印转账日志 106 otherside_dic = public_mod.get_userinfo(otherside_id) # 获取收款方用户数据{} 107 otherside_balance = otherside_dic["balance"] 108 card_info["balance"] = from_balance - transfer_num 109 filename1 = primary_id + ‘.json‘ 110 public_mod.create_userfile(card_info, filename1) # 更新支付方数据表 111 otherside_dic["balance"] = otherside_balance + transfer_num 112 public_mod.create_userfile(otherside_dic, file_name) # 更新收款方数据表 113 print("转账成功!") 114 show_money() 115 else: 116 print("密码错误!") 117 else: 118 print("余额不足!") 119 else: 120 print("无效的输入!") 121 else: 122 print("该用户不存在!") 123 124 # 还款 125 @Login.authentication 126 def repayment(): 127 show_money() 128 print("友情提示:信用卡额度将从账户存款余额中扣除,请保证余额充足!") 129 primary_id = public_mod.get_primaryid() 130 card_info = public_mod.get_userinfo(primary_id) # 当前用户的信息{} 131 Limit = card_info["Limit"] 132 Available_limit = card_info["Available_limit"] 133 return_money = Limit - Available_limit 134 balance = card_info["balance"] 135 return_num = input("请输入要还款的金额:") 136 if return_num.isdigit(): 137 return_num = int(return_num) 138 if balance >= return_money: 139 if return_num <= 0: 140 print("输入金额应大于0!") 141 elif return_num<= return_money: 142 message = "还款%d"%return_num 143 card_info["balance"] = balance - return_num 144 card_info["Available_limit"] = Available_limit + return_num 145 filename = primary_id + ‘.json‘ 146 public_mod.create_userfile(card_info, filename) # 更新数据表 147 show_money() 148 log.atm_log(message) # 打印还款日志 149 else: 150 print("还款金额有误!") 151 else: 152 if return_num <= 0: 153 print("输入金额应大于0!") 154 elif return_num <= balance: 155 message = "还款%d" % return_num 156 card_info["balance"] = balance - return_num 157 card_info["Available_limit"] = Available_limit + return_num 158 filename = primary_id + ‘.json‘ 159 public_mod.create_userfile(card_info, filename) # 更新数据表 160 show_money() 161 log.atm_log(message) # 打印还款日志 162 else: 163 print("余额不足!") 164 else: 165 print("无效的输入!") 166 167 def balance_withhold(total): # 从余额扣款(支付商品) 168 primar_id = public_mod.get_primaryid() 169 user_dic = public_mod.get_userinfo(primar_id) #获取当前主账号信息 170 balance = user_dic["balance"] 171 new_balance = balance - total 172 user_dic["balance"] = new_balance 173 public_mod.create_userfile(user_dic, primar_id + ‘.json‘) # 更新用户信息 174 175 176 177 def Limit_withhold(total): # 信用卡扣款(支付商品) 178 primar_id = public_mod.get_primaryid() 179 user_dic = public_mod.get_userinfo(primar_id) # 获取当前主账号信息 180 Available_limit = user_dic["Available_limit"] 181 new_Availablelimit = Available_limit -total 182 user_dic["Available_limit"] = new_Availablelimit 183 public_mod.create_userfile(user_dic, primar_id + ‘.json‘) # 更新用户信息
/modules/shopping_center.py # 购物中心模块
1 #!/usr/bin/env python 2 # _*_coding:utf-8_*_ 3 # @Author : Wenjie 4 import json,sys,os,hashlib,datetime 5 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 6 sys.path.append(BASE_DIR) 7 DB_DIR = os.path.join(BASE_DIR,‘database‘) 8 UserData_DIR = os.path.join(DB_DIR,‘User_data‘) 9 Goods_DIR = os.path.join(DB_DIR,‘goods_data‘) 10 Shoppingcar_DIR = os.path.join(DB_DIR,‘shoppingcar‘) 11 Log_DIR = os.path.join(BASE_DIR,‘logs‘) 12 Shoppinglog_DIR = os.path.join(Log_DIR,‘shopping_log‘) 13 Atmlog_DIR = os.path.join(Log_DIR,‘atm_log‘) 14 from modules import public_mod,Login,card_center,log 15 16 17 def shoppingcar_goods(dir,filename): # 获取商品数据或购物车数据 18 with open(os.path.join(dir,filename),‘r‘,encoding="utf-8") as f: 19 data = f.read() 20 data = json.loads(data) 21 return data 22 23 def update_shoppingcar(goods): # 加入购物车更新 24 primar_id = public_mod.get_primaryid() 25 data = shoppingcar_goods(Shoppingcar_DIR, primar_id + ‘_shoppingcar.json‘) 26 valu = data["shoppingcar"] 27 valu.append(goods) 28 data[‘shoppingcar‘] = valu 29 with open(os.path.join(Shoppingcar_DIR, primar_id + ‘_shoppingcar.json‘), ‘w‘,encoding="utf-8") as f: 30 json.dump(data, f) 31 32 def shoppingcar_list(): # 获取当前购物车列表 33 primar_id = public_mod.get_primaryid() 34 data = shoppingcar_goods(Shoppingcar_DIR, primar_id + ‘_shoppingcar.json‘) 35 value_list = data["shoppingcar"] 36 return value_list 37 38 def show_shoppingcar(): # 打印当前购物车列表 39 value_list = shoppingcar_list() 40 print("我的购物车:") 41 print("*=+" * 10) 42 for i, k in enumerate(value_list): 43 key = list(k.keys())[0] 44 value = list(k.values())[0] 45 print(i, "%-10s\t%-10d" % (key, value)) 46 47 def clean_shoppingcar(data): # 减少or清空购物车 48 primar_id = public_mod.get_primaryid() 49 with open(os.path.join(Shoppingcar_DIR, primar_id + ‘_shoppingcar.json‘), ‘w‘, encoding="utf-8") as f: 50 json.dump(data, f) 51 52 @Login.authentication 53 def shopping(): # 购物:添加商品到购物车 54 goods_data = shoppingcar_goods(Goods_DIR,‘goods.json‘) # 获取商品数据 55 sort_list = [] 56 for key in goods_data: # 打印商品种类 57 sort_list.append(key) 58 while True: 59 print("*=+"*4,‘欢迎来到购物商城‘,"*=+"*4,) 60 print("ID GoodStyle") 61 for i,k in enumerate(sort_list): 62 print([i],k) 63 choice_sort = input("请输入您感兴趣的商品类别号码(q退出):").strip() 64 if choice_sort.isdigit(): 65 choice_sort = int(choice_sort) 66 if choice_sort in range(len(sort_list)): 67 print("%s\t%-10s\t%-10s"%("ID","Name","Price")) 68 keyname = sort_list[choice_sort] 69 val_list = goods_data[keyname] 70 while True: 71 for i,k in enumerate(val_list): # 打印相应的商品列表 72 key = list(k.keys())[0] 73 value = list(k.values())[0] 74 print([i],"%-10s\t%-10d"%(key,value)) 75 print("请选择心仪的商品:") 76 print("输入商品编号加入购物车,输入q返回上一级") 77 choice_goods = str(input(">")) 78 if choice_goods.isdigit(): 79 choice_goods = int(choice_goods) 80 if choice_goods in range(len(val_list)): 81 goods = val_list[choice_goods] 82 update_shoppingcar(goods) # 将商品写入购物车文件 83 print("商品已加入购物车!") 84 else: 85 print("该商品不存在!") 86 elif choice_goods == ‘q‘: 87 break 88 else: 89 print("无效的输入!") 90 elif choice_sort == "q": 91 break 92 else: 93 print("请输入数字ID") 94 @Login.authentication 95 def shoppingcar(): # 打印购物车、减少购物车商品,合并支付 96 while True: 97 value_list = shoppingcar_list() # 获取购物车列表 98 if len(value_list) > 0: 99 show_shoppingcar() #打印购物车 100 print("[1]、 删减购物车") 101 print("[2]、 合并支付") 102 print("[q]、 返回上级") 103 choice = input(">").strip() 104 if choice == ‘1‘: # 删减购物车 105 value_list1 = shoppingcar_list() 106 if len(value_list1) > 0: 107 show_shoppingcar() 108 id = input("请输入要移除的商品序号:").strip() 109 if id.isdigit(): 110 id = int(id) 111 if id in range(len(value_list1)): 112 del value_list1[id] 113 data = {"shoppingcar": value_list1} 114 clean_shoppingcar(data) 115 else: 116 print("无此商品,请仔细核对商品序号!") 117 else: 118 print("无效的输入!") 119 else: 120 print("购物车为空!") 121 elif choice == ‘2‘: # 合并支付 122 while True: 123 value_list2 = shoppingcar_list() 124 total = 0 125 for i in value_list2: 126 price = list(i.values())[0] 127 total += price 128 print("*=+"*10) 129 print("支付方式:") 130 print("[1]、账户余额支付") 131 print("[2]、信用卡支付") 132 print("[q]、返回上一级") 133 method = input(">") 134 primar_id = public_mod.get_primaryid() 135 user_dic = public_mod.get_userinfo(primar_id) # 获取当前主账号信息 136 balance = user_dic["balance"] 137 Available_limit = user_dic["Available_limit"] 138 if method == ‘1‘: 139 pass 140 # if balance < total: 141 # print("账户余额不足") 142 # else: 143 # shop_message = [] 144 # for i in value_list2: 145 # keys = list(i.keys())[0] 146 # values = list(i.values())[0] 147 # goods = [keys,values] 148 # shop_message.append(goods) 149 # log.shopping_log(shop_message) 150 # card_center.balance_withhold(total) # 余额扣款 151 # data = {"shoppingcar": []} 152 # clean_shoppingcar(data) # 清空购物车 153 # print("购物车已清空!") 154 # break 155 elif method == ‘2‘: 156 if Available_limit < total: 157 print("账户余额不足") 158 else: 159 shop_message = [] 160 for i in value_list2: 161 keys = list(i.keys())[0] 162 values = list(i.values())[0] 163 goods = [keys, values] 164 shop_message.append(goods) 165 log.shopping_log(shop_message) 166 card_center.Limit_withhold(total) 167 data = {"shoppingcar": []} 168 clean_shoppingcar(data) # 清空购物车 169 print("购物车已清空!") 170 break 171 elif choice == ‘q‘: 172 break 173 else: 174 print("无效的输入!") 175 else: 176 break 177 178 @Login.authentication 179 def show_history(): # 查看历史购买记录 180 primar_id = public_mod.get_primaryid() 181 file_name = primar_id + ‘.log‘ 182 with open(os.path.join(Shoppinglog_DIR,file_name), ‘r‘, encoding="utf-8") as f: 183 lines = f.readlines() 184 if len(lines) == 0: 185 print("无历史购买记录!") 186 else: 187 print("我的购买记录:") 188 for i in lines: 189 print(i)
/modules/Login.py # 登录及验证模块
1 #!/usr/bin/env python 2 # _*_coding:utf-8_*_ 3 # @Author : Wenjie 4 import json,sys,os,hashlib 5 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 6 sys.path.append(BASE_DIR) 7 DB_DIR = os.path.join(BASE_DIR,‘database‘) 8 UserData_DIR = os.path.join(DB_DIR,‘User_data‘) 9 10 from modules import public_mod 11 12 13 #信用卡用户登录 14 def login(): 15 i = 0 16 while i<3: 17 print("=*+"*10) 18 card_num = input("请输入卡号:") 19 passwd = input("请输入密码:") 20 file_name = card_num + ‘.json‘ 21 if public_mod.check_card(file_name): 22 hash_passwd = public_mod.password_hash(passwd) 23 user_info = public_mod.get_userinfo(card_num) 24 Card_num = user_info["Card_num"] 25 Password = user_info["Password"] 26 User_name = user_info["User_name"] 27 Login_status = user_info["Login_status"] 28 Card_status = user_info["Card_status"] 29 if Card_status == 1: 30 if card_num == Card_num and hash_passwd == Password: 31 print("Welcom! %s"%User_name) 32 return [card_num] 33 else: 34 if i < 2: 35 print("密码错误,剩余%d次机会!" % (2 - i)) 36 else: 37 print("密码错误!") 38 i += 1 39 else: 40 sys.exit("信用卡账户也被冻结,请联系管理员!") 41 else: 42 if i < 2: 43 print("信用卡ID不存在,剩余%d次机会!" % (2 - i)) 44 else: 45 print("信用卡ID不存在!") 46 i += 1 47 else: 48 sys.exit("信用卡ID或密码输错三次,系统退出!") 49 50 51 # 管理员登录 52 53 def admin_login(): 54 i = 0 55 while True: 56 admin_info = public_mod.get_admininfo() 57 User_name = admin_info["User_name"] 58 password = admin_info["Password"] 59 print("=*+" * 10) 60 username = input("请输入管理员用户名:") 61 passwd = input("请输入密码:") 62 hash_passwd = public_mod.password_hash(passwd) 63 if username == User_name and hash_passwd == password: 64 print("欢迎登陆!") 65 admin_info["Login_status"] = 1 66 public_mod.create_adminfile(admin_info) 67 break 68 else: 69 if i < 2: 70 print("用户名或密码错误,剩余%d次机会!" % (2 - i)) 71 else: 72 print("用户名或密码错误!") 73 i += 1 74 else: 75 sys.exit("用户名或密码输错三次,系统退出!") 76 77 def authentication(func): # 用户认证装饰器 78 def inner(): 79 online = public_mod.get_activeuser(UserData_DIR) 80 if len(online) == 0: 81 print("请先登录信用卡用户!") 82 user_id = login() 83 if type(user_id) == list: 84 public_mod.update_status(user_id[0], 2) 85 public_mod.show_onlineusers() 86 func() 87 else: 88 func() 89 return inner 90 91 def authentication_admin(func): 92 def inner(): 93 admin_info = public_mod.get_admininfo() 94 if admin_info["Login_status"] == 0: 95 print("请先登录!") 96 admin_login() 97 func() 98 else: 99 func() 100 return inner
/modules/public_mod.py # 共用模块
1 #!/usr/bin/env python 2 # _*_coding:utf-8_*_ 3 # @Author : Wenjie 4 import json,os,hashlib,sys 5 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 6 sys.path.append(BASE_DIR) 7 DB_DIR = os.path.join(BASE_DIR,‘database‘) 8 UserData_DIR = os.path.join(DB_DIR,‘User_data‘) 9 AdminData_DIR = os.path.join(DB_DIR,‘Admin_data‘) 10 11 def check_card(file_name): # 校验用户名是否已注册,传入参数为:文件名 12 for root,dirs,files in os.walk(UserData_DIR): 13 if file_name in files: 14 return True 15 else: 16 return False 17 18 def password_hash(passwd): # 将password 进行mod5加密,传入参数为:密码 19 hash = hashlib.md5() 20 hash.update(bytes(passwd,encoding=‘utf-8‘)) 21 return hash.hexdigest() 22 23 def get_userinfo(card): # 获取相应信用卡号的用户信息,传入参数为:信用卡号 24 filename = card + ‘.json‘ 25 with open(os.path.join(UserData_DIR,filename),‘r‘,encoding=‘utf-8‘) as f: 26 user_info = f.read() 27 return json.loads(user_info) 28 29 def get_admininfo(): # 获取管理员账户信息 30 filename = ‘admin‘ + ‘.json‘ 31 with open(os.path.join(AdminData_DIR, filename), ‘r‘, encoding=‘utf-8‘) as f: 32 admin_info = f.read() 33 return json.loads(admin_info) 34 35 def create_userfile(user,user_file): # 生成用户文件,传入参数为:文件内容、文件名 36 file_dir = os.path.join(UserData_DIR,user_file) 37 with open(file_dir,‘w‘,encoding=‘utf-8‘) as f: 38 json.dump(user,f) 39 40 def create_adminfile(admin_info): # 生成新的管理员文件,传入参数:文件内容 41 file_dir = os.path.join(AdminData_DIR,‘admin.json‘) 42 with open(file_dir,‘w‘,encoding=‘utf-8‘) as f: 43 json.dump(admin_info,f) 44 45 def get_activeuser(dir): # 获取已登录的用户列表 46 active_users = [] 47 for root, dirs, files in os.walk(dir): 48 for i in files: 49 i = i.strip(‘.json‘) 50 user_dic = get_userinfo(i) 51 if user_dic["Login_status"] > 0: 52 active_users.append([user_dic["Card_num"],user_dic["Login_status"]]) 53 return active_users 54 55 def login_out(dir): # 当系统退出时,注销所有在线用户,登录状态改为:0 56 active_users = [] 57 for root, dirs, files in os.walk(dir): 58 for i in files: 59 i = i.strip(‘.json‘) 60 user_dic = get_userinfo(i) 61 if user_dic["Login_status"] > 0: 62 user_dic["Login_status"] = 0 63 create_userfile(user_dic, i + ‘.json‘) 64 65 def admin_loginout(): # 当admin管理员退出时,修改登录状态为:0 66 admin_info = get_admininfo() 67 admin_info["Login_status"] = 0 68 create_adminfile(admin_info) 69 70 def get_freeze(): # 获被冻结的用户id列表 71 freeze_user = [] 72 for root, dirs, files in os.walk(UserData_DIR): 73 for i in files: 74 i = i.strip(‘.json‘) 75 user_dic = get_userinfo(i) 76 if user_dic["Card_status"] == 0: 77 freeze_user .append(user_dic["Card_num"]) 78 return freeze_user 79 80 def show_onlineusers(): #打印当前登录的用户,及状态 81 print("当前登录的用户有:") 82 show_online = get_activeuser(UserData_DIR) 83 for i in show_online: 84 if i[1] == 2: 85 print(i[0],i[1],"主账号") 86 else: 87 print(i[0],i[1]) 88 89 def get_primaryid(): # 获取主用户id 90 online = get_activeuser(UserData_DIR) 91 primary = [] 92 for i in online: 93 if i[1] == 2: 94 primary.append(i[0]) 95 primary_id = primary[0] 96 return primary_id 97 98 99 def update_status(id,status): # 修改账号登录状态,传入参数:信用卡ID 100 user_dic = get_userinfo(id) 101 user_dic["Login_status"] = status 102 filename = id + ‘.json‘ 103 create_userfile(user_dic, filename)
/modules/management.py
1 #!/usr/bin/env python 2 # _*_coding:utf-8_*_ 3 # @Author : Wenjie 4 import json,os,hashlib,sys 5 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 6 sys.path.append(BASE_DIR) 7 DB_DIR = os.path.join(BASE_DIR,‘database‘) 8 UserData_DIR = os.path.join(DB_DIR,‘User_data‘) 9 Shoppingcar_DIR = os.path.join(DB_DIR,‘shoppingcar‘) 10 history_dir = os.path.join(DB_DIR,‘history‘) 11 from modules import public_mod,Login 12 13 # 1、添加用户 2、用户额度管理 3、冻结账户 14 # 用户注册 15 #用户信息:“卡号”,“姓名”,“密码”,“登陆状态”,“信用卡状态”(是否被冻结),“额度”,“余额” 16 @Login.authentication_admin 17 def adduser(): # 添加用户 18 print("*=+"*10) 19 print("请按照以下要求正确填写信息^_^") 20 Card_num = input("card_num: ") 21 User_name = input("username:") 22 Password = input("password: ") 23 Limit = input("limit(额度范围(15000-20000):)") 24 if Limit.isdigit(): 25 Limit = int(Limit) 26 user_file = Card_num + ‘.json‘ 27 if not public_mod.check_card(user_file): 28 hash_passwd = public_mod.password_hash(Password) 29 users = {"Card_num": Card_num, "User_name": User_name, "Password": hash_passwd, 30 "Login_status": 0,"Card_status": 1,"Limit": Limit,"Available_limit": Limit, 31 "balance": 0} 32 public_mod.create_userfile(users,user_file) #生成信用卡用户文件 33 #生成用户购物车文件 34 data = {"shoppingcar":[]} 35 with open(os.path.join(Shoppingcar_DIR,Card_num + ‘_shoppingcar.json‘), ‘w‘, encoding="utf-8") as f: 36 json.dump(data, f) 37 print("注册成功!") 38 else: 39 print("用户名已存在!") 40 else: 41 print("请输入(15000-20000)的数字!") 42 43 44 # 更改额度 45 @Login.authentication_admin 46 def update_limit(): 47 print("*=+" * 10) 48 print("请按照以下要求正确填写信息^_^") 49 id = input("请输入要修改额度的信用卡ID:") 50 file_name = id + ‘.json‘ 51 if public_mod.check_card(file_name): 52 user_dic = public_mod.get_userinfo(id) 53 Limit = user_dic["Limit"] 54 print("卡号:%s 当前的额度为:%d"%(id,Limit)) 55 limit_num = input("请输入修改后的额度(15000-25000):") 56 if limit_num.isdigit(): 57 limit_num = int(limit_num) 58 if limit_num < 15000 and limit_num > 25000: 59 print("输入金额不在可用区间内!") 60 else: 61 user_dic["Limit"] = limit_num 62 public_mod.create_userfile(user_dic,file_name) 63 print("修改成功!") 64 print("卡号:%s 当前的额度为:%d" % (id, limit_num)) 65 else: 66 print("此处请输入数字!") 67 68 def show_freeze(): # 打印当前被冻结的信用卡用户 69 freeze_list = public_mod.get_freeze() 70 if len(freeze_list) == 0: 71 pass 72 else: 73 print("当前被冻结的用户有:") 74 for i,k in enumerate(freeze_list,1): 75 print(i,k) 76 77 # 冻结信用卡账户 78 @Login.authentication_admin 79 def freeze(): 80 print("*=+" * 10) 81 print("请按照以下要求正确填写信息^_^") 82 id = input("请输入要冻结的信用卡ID:") 83 file_name = id + ‘.json‘ 84 if public_mod.check_card(file_name): 85 user_dic = public_mod.get_userinfo(id) 86 Card_status = user_dic["Card_status"] 87 if Card_status == 0: 88 print("该信用卡已被冻结!") 89 else: 90 user_dic["Card_status"] = 0 91 public_mod.create_userfile(user_dic, file_name) 92 print("%s 已被冻结!"%id) 93 show_freeze() 94 95 # 冻结信用卡账户 96 @Login.authentication_admin 97 def unfreeze(): #解冻用户 98 show_freeze() 99 freeze_uers = public_mod.get_freeze() 100 print("*=+" * 10) 101 print("请按照以下要求正确填写信息^_^") 102 id = input("请输入要解冻的信用卡ID:") 103 file_name = id + ‘.json‘ 104 if public_mod.check_card(file_name): 105 if id in freeze_uers: 106 user_dic = public_mod.get_userinfo(id) 107 user_dic["Card_status"] = 1 108 public_mod.create_userfile(user_dic, file_name) 109 print("%s 已被解冻!" % id) 110 show_freeze() 111 else: 112 print("用户%s并未被冻结!"%id) 113 else: 114 print("该用户不存在!")
标签:姓名 tran style prim create view inpu put rate
原文地址:https://www.cnblogs.com/alvin-jie/p/8893155.html