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

python_项目_ATM和购物商城的程序

时间:2018-06-04 21:31:16      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:odi   out   card   check   出现   截断   oar   中心   完成   

1 需求

  模拟实现一个ATM + 购物商城程序
    额度15000或自定义
    实现购物商城,买东西加入购物车,调用信用卡接口结账
    可以提现,手续费5%
    支持多账户登录
    支持账户间转账
    记录每月日常消费流水
    提供还款接口
    ATM记录操作日志
    提供管理接口,包括添加账户、用户额度,冻结账户等。。。
    用户认证用装饰器

2 程序目录

技术分享图片
 1  ├── ATM+购物商城
 2             ├── core #入口程序目录
 3             │   ├── __init__.py
 4             │   └── main.py  #入口程序(启动程序)
 5             ├── conf #配置文件目录
 6             │   ├── __init__.py
 7             │   └── README.txt
 8             ├── modules #程序核心目录
 9             │   ├── __init__.py
10             │   ├── admin_center.py  #管理模块
11             │   ├── authenticate.py  #认证模块
12             │   ├── creditcard_center.py  #信用卡模块
13             │   ├── shopping_center.py  #购物模块
14             ├── database #程序数据库
15             │   ├── creditcard_info  #信用卡数据库
16             │   ├── creditcard_record  #信用卡流水记录数据库
17             │   ├── production_list  #商城产品数据库
18             │    |── user  #用户数据库
19             │   └── shopping_cart  #购物车数据库
20             │   ├── shopping_record  #购物记录
21 22             └── log
23                 ├── __init__.py
24                 └── user_flowlog  #用户操作日志 
程序目录

3 modules目录文件

技术分享图片
1 from . import creditcard_center, shopping_center, admin_center, authenticate
__init__
技术分享图片
  1 import os, sys, json, time
  2 
  3 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  4 sys.path.append(BASE_DIR)
  5 
  6 from modules import creditcard_center, admin_center
  7 logger = admin_center.get_logger()
  8 
  9 db_produ_list = BASE_DIR + r/database/production_list
 10 db_shopping_cart = BASE_DIR + r/database/shopping_cart
 11 shopping_record = BASE_DIR + r/database/shopping_record
 12 db_user = BASE_DIR + r/database/user
 13 db_credit_info =  BASE_DIR + r/database/creditcard_info
 14 user_log =  BASE_DIR + r/log/user_flowlog
 15 
 16 # 购物
 17 def shopping_mall():
 18     shopping_list = []
 19     buy_list = []
 20     with open(db_produ_list, r, encoding=utf-8) as f:
 21         for item in f:
 22             shopping_list.append(item.split(\n).split())
 23     while True:
 24         print(商城在售商品清单.center(50, -))
 25         for index, item in enumerate(shopping_list):
 26             print(%d %s %s % (index+1, item[0], item[1]))
 27         choice_id = input(选择要购买的商品编号或返回b)
 28         if choice_id.isdigit():
 29             choice_id = int(choice_id)
 30             if choice_id <= len(shopping_list) and choice_id >= 0:
 31                 buy_item = shopping_list[(int(choice_id) - 1)]
 32                 print(商品[%s]加入购物车,价格[¥%s] % (buy_item[0], buy_item[1]))
 33                 buy_list.append(buy_item)
 34                 shopping_log = [商品, buy_item[0], 价格, buy_item[1]]
 35                 message = --.join(shopping_log)
 36                 logger.info(message)
 37             else:
 38                 print(无效编号,请重新输入)
 39         elif choice_id == b:
 40             with open(db_shopping_cart, r+) as f1:
 41                 list = json.loads(f1.read())
 42                 list.extend(buy_list)
 43                 list = json.dumps(list)
 44                 f1.seek(0)
 45                 f1.truncate(0)  # 截断数据
 46                 f1.write(list)
 47             break
 48         else:
 49             print(无效的字符,请重新输入)
 50 
 51 # 购物车
 52 def shopping_cart():
 53     while True:
 54         with open(db_shopping_cart, r+, encoding=utf-8) as f1:
 55             shopping_list = json.loads(f1.read())
 56             sum = 0
 57             print(购物车信息清单.center(50, -))
 58             for index, item in enumerate(shopping_list):
 59                 print(index, item[0], item[1])
 60                 sum += int(item[1])
 61                 print(商品总额共计: % sum)
 62                 choice = input(清空购物车c或返回按b请选择:)
 63                 if choice == c:
 64                     with open(db_shopping_cart, w, encoding=utf-8) as f2:
 65                         list = json.dumps([])
 66                         f2.write(list)
 67                 elif choice == b:
 68                     break
 69 
 70 # 购物结算
 71 def shopping_pay(login_user):
 72     while True:
 73         with open(db_shopping_cart, r+, encoding=utf-8) as f:
 74             shopping_list = json.loads(f.read())
 75             sum = 0
 76             for item in shopping_list:
 77                 sum += int(item[1])
 78                 choice = input(当前商品总额为:¥%s,是否进行支付,是y,返回b,请选择 % sum)
 79                 if choice == y:
 80                     with open(db_user, r+, encoding=utf-8) as f1:
 81                         user_dic = json.loads(f1.read())
 82                         if user_dic[login_user][creditcard] == 0:
 83                             print(该用户%s未绑定信用卡,请到个人中心绑定信用卡 % login_user)
 84                         # 绑定的信用卡
 85                         else:
 86                             creditcard_center.credit_pay(sum, login_user)
 87                             shop_record(login_user, shopping_list)
 88                             break
 89                 elif choice == b:
 90                     break
 91 
 92 # 历史购物记录写入
 93 def shop_record(login_user,shopping_list):
 94     with open(shopping_record, r+) as f:
 95         record_dict = json.loads(f.read())
 96         month = time.strftime(%Y-%m-%d, time.localtime())
 97         times = time.strftime(%H:%M:%S)
 98         if str(login_user) not in record_dict.keys():
 99             record_dict[login_user] = {month:{times:shopping_list}}
