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

  1. 毕业设计代做,各种系统微服务项目ssm项目,员工管理系统,微信小程序,购物商城,二手商城系统,销售系统,等等

    毕业设计代做,各种系统,微服务项目,ssm项目 小程序,商城等,期末作业等都可以,价格好说,长期接单, 有项目说明书,软件介绍相关文档,答辩的时候包过,知识点对接好,给你讲解等, 毕业设计代做,各种系 ...

  2. 微信小程序购物商城系统开发系列

    微信小程序购物商城系统开发系列 微信小程序开放公测以来,一夜之间在各种技术社区中就火起来啦.对于它 估计大家都不陌生了,对于它未来的价值就不再赘述,简单一句话:可以把小程序简单理解为一个新的操作系统. ...

  3. 2.2 - ATM+购物商城程序

    要求:模拟实现一个ATM + 购物商城程序1.额度 15000或自定义2.实现购物商城,买东西加入 购物车,调用信用卡接口结账3.可以提现,手续费5%4.支持多账户登录5.支持账户间转账6.记录每月日 ...

  4. java 购物商城小项目训练

    java web 模拟购物车练习(项目一) 首页(index.jsp) <div align="center" class="index"> < ...

  5. 项目1:ATM+购物商城项目

    项目1:ATM+购物商城 1.项目介绍 项目需求: # 项目需求如下:'''- 额度 15000或自定义​- 实现购物商城,买东西加入购物车,调用信用卡接口结账​- 可以提现,手续费5%​- 支持多账 ...

  6. 微信小程序购物商城系统开发系列-目录结构

    上一篇我们简单介绍了一下微信小程序的IDE(微信小程序购物商城系统开发系列-工具篇),相信大家都已经蠢蠢欲试建立一个自己的小程序,去完成一个独立的商城网站. 先别着急我们一步步来,先尝试下写一个自己的 ...

  7. Python开发程序:ATM+购物商城

    一.程序要求 模拟实现一个ATM + 购物商城程序 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 每月22号出账单,每月10号为还款日,过期未还 ...

  8. 模拟实现一个ATM+购物商城程序

    记得上次小编上传了一个购物车程序,这次呢稍微复杂一点,还是那句话,上传在这里不是为了炫耀什么,只是督促小编学习,如果大神有什么意见和建议,欢迎指导.(PS:本次主要参考学习为主,自己原创的很少) 要求 ...

  9. 模拟实现ATM+购物商城程序

    流程图: 需求: ATM:模拟实现一个ATM + 购物商城程序额度 15000或自定义实现购物商城,买东西加入 购物车,调用信用卡接口结账可以提现,手续费5%支持多账户登录支持账户间转账记录每月日常消 ...

随机推荐

  1. 怎样检测TCP/UDP端口的连通性

    1 TCP端口的连通性 TC端口的连通性,一般通过telnet检测: TCP协议是面向连接的,可以直接通过telnet命令连接 telnet host-ip port 2 UDP端口的连通性 因为公司 ...

  2. MYSQL列表中常用语句代码块

    查看数据表是否存在:SHOW TABLES; 显示已经打开的数据库:SELECT DATABASE(); 查看数据表结构:SHOW COLUMNS FROM ***(数据表名): 插入数据:INSER ...

  3. Power BI 关注博客更新

    原本当你访问你常用的数据库时候,该库的新增,修改,删除,通过PowerBI都很容易发现,但是在Web上面,通过PowerBI来发现Web修改就没那么容易了. 现在,我想通过PowerBI的报告来显示某 ...

  4. pytorch怎么抽取中间的特征或者梯度

    for i, (input, target) in enumerate(trainloader): # measure data loading time data_time.update(time. ...

  5. Android版本28使用http请求

    Android版本28使用http请求报错not permitted by network security policy android模拟器调试登录的时候报错 CLEARTEXT communic ...

  6. 解决Maven web 项目 Cannot detect Web Project version. Please specify version of Web Project through ... 的错误

    创建maven项目的时候,maven文件头报错: Cannot detect Web Project version. Please specify version of Web Project th ...

  7. ajango--orm操作

    一 必知必会13条: 返回对象列表的: 1.all() :查所有 2.filter() :查一行 3.exclude():排除(里面可以写条件,意思除了这个条件的所有) 4.order_by():默认 ...

  8. Hibernate5.3 + mysql8.0遇到的问题

    今天学习Hibernate看的是旧版本的视频Hibernate4.0版本 遇到几个新旧版本的区别. 1.方言,这个是因为SQL不是因为Hibernate 新版方言 2.将编译的类配置进congifur ...

  9. win10 + ubuntu 16.04 双系统安装

    第一次写博客,有错的请指教emmmm 这是因为老师的要求在做课程设计,要用到ubuntu环境,对于这个来说,学长说的是14 16 18都很稳定,但是他在用16.04所以我也用的ubuntu16.04方 ...

  10. p132程序代码解析

    1.  long before = System.currentTimeMillis(); ...... long after = System.currentTimeMillis(); 解析:该两句 ...