ATM
readme:
#项目说明 ##ATM+购物车 #项目需求 1,额度 15000或自定义 -->登录功能 2,实现购物商城,买东西加入购物车,调用信用卡接口结账 -->购物功能 支付功能 3,可以提现,手续费5% -->提现功能 4,支持多账户登录 -->登录功能 5,支持账户间转账 -->转账功能 6,记录每月日常消费流水 -->查看流水功能 7,提供还款接口 -->还款功能 8,ATM记录操作日志 -->记录日志功能 9,提供管理接口,包括添加账户、用户额度,冻结账户等。。。-->管理员功能 10,用户认证用装饰器 -->登录认证装饰器 ## 用户视图层展示给用户可选择的功能 1,注册功能 2,登录功能 3,查看余额 4,转账功能 5,取款款功能 6,还款功能 7,查流水功能 8,购物功能 9,查看购物车 #一个项目是如何从无到有 ## 一 需求分析 1,拿到项目,会先在客户端那里一起讨论需求, 商量项目的功能是否能实现,得到一个需求文档。 2,最后在公司内部需要开一次会议,最终得到一个开发文档, 交给不同程序员进行开发。 - python:后端 ,爬虫 -不同岗位: - UI界面设计: - 前端: - 后端: - 测试: - 运维: ## 二 程序的架构设计 ## 三 分任务开发 ## 四 测试 ## 五 上线
start
1 import sys 2 from conf import setting 3 from core import src 4 5 # 将程序根路径搭建好 6 path=setting.BASE_DIR 7 sys.path.append(path) 8 9 if __name__ == ‘__main__‘: 10 src.run()
conf-->setting
import os BASE_DIR=os.path.dirname(os.path.dirname(__file__))
lib-->common
# 用户登录状态验证装饰器 from core import src def logging_auth(func): def wrapper(*args,**kwargs): if src.user_auth[‘is_auth‘]: return func(*args,**kwargs) else: print(‘请先登录‘) src.logging() return wrapper import logging def write_logger(name): return logging.info(name)
db-->db_handler
import json,os from conf import setting # 查看用户信息 def select(user_name): path=setting.BASE_DIR path_file=os.path.join(path,‘db‘,‘%s.json‘%user_name) # 判断用户是否存在 if os.path.exists(path_file): with open(path_file,mode=‘r‘,encoding=‘utf-8‘)as f: user_dic=json.load(f) # print(user_dic) return user_dic else: return False # 更改用户信息 def update(user_dic): path = setting.BASE_DIR path_file = os.path.join(path, ‘db‘, ‘%s.json‘ %user_dic[‘name‘]) with open(path_file,mode=‘wt‘,encoding=‘utf-8‘)as f: json.dump(user_dic,f) f.flush()
interface:
user
from db import db_handler # 用户信息 def user_info_interface(user_name): user_dic=db_handler.select(user_name) return user_dic # 注册接口 def register_interface(user_name,pwd): user_dic={‘name‘:user_name,‘pwd‘:pwd,‘locked‘:False,‘balance‘:20000,‘shopping_car‘:{},‘bank_flow‘:[]} db_handler.update(user_dic)
bank
# 查看余额接口 from db import db_handler from lib import common def check_balance_interface(user_name): user_dic=db_handler.select(user_name) balance=user_dic[‘balance‘] return balance # 查看流水 def check_bank_flow(user_name): user_dic = db_handler.select(user_name) bank_flow = user_dic[‘bank_flow‘] for i in bank_flow: print(i) # 取款接口 def withdraw_interface(money,name): user_dic = db_handler.select(name) balance = user_dic[‘balance‘] if balance >= money * 1.01: balance -= money * 1.01 user_dic[‘balance‘] = balance # 记录流水 user_dic[‘bank_flow‘].append("%s 取款 %s元" % (name, money)) # 取款日志 common.write_logger("%s 还款 %s元" % (name, money)) db_handler.update(user_dic) else: print(‘余额不足‘) # 转账接口 def transfer_interface(from_user,to_user,money): from_user_dic = db_handler.select(from_user) from_user_balance = from_user_dic[‘balance‘] to_user_dic = db_handler.select(to_user) to_user_balance = to_user_dic[‘balance‘] if from_user_balance>=money: from_user_balance -=money to_user_balance +=money from_user_dic[‘balance‘]=from_user_balance to_user_dic[‘balance‘]=to_user_balance # 记录转账流水 from_user_dic[‘bank_flow‘].append(‘%s 向 %s 转了 %s 元‘ % (from_user, to_user, money)) to_user_dic[‘bank_flow‘].append(‘%s 收 %s 的 %s元‘ % (to_user, from_user, money)) # 记录转账日志 common.write_logger(‘%s 向 %s 转了 %s 元‘ % (from_user, to_user, money)) common.write_logger(‘%s 收到来自 %s 的 %s元‘ % (to_user, from_user, money)) # 更新用户信息 db_handler.update(from_user_dic) db_handler.update(to_user_dic) print(‘转账成功‘) else: print(‘余额不足‘) return # 付款接口 from interface import user def repay_interface(name, money): # 获取用户信息 user_dic = user.user_info_interface(name) if user_dic[‘balance‘]>=money: user_dic[‘balance‘] -= money # 记录流水 user_dic[‘bank_flow‘].append("%s 还款 %s元" % (name, money)) # 取款日志 common.write_logger("%s 还款 %s元" % (name, money)) db_handler.update(user_dic) else: print(‘余额不足‘)
shop
from db import db_handler from interface import user def add_shopping_car_interface(name, shopping_cart): user_dic = user.user_info_interface(name) # 把购物车数据存入到用户信息中 user_dic[‘shopping_car‘] = shopping_cart # {"tea":{"price":200,"count":2}} db_handler.update(user_dic) def check_shopping_car_interface(name): user_dic=user.user_info_interface(name) pro_list=user_dic[‘shopping_car‘] for key in pro_list: print(key)
core-->src
1 # 注册功能 2 3 from interface import user 4 from lib import common 5 def register(): 6 # 登录状态不能注册 7 if user_auth[‘is_auth‘]: 8 print(‘登录状态‘) 9 return 10 print(‘注册功能‘) 11 while True: 12 user_name = input(‘请输入注册用户名:‘).strip() 13 14 if user_name.isalpha(): 15 ## 判断当前用户名是否已经存在,如果有用户信息,则存在。 16 if user.user_info_interface(user_name): 17 print(‘用户名已存在‘) 18 continue 19 else: 20 while True: 21 pwd = input(‘请输入注册密码:‘).strip() 22 re_pwd = input(‘请再次注册输入密码:‘).strip() 23 24 if pwd == re_pwd: 25 if len(pwd) >= 5: 26 # 调用注册接口 27 user.register_interface(user_name, pwd) 28 29 print(‘注册成功‘) 30 break 31 else: 32 print(‘注册密码长度不小于5‘) 33 continue 34 else: 35 print(‘两次输入密码不一样‘) 36 continue 37 break 38 else: 39 print(‘用户名必须是字母组合‘) 40 41 42 # 记录用户登录状态 43 user_auth = {‘name‘: None, ‘is_auth‘: False} # 为什么不放用户信息字典里? 44 from db import db_handler 45 46 def logging(): 47 # 登录状态不能登录 48 if user_auth[‘is_auth‘]: 49 print(‘登录状态‘) 50 return 51 print(‘登录功能‘) 52 count=0 53 while True: 54 user_name = input(‘请输入登录用户名:‘).strip() # 可以将此句移动到while循环外 55 # 校验登录用户名是否注册 56 user_dic = user.user_info_interface(user_name) 57 # 用户名存在 58 if user_dic: 59 # 如果用户名没有锁定 60 if not user_dic[‘locked‘]: 61 # 用户名次数 62 if count!=3: 63 64 pwd=input(‘请输入密码:‘).strip() 65 66 if pwd==user_dic[‘pwd‘]: 67 user_auth[‘name‘]=user_name 68 user_auth[‘is_auth‘]=True 69 print(‘登录成功‘) 70 break 71 else: 72 # 此处有Bug,次数累加对事不对人。 73 count +=1 74 print(‘密码错误‘) 75 else: 76 user_dic[‘locked‘] = True 77 # 这里没必要再写用户锁定接口了 78 db_handler.update(user_dic) 79 print(‘输错次数达到三次,用户名已被锁定‘) 80 break 81 else: 82 print(‘用户名已锁定‘) 83 else: 84 print(‘用户名不存在‘) 85 86 87 88 # 查询余额功能 89 90 @common.logging_auth 91 def check_balance(): 92 print(‘查看余额功能‘) 93 user_name = input(‘请输入所要查看用户名:‘).strip() 94 # 调用查看余额功能接口 95 balance = bank.check_balance_interface(user_name) 96 print(balance) 97 98 99 # 取款功能 100 101 @common.logging_auth 102 def withdraw(): 103 while True: 104 money=input(‘请输入取款金额:‘).strip() 105 106 if money.isdigit(): 107 money=int(money) 108 # 调用取款接口 109 bank.withdraw_interface(money,user_auth[‘name‘]) 110 print(‘取款成功‘) 111 break 112 113 else: 114 print(‘输入金额不合法‘) 115 116 117 # 转账功能 118 119 from interface import bank 120 @common.logging_auth 121 def transfer(): 122 while True: 123 money=input(‘请输入转账金额:‘).strip() 124 if money.isdigit(): 125 money=int(money) 126 127 from_user=user_auth[‘name‘] 128 to_user=input(‘请输入收款人:‘).strip() 129 130 # 判断收款人是否存在 131 if db_handler.select(to_user): 132 133 # 调用转账接口 134 bank.transfer_interface(from_user,to_user,money) 135 break 136 else: 137 print(‘收款人不存在‘) 138 continue 139 else: 140 print(‘输入金额不合法‘) 141 continue 142 143 144 145 def repay(): 146 money=input(‘请输入还款金额‘).strip() 147 if money.isdigit(): 148 money=int(money) 149 bank.repay_interface(user_auth[‘name‘],money) 150 151 print(‘还款成功‘) 152 153 154 155 def check_flow(): 156 157 bank.check_bank_flow(user_auth[‘name‘]) 158 print(‘查看流水成功‘) 159 160 # 查看日志 161 from lib import common 162 def check_logging(): 163 # 调记录日志接口 164 user_log=common.write_logger(user_auth[‘name‘]) 165 return user_log 166 167 168 169 170 def exit(): 171 return exit 172 173 174 175 # 加入购物车 176 from interface import shop 177 @common.logging_auth 178 def shopping(): 179 print(‘购物车功能‘) 180 # 将购物车放到用户信息中 181 shopping_car={} 182 while True: 183 184 # 可选商品清单 185 shopping_list=[ 186 [‘bicycle‘,2000], 187 [‘soccer‘,500], 188 [‘book‘,200], 189 [‘notepad‘,100] 190 ] 191 192 # 展示商品清单 193 for i,product in enumerate(shopping_list): 194 print(i,product) 195 196 choose_num=input(‘请输入商品编号:‘) 197 198 # 校验商品编号是否存在 199 if choose_num.isdigit(): 200 choose_num = int(choose_num) 201 202 if choose_num < len(shopping_list): 203 204 # 拿到商品信息 205 pro_name=shopping_list[choose_num][0] 206 pro_price=shopping_list[choose_num][1] 207 208 # 判断商品在购物车中是否存在 209 if pro_name not in shopping_car: 210 shopping_car[pro_name]= {‘price‘:pro_price,‘count‘:1} 211 else: 212 213 shopping_car[pro_name][‘count‘] +=1 214 print(‘已加入购物车‘) 215 else: 216 print(‘没有商品编号‘) 217 218 219 elif choose_num ==‘q‘: 220 221 # 判断购物车是否为空 222 if shopping_car: 223 224 # 调用添加购物车接口 225 shop.add_shopping_car_interface(user_auth[‘name‘],shopping_car) 226 print(‘加入购物车成功‘) 227 break 228 229 else: 230 print(‘购物车为空‘) 231 continue 232 233 234 # 查看购物车 235 236 @common.logging_auth 237 def check_shopping(): 238 shop.check_shopping_car_interface(user_auth[‘name‘]) 239 240 241 242 243 244 245 # 定义功能字典,索引对应功能。 246 func_dic = { 247 248 ‘1‘: register, 249 ‘2‘: logging, 250 ‘3‘: check_balance, 251 ‘4‘: transfer, 252 ‘5‘: withdraw, 253 ‘6‘: repay, 254 ‘7‘: shopping, 255 ‘8‘: check_flow, 256 ‘9‘:check_logging, 257 ‘10‘: check_shopping, 258 ‘11‘:exit 259 } 260 261 262 # 定义主程序 263 def run(): 264 while True: 265 print(‘‘‘ 266 1,注册功能 267 2,登录功能 268 3,查看余额 269 4,转账功能 270 5,取款功能 271 6,还款功能 272 7,购物功能 273 8,查流水功能 274 9,查看日志 275 10,查看购物车 276 11,退出 277 ‘‘‘) 278 279 option = input(‘Please enter your option:‘).strip() 280 281 if option.isdigit(): 282 283 if option in func_dic: 284 res=func_dic[option]() 285 286 # 输入11退出登录 287 if res==exit: 288 break 289 290 else: 291 print(‘No this option ‘) 292 continue 293 else: 294 print(‘Must be a number‘) 295 continue
Python基础篇===》《《第二次考试 ATM+购物车》》 [2021/4/10]
原文:https://www.cnblogs.com/huo-shui/p/14668805.html