python_项目_ATM和购物商城的程序
Posted Artisan正传
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python_项目_ATM和购物商城的程序相关的知识,希望对你有一定的参考价值。
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
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
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(以上是关于python_项目_ATM和购物商城的程序的主要内容,如果未能解决你的问题,请参考以下文章