标签:python
一、简单说明
1、源代码文件见附件 Credit.zip
2、关于转账功能,因时间问题,转账功能待续
4、逻辑图
二、代码
1、包encryption中的(password.py文件)
#!/usr/bin/env python # -*- coding: utf-8 -*- """ Created on Thu Nov 10 14:47:29 2016 密码相关功能 @author: toby """ import hashlib, re #密码进行MD5加密 def md5(passwd): hashs = hashlib.md5() #创建MD5对象 hashs.update(passwd) #生成加密串 return hashs.hexdigest() #获取加密串 #密码位数检测,只能是6位数字 def check_digit(passwd): com = re.compile(‘^\d{6,6}$‘) temps = com.findall(passwd) if len(temps) == 0: return False else: return True #检测银行卡号位数 def check_card(card): com = re.compile(‘^\d{16,16}$‘) temps = com.findall(card) if len(temps) == 0: return False else: return True
2、applys.py
#!/usr/bin/env python # -*- coding: utf-8 -*- """ Created on Thu Nov 10 14:42:29 2016 卡号申请程序 @author: toby """ from encryption import password import shelve, random, getpass, time #申请资格评估 def ssessment(income,age): if income >= 3000 and age >= 22 and age < 60: return True else: return False #确定额度 def quota(money): if money > 3000 and money < 6000: return 5000 if money > 6000 and money < 10000: return 10000 if money > 10000 and money < 20000: return 15000 #生成卡号 def carid(): carid = "" for i in range(16): temp = random.randint(0,9) carid += str(temp) return carid #新申请的用户数据入库 def examine(key,val): data = shelve.open(‘data.db‘) data[key] = val if __name__ == "__main__": print ‘‘‘ 信用卡申请资格评估 ‘‘‘ income = input(‘月收入:‘) age = input(‘年龄:‘) #评估申请资格 if ssessment(income,age) == False: print ‘评估结果:无申请资格‘ else: print ‘‘‘ 评估结果:可申请 请填写详细资料 ‘‘‘ name = raw_input(‘姓名:‘) card = raw_input(‘身份证:‘) phone = raw_input(‘手机:‘) address = raw_input(‘地址:‘) #根据条件自动生成固定额度 fixed_amount = quota(income) #调用自动生成额度函数 #初次申请卡号,初始记录还款的key为0 repayment_amount = 0 #初始化还款金额,首次为0元 #自动生成卡号 kaid = carid() #调用自动生成16位卡号函数 while True: passwd1 = getpass.getpass(‘设置密码:‘) passwd2 = getpass.getpass(‘确认密码:‘) if passwd1 != passwd2: print ‘密码不一致‘ continue if password.check_digit(passwd1) == False: #检测密码合法性 print ‘只能输入6位数字密码‘ continue else: print ‘------卡号申请成功!------‘ print ‘卡号为: %s‘ % kaid print ‘可用额度: %s‘ % fixed_amount date = time.strftime(‘%Y-%m-%d %H:%M:%S‘) #记录申请时间 repayment_time = time.strftime(‘%Y-%m-%d‘) #申请通过当天日期为还款日 passwd_data = password.md5(passwd1) #密码使用MD5加密 temp = {‘date‘:date,‘name‘:name,‘age‘:age,‘income‘:income,‘card‘:card,‘phone‘:phone,‘address‘:address,‘passwd‘:passwd_data,‘fixed_amount‘:fixed_amount,‘temp_money‘:int(fixed_amount),‘repayment_amount‘:repayment_amount,‘repayment_time‘:repayment_time} examine(kaid,temp) #调用数据入库函数进行将新申请的数据存入模拟数据库 break
3、login.py
# -*- coding: utf-8 -*- """ Created on Fri Nov 11 21:35:04 2016 模拟取款机登录入口程序 @author: toby """ from encryption import password import getpass from data_function import * if __name__ == "__main__": try: while True: print ‘‘‘ ------银行系统登录入口------ ‘‘‘ card_number = raw_input(‘卡号:‘) card_password = getpass.getpass(‘口令:‘) if password.check_card(card_number) == False: print ‘卡号位数不正确,请仔细输入‘ continue if check_card_active(card_number) == False: print ‘对不起,该卡号不存在‘ continue if password.check_digit(card_password) == False: print ‘密码无效‘ continue passwd_md5 = password.md5(card_password) #将输入的密码进行加密,用于和数据库中的md5密码匹配 db_passwd = get_data(card_number,‘passwd‘) #从数据库读取对应卡号的密码 if passwd_md5 != db_passwd: #输入的密码进行加密后和从数据库中读取的MD5密码串进行配对 print ‘密码错误‘ continue else: while True: print ‘‘‘ XX银行欢迎您 1 ----------------- 取现 2 ----------------- 查询 3 ----------------- 还款 4 ----------------- 转账 5 ----------------- 退出 ‘‘‘ main_menu = input(‘选择您需要的操作(1-5):‘) if main_menu == 1: cash_amount = input(‘取款金额:‘) print ‘‘‘ 温馨提示:信用卡取现需收取5%手续费 ‘‘‘ action = raw_input(‘是否继续套现(y/n):‘) if action == ‘y‘: if cash_debit(card_number,int(cash_amount)): #调用套现接口 print ‘请提取现金‘ continue else: print ‘机器故障,无法提取‘ break if action == ‘no‘: continue if main_menu == 2: print ‘‘‘ 1 ----------------- 查询余额 2 ----------------- 查询明细 ‘‘‘ sub_menu = input(‘选择您需要的操作(1-2):‘) if sub_menu == 1: get_all_data(card_number) action = raw_input(‘是否返回主菜单(y):‘) if action == ‘y‘: continue if sub_menu == 2: get_water(card_number) #调用查询流水函数 action = raw_input(‘是否返回主菜单(y):‘) if action == ‘y‘: continue if main_menu == 3: temp_money = get_data(card_number,‘temp_money‘) #读取可用金额 repayment_amount = get_data(card_number,‘repayment_amount‘) #读取需还款金额 print ‘‘‘ 可用余额: %s (人民币) 您需还款: %s (人民币) ‘‘‘ % (temp_money,repayment_amount) money = input(‘还款金额:‘) if repayment_interface(card_number,int(money)) == True: print ‘还款成功‘ else: print ‘还款失败‘ action = raw_input(‘是否返回主菜单(y):‘) if action == ‘y‘: continue if main_menu == 4: print ‘转账功能正在开发‘ action = raw_input(‘是否返回主菜单(y):‘) if action == ‘y‘: continue if main_menu == 5: print ‘柜员机系统已退出‘ break continue except Exception,e: print Exception,":::-->",e
4、data_function.py
# -*- coding: utf-8 -*- """ Created on Fri Nov 11 16:10:05 2016 数据操作相关功能 #我的卡号:1098521606032848 @author: toby """ from encryption import password import shelve, getpass, time #数据库读取操作,这是一个全局动作 f = shelve.open(‘data.db‘) #检测卡号是否存在 def check_card_active(card_number): if f.get(card_number,None) != None: #判断对应卡号是否存在 return True else: return False #检测对应卡号中对应的关键字数据,例如检测对应卡号密码 def get_data(card,keys): data = f[card] return data[keys] #查询对应卡号的所有数据 def get_all_data(card): data = f[card] print ‘‘‘ 卡号: %s 户名: %s 固定额度: %s (人民币) 可用余额: %s (人民币) 需还款: %s (人民币) 还款日: %s ‘‘‘% (card,data[‘name‘],data[‘fixed_amount‘],data[‘temp_money‘],data[‘repayment_amount‘],data[‘repayment_time‘]) #扣款通用接口,正常POS机消费或者网银支付 ,无需手续费 def pos_smart_pay(card_number,card_password,price): if check_card_active(card_number) == False: print ‘对不起,卡号有误!‘ return False passwd_md5 = password.md5(card_password) #将输入的密码进行加密,用于和数据库中的md5密码匹配 db_passwd = get_data(card_number,‘passwd‘) #从数据库读取对应卡号的密码 if passwd_md5 != db_passwd: print ‘支付密码错误‘ return False else: data = f[card_number] #读取对应卡号信息 existing = data[‘temp_money‘] #读取现有金额 if price > existing: print ‘对不起,余额不足‘ else: result = existing - price #现有金额减去正常消费价格 data[‘temp_money‘] = int(result) #临时存储计算结果 f[card_number] = data #计算结果数据入库 current_repayment_amoount = data[‘repayment_amount‘] #读取现有还款金额 res = current_repayment_amoount + price #现有还款金额 + 当前消费金额 = 需还款的金额 data[‘repayment_amount‘] = int(res) f[card_number] = data #需还款金额数据入库 action = "POS消费" record_water(card_number,action,price) f.close() return True #取款机取款功能 需5%手续费 def cash_debit(card_number,money): data = f[card_number] #读取对应卡号信息 existing = data[‘temp_money‘] #读取现有可用金额 if money > existing: print ‘‘‘ 对不起,余额不足,无法套现 您当前可用金额: %s ‘‘‘ % existing else: repayment = money * 0.05 #套现时计算手续费 print ‘‘‘ 取款金额: %s 手续费: %s ‘‘‘ % (money, repayment) result = existing - money - repayment #现有金额-套现金额(再减去手续费) data[‘temp_money‘] = int(result) #临时存储计算结果 f[card_number] = data #计算结果数据入库 current_repayment_amoount = data[‘repayment_amount‘] #读取现有还款金额 temporary = current_repayment_amoount + money + (money * 0.05) #现有还款金额 + 当前套现金额+手续费 data[‘repayment_amount‘] = int(temporary) f[card_number] = data #需还款金额数据入库 #调用记录流水的函数进行记录流水 action = "取款机套现" record_water(card_number,action,money) f.close() return True #通用还款接口 def repayment_interface(card_number,money): try: data = f[card_number] current_repayment_amoount = data[‘repayment_amount‘] #读取需还款金额 data[‘repayment_amount‘] = current_repayment_amoount - money #需还款金额 - 传入进来的还款额 = 剩余还款额(全部还清后这里为0元,还多少减多少) f[card_number] = data #剩余还款额数据入库 existing = data[‘temp_money‘] #读取现有可用金额 data[‘temp_money‘] = existing + money #现有可用金额 + 已传入进来的需还金额 (累加到可用金额,还多少就会累加多少) f[card_number] = data #还款后累加可用后的金额数据入库 action = "还款" record_water(card_number,action,money) f.close() return True except Exception,e: print Exception,":::-->",e #通用记录流水接口 def record_water(card_number,action,money): water = shelve.open(‘water_data/%s.list‘ % card_number) date = time.strftime(‘%Y-%m-%d %H:%M:%S‘) water[date] = "行为:%s, 金额:%s" % (action,money) water.close() #通用查询流水接口 def get_water(card_number): water = shelve.open(‘water_data/%s.list‘ % card_number) for val in water: print val, water[val]
5、payment_interface.py
# -*- coding: utf-8 -*- """ Created on Sat Nov 12 13:47:03 2016 模拟第三方支付 @author: toby """ import getpass from data_function import * #第三方支付接口 while True: payment_card = raw_input(‘支付卡号:‘) payment_price = input(‘价格:‘) payment_password = getpass.getpass(‘支付密码:‘) if pos_smart_pay(payment_card,payment_password,int(payment_price)) == True: print ‘支付完成‘ break else: print ‘支付失败‘ continue
本文出自 “FA&IT运维-Q群:223843163” 博客,请务必保留此出处http://freshair.blog.51cto.com/8272891/1872529
python 之简单模拟银行系统功能(卡号申请、还款、支付、取现)
标签:python
原文地址:http://freshair.blog.51cto.com/8272891/1872529