100         else:
101             if month not in record_dict[login_user].keys():
102                 record_dict[login_user][month] = {time: shopping_list}
103             else:
104                 record_dict[login_user][month][times] = shopping_list
105             dict = json.dumps(record_dict)
106             f.seek(0)
107             f.write(dict)
108 
109 # 历史购物记录查询
110 def check_record(login_user):
111     while True:
112         with open(shopping_record, r+) as f1:
113             record_dict = json.loads(f1.read())
114             if login_user not in record_dict.keys():
115                 print(没有该用户%s购物记录 % login_user)
116             else:
117                 data = record_dict[login_user]
118                 for month in data:
119                     times = record_dict[login_user][month]
120                     for t in times:
121                         print(时间%s %s % (month, t))
122                         list1 = record_dict[login_user][month][t]
123                         print(商品     价格)
124                         for value in list1:
125                             print(%s    %s % (value[0], value[1]))
126         choice = input(是否返回b)
127         if choice == b:
128             break
shopping_center
技术分享图片
  1 import os
  2 import sys
  3 import json
  4 import time
  5 from modules import shopping_center, admin_center
  6 
  7 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  8 sys.path.append(BASE_DIR)
  9 
 10 logger = admin_center.get_logger()
 11 
 12 db_user = BASE_DIR + r/database/user
 13 db_credit_info = BASE_DIR + r/database/creditcard_info
 14 credit_record = BASE_DIR + r/database/credicard_record
 15 user_log = BASE_DIR + r/database/user_flowlog
 16 
 17 
 18 # 卡信息
 19 def creditcard_info():
 20     while True:
 21         with open(db_credit_info, r+) as f:
 22             creditcard_data = json.loads(f.read())
 23         creditcard_num = input(请输入需要查询的信用卡卡号(6位数字)).strip()
 24         if creditcard_num in creditcard_data.keys():
 25             print(您要查询的信用卡信息如下:.center(50, -))
 26             print(信用卡卡号:%s\n个人信息:%s\n可用额度:%s\n取现额度:%s\n % (
 27                 creditcard_num, creditcard_data[creditcard_num][personalinfo],
 28                 creditcard_data[creditcard_num][limit],
 29                 creditcard_data[creditcard_num][limitcash]
 30             ))
 31             break
 32         else:
 33             print(信用卡卡号:%s不存在 % creditcard_num)
 34 
 35 
 36 # 提现
 37 def withdraw():
 38     while True:
 39         with open(db_credit_info, r+) as f:
 40             creditcard_data = json.loads(f.read())
 41         creditcard_num = input(请输入需要提现的信用卡卡号(6位数字):).strip()
 42         if creditcard_num in creditcard_data.keys():
 43             password = input(请输入需要提现的信用卡密码(6位数字):).strip()
 44             if password == creditcard_data[creditcard_num][password]:
 45                 print(可用额度:% s\n取现额度:% s\n % (creditcard_data[creditcard_num][limit],
 46                                                 int((creditcard_data[creditcard_num]["limitcash"]*0.95))
 47                                                 ))
 48                 withdraw_money = input(请输入需要提现的金额(收取%5手续费)或返回b)
 49                 withdraw_money = int(withdraw_money)
 50                 if withdraw_money == b:
 51                     break
 52                 elif int(withdraw_money) <= creditcard_data[creditcard_num][limitcash]:
 53                     with open(db_credit_info, r) as f1:
 54                         new_limitcash = creditcard_data[creditcard_num][limitcash] -  55                                         (withdraw_money + 0.05 * withdraw_money)
 56                         new_limit = creditcard_data[creditcard_num][limit] - (withdraw_money+0.05*withdraw_money)
 57                         print(您已经成功提现人民币:¥%s,手续费:%s,可用额度:%s,取现额度:%s %
 58                               (withdraw_money, 0.05 * withdraw_money, new_limit, new_limitcash)
 59                               )
 60                         creditcard_data[creditcard_num][limit] = new_limit
 61                         creditcard_data[creditcard_num][limitcash] = int(new_limitcash)
 62                         data = json.dumps(creditcard_data)
 63                         f1.seek(0)
 64                         f1.write(data)
 65                         withdraw_log = [str(creditcard_data[creditcard_num]), 金额, str(withdraw_money), 提现成功]
 66                         message = "--".join(withdraw_log)
 67                         logger.info(message)
 68                         break
 69                 elif int(withdraw_money) >= creditcard_data[creditcard_num][limitcash]:
 70                     print("已经超出最大提现金额,请重试")
 71             else:
 72                 print(提现密码不正确,重新输入)
 73         else:
 74             print(系统没有次卡号,重新输入)
 75 
 76 
 77 # 转账
 78 def transfer():
 79     while True:
 80         with open(db_credit_info, r+) as f:
 81             creditcard_data = json.loads(f.read())
 82             creditcard_num = input(请输入信用卡卡号(6位数字):).strip()
 83             if creditcard_num in creditcard_data.keys():
 84                 trans_creditcard_num = input(请输入对方的信用卡卡号:)
 85                 if trans_creditcard_num in creditcard_data.keys():
 86                     transfer_money = input(请输入转账金额:)
 87                     transfer_money = int(transfer_money)
 88                     if transfer_money <= creditcard_data[creditcard_num][limint]:
 89                         password = input(请输入您的信用卡密码(6位数字):).strip()
 90                         if password == creditcard_data[creditcard_num][password]:
 91                             creditcard_data[creditcard_num][limit] -= transfer_money
 92                             creditcard_data[creditcard_num][limitcash] =  93                                 creditcard_data[creditcard_num][limit] // 2
 94                             creditcard_data[trans_creditcard_num][limit] += transfer_money
 95                             creditcard_data[trans_creditcard_num][limitcash] =  96                                 creditcard_data[trans_creditcard_num][limit] // 2
 97                             print(您已成功向信用卡:%s转账:%s\n可用额度:%s\n取现额度:%s\n
 98                                   % (trans_creditcard_num, transfer_money,
 99                                      creditcard_data[creditcard_num][limit],
100                                      creditcard_data[creditcard_num][limitcash]
101                                      )
102                                   )
103                             data = json.dumps(creditcard_data)
104                             f.seek(0)
105                             f.write(data)
106                             transfer_log = [对方卡号, trans_creditcard_num, 金额,
107                                             str(transfer_money), 信用卡转账成功
108                                             ]
109                             creditcard_record(creditcard_num, 转账%s % transfer_money)
110                             message = --.join(transfer_log)
111                             logger.info(message)
112                             break
113                         else:
114                             print(您的密码不正确,重新输入)
115                     else:
116                         print(该卡号%s 不存在,请重新输入 % trans_creditcard_num)
117                 else:
118                     print(该卡号%s不存在,重新输入 % creditcard_num)
119 
120 
121 # 还款
122 def repay():
123     while True:
124         with open(db_credit_info, r+) as f:
125             creditcard_data = json.loads(f.read())
126         creditcard_num = input(请输入需要还款的信用卡卡号(6位数字):).strip()
127         if creditcard_num in creditcard_data.keys():
128             repay_money = input(请输入您的还款金额:)
129             if repay_money.isdigit():
130                 repay_money = int(repay_money)
131                 password = input(请输入您的信用卡的还款密码)
132                 if password == creditcard_data[creditcard_num][password]:
133                     with open(db_credit_info, r+) as f2:
134                         creditcard_data[creditcard_num][limit] += repay_money
135                         creditcard_data[creditcard_num][limitcash] = creditcard_data136                         [creditcard_num][limit] // 2
137                         print(信用卡:%s已经成功还款,金额:%s\n新的可用额度:%s,新的取现额度:%s %
138                               (creditcard_num, repay_money, creditcard_data[creditcard_num][limit],
139                                creditcard_data[creditcard_num][limitcash]
140                                )
141                               )
142                         data = json.dumps(creditcard_data)
143                         f2.seek(0)
144                         f2.write(data)
145                         repay_log = [creditcard_data[creditcard_num][personalinfo],
146                                      creditcard_num, 还款, str(repay_money)
147                                      ]
148                         credit_record(creditcard_num, 还款%s % repay_money)
149                         message = --.join(repay_log)
150                         logger.info(message)
151                         break
152                 else:
153                     print("您的还款密码不正确,重新输入")
154             else:
155                 print(金额为数字,重新输入)
156         else:
157             print(该号%s不存在,重新输入 % creditcard_num)
158 
159 
160 # 申请信用卡
161 def apply_creditcard(limit=15000, locked=true):
162     while True:
163         with open(db_credit_info, r+, encoding=utf-8) as f:
164             creditcard_dic = json.loads(f.read())
165             for key in creditcard_dic.keys():
166                 print(系统已有信用卡:%s,持卡人:%s % (key, creditcard_dic[key][personalinfo]))
167             choice = input(是否申请信用卡,是y否b:)
168             if choice == y:
169                 creditcard = input(请输入要申请的信用卡卡号(6位数字):).strip()
170                 if creditcard not in creditcard_dic:
171                     if creditcard.isdigit() and len(creditcard) == 6:
172                         password = input(输入要申请的信用卡密码(6位数字):)
173                         if len(password) == 6:
174                             personalinfo = input(输入要申请的信用卡持卡人姓名:)
175                             if personalinfo.isalpha():
176                                 creditcard_dic[creditcard] = {creditcard: creditcard,
177                                                               password: password,
178                                                               personalinfo: personalinfo,
179                                                               limit: limit,
180                                                               limitcash: limit // 2,
181                                                               locked: locked
182                                                               }
183                                 dic = json.dumps(creditcard_dic)
184                                 f.seek(0)
185                                 f.write(dic)
186                                 print(信用卡:%s\n持卡人:%s\n可用额度:%s\n取现额度:%s申请成功 %
187                                       (creditcard, personalinfo, limit, limit//2)
188                                       )
189                                 apply_creditcard_log = [creditcard, personalinfo, 信用卡申请成功]
190                                 message = --.join(apply_creditcard_log)
191                                 logger.info(message)
192                             else:
193                                 print(无效字符)
194                         else:
195                             print(密码规则不符合)
196                     else:
197                         print(卡号不符合规则)
198             elif choice == b:
199                 break
200 
201 
202 # 信用卡绑定API
203 def credit_api():
204     while True:
205         with open(db_user, r+, encoding=utf-8) as f, open(db_credit_info, r+, encoding=utf-8) as f1:
206             user_dic = json.loads(f.read())
207             credit_dic = json.loads(f1.read())
208         username = input(请输入需要绑定的信用卡用户名或返回b)
209         if username in user_dic.keys():
210             creditcard_num = user_dic[username][creditcard]
211             if creditcard_num == 0:
212                 print(当前用户%s没有绑定信用卡 % username)
213                 attach_creditcard = input("请输入需要绑定的信用卡卡号:")
214                 if username == credit_dic[attach_creditcard][personalinfo]:
215                     attach_password = input(请输入需要绑定的信用卡密码:)
216                     with open(db_user, r+, encoding=utf-8) as f:
217                         if attach_password == credit_dic[attach_password][password]:
218                             user_dic[username][creditcard] = attach_password
219                             dic = json.dumps(user_dic)
220                             f.seek(0)
221                             f.write(dic)
222                             print(持卡人:%s\n信用卡号:%s\n已成功绑定 % (username, attach_password))
223                             attach_log = [username, attach_creditcard, 信用卡绑定成功]
224                             message = --.join(attach_log)
225                             logger.info(message)
226                             break
227                         else:
228                             print(密码错误,请重新输入)
229                 else:
230                     print(信用卡用户名不存在,请重新输入)
231             else:
232                 print(当前用户%s有绑定信用卡 % username)
233                 break
234 
235 
236 # 信用卡付款API
237 def credit_pay(sum, login_user):
238     print(尊敬的信用卡绑定用户,欢迎来到信用卡结算中心.center(50, -))
239     with open(db_credit_info, r+, encoding=utf-8) as credit_payment, open(db_user, r+, encoding=utf-8) as f:
240         credit_dic = json.loads(credit_payment.read())
241         user_dic = json.loads(f.read())
242         pwd = input(请输入信用卡密码完成支付:)
243         creditcard_num = str(user_dic[login_user][creditcard])
244         limit = credit_dic[creditcard_num][limit]
245         limitcash = credit_dic[creditcard_num][limitcash]
246         if pwd == credit_dic[creditcard_num][password]:
247             credit_dic[creditcard_num][limit] = limit - sum
248             credit_dic[creditcard_num][limitcash] = limitcash - sum
249             print(信用卡:%s已经付款成功:¥%s % (creditcard_num, sum))
250             data = json.dumps(credit_dic)
251             credit_payment.seek(0)
252             credit_payment.write(data)
253             shopping_log = [login_user, creditcard_num, 信用卡结账, str(sum)]
254             credit_record(creditcard_num, 购物支付%s % sum)
255             message = --.join(shopping_log)
256             logger.info(message)
257         else:
258             print(支付密码不对,请重新输入)
259 
260 
261 # 信用卡流水创建
262 def creditcard_record(creditcard_num, record):
263     with open(credit_record, r+) as f1:
264         record_dict = json.loads(f1.read())
265         month = time.strftime(%Y-%m-%d, time.localtime())
266         times = time.strftime(%H:%M:%S)
267         if str(creditcard_num) not in record_dict.keys():
268             record_dict[creditcard_num] = {month: {times:record}}
269         else:
270             if month not in record_dict[creditcard_num].keys():
271                 record_dict[creditcard_num][month]= {times: record}
272             else:
273                 record_dict[creditcard_num][month][times] = record
274         dict = json.dumps(record_dict)
275         f1.seek(0)
276         f1.write(dict)
277 
278 
279 # 信用卡流水查询
280 def credit_check(creditcard_num):
281     while True:
282         print(信用卡流水单.center(50, -))
283         with open(credit_record, r+) as f1:
284             record_dict = json.loads(f1.read())
285             print(流水单日期)
286             if creditcard_num in record_dict.keys():
287                 for key in record_dict[creditcard_num]:
288                     print(key)
289                 date = input(流水单查询,返回b, 输入流水单日期2018-06-03)
290                 if date == b: break
291                 if date in record_dict[creditcard_num].keys():
292                     keys = record_dict[creditcard_num][date]
293                     print(当前信用卡 %s 交易记录>>> % creditcard_num)
294                     for key in keys:
295                         print(时间:%s   %s % (key, record_dict[creditcard_num][date][key]))
296                 else:
297                     print(输入日期有误)
298             else:
299                 print(信用卡%s还没有进行过消费 % creditcard_num)
300                 break
creditcard_center
技术分享图片
  1 import os, sys, json, logging
  2 
  3 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  4 sys.path.append(BASE_DIR)
  5 
  6 db_user = BASE_DIR + r"/database/user"  # 存放用户信息
  7 db_credit_info = BASE_DIR + r"/database/creditcard_info"  # 存放信用卡信息
  8 user_log = BASE_DIR + r"/log/user_flowlog"  # 存放日志信息
  9 
 10 
 11 # 添加用户
 12 def add_user(creditcard=0, locked=true):
 13     while True:
 14         print("添加用户".center(50, -))
 15         with open(db_user, r+, encoding=utf-8) as f:  # encoding为编码的操作,将数据按照utf-8加载到内存
 16             user_dic = json.loads(f.read())
 17             for key in user_dic:
 18                 print("系统已经有用户%s" % key)
 19             choice = input("是否添加用户,是y,返回b")
 20             if choice == "y":
 21                 username = input(请输入要注册的用户名:).strip()  # 去除输入后的空格
 22                 if username not in user_dic:
 23                     if username.isalpha():
 24                         password = input("请输入密码:")
 25                         if len(password) > 0:
 26                             user_dic[username] = {username: username, password: password,
 27                                                   creditcard: creditcard, locked: locked}
 28                             dic = json.dumps(user_dic)
 29                             f.seek(0)
 30                             f.write(dic)
 31                             print(用户:%s添加成功 % username)
 32                         else:
 33                             print(密码不能为空)
 34                     else:
 35                         print(账户只能为字母组合)
 36                 else:
 37                     print(该账户已经注册)
 38             elif choice == b:
 39                 break
 40 
 41 
 42 # 锁定用户
 43 def lock_user():
 44     while True:
 45         print(锁定用户.center(50, -))
 46         with open(db_user, r+, encoding=utf-8) as f:
 47             user_dic = json.loads(f.read())
 48             for key in user_dic:
 49                 if user_dic[key][locked] == false:
 50                     print(用户%s未锁定 % key)
 51                 else:
 52                     print(用户%s未锁定 % key)
 53             choice = input("是否进行锁定,是y或返回b")
 54             if choice == y:
 55                 loc_user = input("请输入要锁定的用户名:")
 56                 if loc_user in user_dic.keys:
 57                     if user_dic[loc_user][locked] == false:
 58                         user_dic[loc_user][locked] = true
 59                         dic = json.dumps(user_dic)
 60                         f.seek(0)
 61                         f.write(dic)
 62                         print(用户 %s 锁定成功 % loc_user)
 63                     else:
 64                         print(用户 %s 锁定失败,已经锁定! % loc_user)
 65                 else:
 66                     print(用户 %s 不存在 % loc_user)
 67             elif choice == b:
 68                 break
 69 
 70 
 71 # 冻结信用卡
 72 def lock_creditcard():
 73     while True:
 74         print(冻结信用卡.center(50, -))
 75         with open(db_credit_info, r+, encoding=utf-8) as f:
 76             creditcard_dic = json.loads(f.read())
 77             for key in creditcard_dic:
 78                 if creditcard_dic[key][locked] == false:
 79                     print(信用卡 %s 未冻结 % key)
 80                 else:
 81                     print(信用卡 %s 已冻结 % key)
 82             choice = input(是否对信用卡进行冻结操作,是y或返回b:)
 83             if choice == y:
 84                 loc_creditcard = input(请输入要冻结的信用卡卡号(6位数字))
 85                 if loc_creditcard in creditcard_dic.keys() and len(loc_creditcard) == 6:
 86                     if creditcard_dic[loc_creditcard][locked] == false:
 87                         creditcard_dic[loc_creditcard][locked] = true
 88                         dic = json.dumps(creditcard_dic)
 89                         f.seek(0)
 90                         f.write(dic)
 91                         print(信用卡 %s 冻结成功 % loc_creditcard)
 92                     else:
 93                         print(信用卡 %s 冻结失败,已经冻结 % loc_creditcard)
 94                 else:
 95                     print(信用卡 %s 不存在! % loc_creditcard)
 96             elif choice == b:
 97                 break
 98 
 99 
100 # 解冻信用卡
101 def unlock_creditcard():
102     while True:
103         print(解冻信用卡.center(50, -))
104         with open(db_credit_info, r+, encoding=utf-8) as f:
105             creditcard_dic = json.loads(f.read())
106             for key in creditcard_dic:
107                 if creditcard_dic[key][locked] == false:
108                     print(信用卡 %s 未冻结 % key)
109                 else:
110                     print(信用卡 %s 已冻结 % key)
111                 choice = input(是否对信用卡进行解冻操作,是y或返回b:)
112                 if choice == y:
113                     unloc_creditcard = input(请输入要解冻的信用卡号(6位数字):)
114                     if unloc_creditcard in creditcard_dic.keys() and len(unloc_creditcard) == 6:
115                         if creditcard_dic[unloc_creditcard][locked] == true:
116                             creditcard_dic[unloc_creditcard][locked] = false
117                             dic = json.dumps(creditcard_dic)
118                             f.seek(0)
119                             f.write(dic)
120                             print(信用卡 %s 解冻成功! % unloc_creditcard)
121                         else:
122                             print(信用卡 %s 解冻失败,已经解冻! % unloc_creditcard)
123                     else:
124                         print(信用卡 %s 不存在 % unloc_creditcard)
125                 elif choice == b:
126                     break
127 
128 
129 # 更新密码
130 def update_password(login_user):
131     while True:
132         with open(db_user, r+, encoding=utf-8) as f:
133             user_dic = json.loads(f.read())
134             print(当前用户账户:%s\n当前用户密码:%s % (login_user, user_dic[login_user][password]))
135             choice = input(是否修改当前密码,是y,返回按b:)
136             if choice == y:
137                 old_pwd = input(请输入%s的原密码: % login_user)
138                 if old_pwd == user_dic[login_user][password]:
139                     new_pwd = input(请输入%s的新密码: % login_user)
140                     user_dic[login_user][password] = new_pwd
141                     user = json.dumps(user_dic)
142                     f.seek(0)
143                     f.write(user)
144                     print(%s密码修改成功 % login_user)
145                     break
146                 else:
147                     print(%s 原密码不正确,请重新输入! % login_user)
148             elif choice == b:
149                 break
150 
151 
152 # 日志文件
153 def get_logger():
154     logger = logging.getLogger()
155     logger.setLevel(logging.INFO)
156 
157     fh = logging.FileHandler(user_log)
158     fh.setLevel(logging.INFO)
159     formatter = logging.Formatter(%(asctime)s - %(message)s)
160     fh.setFormatter(formatter)
161     logger.addHandler(fh)
162     return logger
admin_center
技术分享图片
 1 import os, sys, json
 2 
 3 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
 4 db_user = BASE_DIR + r/database/user
 5 db_credit_info = BASE_DIR + r/database/creditcard_info
 6 db_credit_record = BASE_DIR +r/database/creditcard_record
 7 db_user_log = BASE_DIR + r/database/user_flowlog
 8 
 9 
10 # 认证装饰器
11 def auth(auth_type):
12     def outer_wrapper(func):
13         if auth_type == user_auth:
14             def wrapper():
15                 #用户认证
16                 func()
17                 with open(db_user, r) as f_user:
18                     user_list = json.loads(f_user.read())
19                     while True:
20                         user = input(请输入用户名:).strip()
21                         pwd = input(请输入密码:).strip()
22                         if user and pwd:
23                             for key in user_list:
24                                 if user == user_list[key][username] and pwd == user_list[key][password]:
25                                     if user_list[user][locked] == false:
26                                         print(用户%s认证成功 % user)
27                                         return user
28                                     else:
29                                         print(用户%s已经被锁定,认证失败 % user)
30                                 else:
31                                     print(用户名或密码错误,认证失败)
32                             else:
33                                 print(用户名和密码不能为空)
34             return wrapper
35         elif auth_type == creditcard_auth:
36             def wrapper():
37                 func()
38                 with open(db_credit_info, r) as f:
39                     creditcard_data = json.loads(f.read())
40                     while True:
41                         credit_card = input(请输入信用卡号(6位数字):).strip()
42                         password = input(请输入信用卡密码(6位数字):).strip()
43                         if credit_card and password:
44                             for key in creditcard_data:
45                                 if credit_card == creditcard_data[key][creditcard]:
46                                     if password == creditcard_data[key][password]:
47                                         if creditcard_data[key][locked] == false:
48                                             print(信用卡%s验证成功 % credit_card)
49                                             return credit_card
50                                         else:
51                                             print(信用卡%s已经被冻结,请使用其它信用卡 % credit_card)
52                                     else:
53                                         print(信用卡卡号或密码输入错误)
54                         else:
55                             print(信用卡账号输入不能为空)
56             return wrapper
57         elif auth_type == admin_auth:  # 管理员认证
58             def wrapper():
59                 func()
60                 admin_dic = {admin: admin, password: admin}
61                 while True:
62                     admin_name = input(请输入管理员账号:).strip()
63                     admin_pwd = input(请输入密码:).strip()
64                     if admin_name and admin_pwd:
65                         if admin_name in admin_dic and admin_pwd == admin_dic[password]:
66                             print(管理员账号%s登陆成功 % admin_name)
67                             return admin_name
68                         else:
69                             print(账号或密码错误)
70                     else:
71                         print(管理员账号不能为空)
72             return wrapper
73     return outer_wrapper
74 
75 
76 @auth(auth_type=user_auth)
77 def user_auth():
78     print(用户登录认证.center(50, -))
79 
80 
81 @auth(auth_type=creditcard_auth)
82 def creditcard_auth():
83     print(信用卡登录认证.center(50, -))
84 
85 
86 @auth(auth_type=admin_auth)
87 def admin_auth():
88     print(管理员登录认证.center(50, -))
authenticate

4 core目录中的文件

技术分享图片
  1 import os, sys
  2 
  3 # 获取程序的主目录
  4 BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  5 # 将主目录添加到环境变量
  6 sys.path.append(BASE_DIR)
  7 from modules import admin_center, creditcard_center, shopping_center, authenticate
  8 
  9 
 10 db_user = BASE_DIR + r/database/user
 11 
 12 
 13 def option_personal(login_user):
 14     while True:
 15         option_list = [历史购物记录, 修改登录密码, 信用卡绑定, 返回]
 16         count = 0
 17         for option in option_list:
 18             count += 1
 19             print(str(count) + . + option)
 20         user_choice = input(>>>:)
 21         if user_choice.isdecimal():
 22             if int(user_choice) == 1:
 23                 print(历史购物记录.center(50, -))
 24                 shopping_center.check_record(login_user)
 25                 break
 26             elif int(user_choice) == 2:
 27                 print(修改登录密码.center(50, -))
 28                 admin_center.update_password(login_user)
 29             elif int(user_choice) == 3:
 30                 print(信用卡绑定.center(50, -))
 31                 creditcard_center.credit_api()
 32                 break
 33             elif int(user_choice) == 4:
 34                 print(返回.center(40, -))
 35         else:
 36             print(无效的字符)
 37 
 38 
 39 # 购物
 40 def option_shopping(login_user):
 41     while True:
 42         print(购物中心.center(40, -))
 43         option_list = ["购物", "查看购物车", "购物结算", "个人中心", "返回"]
 44         count = 0
 45         for option in option_list:
 46             count += 1
 47             print(str(count) + . + option)
 48         user_choice = input(>>>:)
 49         if user_choice.isdecimal():
 50             if int(user_choice) == 1:
 51                 print(购物.center(50, -))
 52                 shopping_center.shopping_mall()
 53             elif int(user_choice) == 2:
 54                 print(查看购物车.center(50, -))
 55                 shopping_center.shopping_cart()
 56             elif int(user_choice) == 3:
 57                 print(购物结算.center(50, -))
 58                 shopping_center.shopping_pay(login_user)
 59             elif int(user_choice) == 4:
 60                 print(个人中心.center(50, -))
 61                 option_personal(login_user)
 62             elif int(user_choice) == 5:
 63                 print(返回.center(50, -))
 64                 break
 65         else:
 66             print(无效字符)
 67 
 68 
 69 # 信用卡
 70 def option_creditcard(credit_card):
 71     while True:
 72         print(信用卡中心.center(50, -))
 73         option_list = ["信用卡信息", "提现", "转账", "还款","流水记录", "申请信用卡", "返回"]
 74         count = 0
 75         for option in option_list:
 76             count += 1
 77             print(str(count) + . + option)
 78         user_choice =  input(>>>:)
 79         if user_choice.isdecimal():
 80             if int(user_choice) == 1:
 81                 print("信用卡信息".center(50, "-"))
 82                 creditcard_center.creditcard_info()
 83             elif int(user_choice) == 2:
 84                 print("提现".center(50, "-"))
 85                 creditcard_center.withdraw()
 86             elif int(user_choice) == 3:
 87                 print("转账".center(50, "-"))
 88                 creditcard_center.transfer()
 89             elif int(user_choice) == 4:
 90                 print("还款".center(50, "-"))
 91                 creditcard_center.repay()
 92             elif int(user_choice) == 5:
 93                 print("流水记录".center(50, "-"))
 94                 creditcard_center.credit_check(credit_card)
 95             elif int(user_choice) == 6:
 96                 print("申请信用卡".center(50, "0"))
 97                 creditcard_center.apply_creditcard(limit=15000, locked="true")
 98             elif int(user_choice) == 7:
 99                 print("返回".center(50, "-"))
100                 break
101         else:
102             print("无效的字符")
103 
104 
105 # 后台管理
106 def option_backadmin(user):
107     while True:
108         print(后台管理.center(50, -))
109         option_list = ["添加账户", "锁定账户", "解锁账户", "冻结信用卡","解冻信用卡", "返回"]
110         count = 0
111         for option in option_list:
112             count += 1
113             print(str(count) + . + option)
114         user_choice = input(>>>:)
115         if user_choice.isdecimal():
116             if int(user_choice) == 1:
117                 print("添加账户".center(50, "-"))
118                 admin_center.add_user()
119             elif int(user_choice) == 2:
120                 print("锁定账户".center(50, "-"))
121                 admin_center.lock_user()
122             elif int(user_choice) == 3:
123                 print("解锁账户".center(50, "-"))
124                 admin_center.unlock_user()
125             elif int(user_choice) == 4:
126                 print("冻结信用卡".center(50, "-"))
127                 admin_center.lock_creditcard()
128             elif int(user_choice) == 5:
129                 print("解冻信用卡".center(50, "-"))
130                 admin_center.unlock_creditcard()
131             elif int(user_choice) == 6:
132                 print("返回".center(50, "-"))
133                 break
134         else:
135             print("无效的字符")
136 
137 
138 # 认证模块
139 while True:
140     print("欢迎进入信用卡网购程序".center(50, "-"))
141     option_list = ["购物中心", "信用卡中心", "后台管理", "退出"]
142     count = 0
143     for option in option_list:
144         count += 1
145         print(str(count) + . + option)
146     user_choice = input(">>>:")
147     if user_choice.isdecimal():
148         if int(user_choice) == 1:
149             option_shopping(authenticate.user_auth())
150         elif int(user_choice) == 2:
151             option_creditcard(authenticate.creditcard_auth())
152         elif int(user_choice) == 3:
153              option_backadmin(authenticate.admin_auth())
154         elif int(user_choice) == 4:
155             print("再见!")
156             break
157     else:
158         print("无效的字符")
main

5 database目录中文件

技术分享图片
1 { }
user
技术分享图片
1 # 默认为空
shopping_record
技术分享图片
1 # 默认为空
shopping_cart
技术分享图片
1 IPhone 8800
2 Mac-Pro 12388
3 IPad 4999
4 Mouse 181
5 Bike 800
6 keyboard 199
7 Dell-Screen 5999
8 Nike-Watch 999
production_list
技术分享图片
1 # 默认为空
creditcard_record
技术分享图片
1 { }
creditcard_info

6 log目录文件

技术分享图片
1 # 默认为空
user_flowlog

7 个人理解

  编程序,要多动手,才会有更多的收获。光靠看去思考,转瞬就忘,不会有长久的记忆,更谈不上有深刻的理解。

  自己写的或者抄写的代码,也要经常回头看看,加强自己的理解。

  函数之间,要有两个空行。

  换行适用“\”符号,但是如果是括号内换行,则可不使用。

  使用条件语句时,如有必要,if于对应的else要成对出现。

  要深刻理解python语言的思想精髓,打好自己的基础。

  复杂的程序要有模块化的思维,任何程序要确定数据结构(是列表,还是字典),其次是要实现的方法(动作),最后是业务逻辑。

8 思路来源

  http://www.cnblogs.com/lianzhilei/p/5786223.html

  https://www.cnblogs.com/Mr-hu/articles/7423088.html

  https://www.jianshu.com/p/734931a76bc1

  https://www.cnblogs.com/garrett0220/articles/6861418.html

python_项目_ATM和购物商城的程序

标签:odi   out   card   check   出现   截断   oar   中心   完成   

原文地址:https://www.cnblogs.com/pythonproject/p/9112221.html

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