python实现银行管理系统


Posted in Python onOctober 25, 2019

python实现银行管理系统,供大家参考,具体内容如下

有的地方用的方法的比较复杂,主要是为回顾更多的知识

test1用来存类和函数

#test1.py 
import random #用来随机产生卡号
import pickle #序列化,用来存放和取出产生的用户数据
import os #产生文件
import re #正则表达式,用来判断身份证和手机号,其他地方也可以使用
class Card:
 def __init__(self,cardId,password,money=0):
 self.cardId=cardId
 self.password=password
 self.money=money
class User(Card):
 def __init__(self,username,phone,uid,card):
 self.username=username
 self.phone=phone
 self.uid=uid
 self.user_card=card #User继承Card对象
class Bank(User): #Bank 继承User,Bank是序列化的对象,所以将其变为字典
 def __init__(self,user):
 self.cardId=user.user_card.cardId 
 self.user=user 
 self.users={self.cardId:self.user} #键,卡号 :值,User对象 

def generate_cardid(): #方法一 产生卡号
 list=[]
 for i in range(0,11):
 n=random.randint(0,9)
 n=str(n)
 list.append(n)
 # str="".join(list)
 return list
# generate_cardid()
# user_cardId=input("请输入您的账号:")
def create_user(): #方法2:开户
 while True:
 uid=input("请输入身份证:")
 realut=re.fullmatch("\d{17}[\d,X]",uid) #正则判断身份证是否合理
 if realut:
 uid=realut.group()
 break
 else:
 print("格式不合法")
 continue
 username=input("请输入姓名:")
 while True:
 phone=input("请输入手机号码:")
 realut = re.fullmatch("1\d{10}", phone) #正则判断手机号是否合理,其他需要判断的地方都可以判断,我就不再使用了
 if realut:
 phone=realut.group()
 # print(phone)
 break
 else:
 print("格式不合法")
 continue
 list=generate_cardid() #得到卡号列表
 cardId="".join(list) #将卡号变成字符串,字符串的卡号才能做成键
 print(f"您的卡号为:{cardId}")
 while True:
 password1=input("请输入密码:")
 password2=input("再次输入密码确认:")
 if password1==password2:
 password=password1
 break
 else :
 print("两次密码不同,请重新输入!")
 continue
 card=Card(cardId,password) 
 user=User(uid,username,phone,card)
 bank=Bank(user) #产生bank对象
 with open(f"data\\{cardId}.txt","ab") as file_w: #重点:创建一个文件夹data来存放产生的bank对象,每个对象根据卡号产生一个txt文件,用来存放用户的所有数据
 pickle.dump(bank,file_w) #将bank 序列化保存到文档中
# create_user()

def user_login(user_cardId): #登录

 if os.path.exists(f"data\\{user_cardId}.txt"):
 with open(f"data\\{user_cardId}.txt", "rb") as file_r:
 u_data = pickle.load(file_r) #根据卡号取出txt文档,反序列化取出数据
 if u_data.cardId == user_cardId: #u_data是一个字典,键是卡号,值是user对象 
 n = 1
 while True:
 if n <= 3:
 user_pw = input("请输入密码:")
 if u_data.user.user_card.password == user_pw:
 return True
 else:
 print("密码错误!")
 n+=1
 continue
 else:
 print("三次输入密码错误!")
 return
 else:
 print("没有该用户")

# user_login(user_cardId)
def save_money(user_cardId): # 方法4:存钱
 if user_login(user_cardId): #如果登录成功
 money=int(input("请您输入存钱金额:"))
 with open(f"data\\{user_cardId}.txt", "rb") as file_r:
 u_data = pickle.load(file_r)
 u_data.user.user_card.money=u_data.user.user_card.money+money
 print("您的余额为:",u_data.user.user_card.money)
 with open(f"data\\{user_cardId}.txt", "wb") as file_w: #这里要用wb,而不是ab,改变数据后,需要覆盖原来的数据,而不是添加
 pickle.dump(u_data, file_w)
# save_money()

def withdraw_money(user_cardId): # 方法5:取钱
 if user_login(user_cardId):
 money=int(input("请您输入取款金额:"))
 with open(f"data\\{user_cardId}.txt", "rb") as file_r:
 u_data = pickle.load(file_r)
 if money>u_data.user.user_card.money:
 print("余额不足")
 else:
 u_data.user.user_card.money=u_data.user.user_card.money-money
 print("您的余额为:", u_data.user.user_card.money)
 with open(f"data\\{user_cardId}.txt", "wb") as file_w:
 pickle.dump(u_data, file_w)

# withdraw_money()

def transfer_accounts(user_cardId): #方法6,转账
 if user_login(user_cardId):
 with open(f"data\\{user_cardId}.txt", "rb") as file_r:
 u_data = pickle.load(file_r)
 while True:
 money = int(input("请您转账取款金额:"))
 if money > u_data.user.user_card.money:
 print("余额不足")
 break
 else:
 cardId=int(input("请您对方卡号:"))
 if os.path.exists(f"data\\{cardId}.txt"): #如果对方卡号存在
 u_data.user.user_card.money = u_data.user.user_card.money - money #自己的money减
 print("您的余额为:", u_data.user.user_card.money)
 with open(f"data\\{user_cardId}.txt", "wb") as file_w:
 pickle.dump(u_data, file_w)

 with open(f"data\\{cardId}.txt", "rb") as file_r1: #根据对方的卡号进行查找对方的数据
 u_data1 = pickle.load(file_r1)
 with open(f"data\\{cardId}.txt", "wb") as file_w1:
 u_data1.user.user_card.money = u_data1.user.user_card.money + money #对方money加
 pickle.dump(u_data1, file_w1)
 print("转账成功")
 break
 else:
 print("该用户不存在")
 break

# transfer_accounts()

def select_user(user_cardId): # 方法7:查询余额
 if user_login(user_cardId):
 with open(f"data\\{user_cardId}.txt", "rb") as file_r:
 u_data = pickle.load(file_r)
 print("您的余额为:",u_data.user.user_card.money)
# select_user()

def update_password(user_cardId): # 方法8:修改密码
 if user_login(user_cardId):
 while True:
 pw1=input("请输入新密码:")
 pw2=input("请再次输入密码:")
 if pw1==pw2:
 with open(f"data\\{user_cardId}.txt", "rb") as file_r:
 u_data = pickle.load(file_r)
 u_data.user.user_card.password=pw1
 with open(f"data\\{user_cardId}.txt", "wb") as file_w:
 pickle.dump(u_data, file_w)
 break

 else:
 print("两次密码不相同")
 continue

test2用来调用test1中的函数

import test1
import os
T =True
while True:
 user_cardId=input("请输入您的账号,退出请按0,注册请按1:\n")
 if os.path.exists(f"data\\{user_cardId}.txt"):
 true=True
 while true:
 x = input("[2]查询余额 [3]取款 [4]存钱 [5]转账 [6]修改密码 [7]退出系统\n"
 "请选择需要的操作输入对应的数字:"
 if x=="2": #键盘输入默认是str类型
 test1.select_user(user_cardId)
 continue
 elif x=="3":
 test1.withdraw_money(user_cardId)
 continue

 elif x=="4":
 test1.save_money(user_cardId)
 continue
 elif x=="5":
 test1.transfer_accounts(user_cardId)
 continue
 elif x=="6":
 test1.update_password(user_cardId)
 continue
 elif x=="7":
 true=False
 else:
 print("没有对应操作")
 continue
 elif user_cardId=="0":
 print("欢迎再次使用")
 break
 elif user_cardId=="1":
 test1.create_user()
 continue

 else :
 print("没有该账户")
 T=True
 while T:
 num = input("需要注册账号请按1,退出注册服务请按0:")
 if num =="1":
 test1.create_user()
 elif num =="0":
 print("欢迎再次使用")
 T =False
 break
 else:
 print("没有对应操作")
 continue
 break

更多学习资料请关注专题《管理系统开发》。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中使用glob和rmtree删除目录子目录及所有文件的例子
Nov 21 Python
Python 两个列表的差集、并集和交集实现代码
Sep 21 Python
用python结合jieba和wordcloud实现词云效果
Sep 05 Python
一道python走迷宫算法题
Jan 22 Python
python清除函数占用的内存方法
Jun 25 Python
Python实现图片拼接的代码
Jul 02 Python
python一键去抖音视频水印工具
Sep 14 Python
浅谈python新式类和旧式类区别
Apr 26 Python
pytorch实现用Resnet提取特征并保存为txt文件的方法
Aug 20 Python
python爬虫用mongodb的理由
Jul 28 Python
Python离线安装各种库及pip的方法
Nov 28 Python
python创建字典及相关管理操作
Apr 13 Python
Django视图扩展类知识点详解
Oct 25 #Python
Python装饰器使用你可能不知道的几种姿势
Oct 25 #Python
win7下 python3.6 安装opencv 和 opencv-contrib-python解决 cv2.xfeatures2d.SIFT_create() 的问题
Oct 24 #Python
Python下应用opencv 实现人脸检测功能
Oct 24 #Python
Python迭代器iterator生成器generator使用解析
Oct 24 #Python
Python 取numpy数组的某几行某几列方法
Oct 24 #Python
Django和Flask框架优缺点对比
Oct 24 #Python
You might like
php生成zip压缩文件的方法详解
2013/06/09 PHP
在php和MySql中计算时间差的方法详解
2015/03/27 PHP
PHPCMS V9 添加二级导航的思路详解
2016/10/20 PHP
PHP正则匹配反斜杠'\'和美元'$'的方法
2017/02/08 PHP
php设计模式之单例模式用法经典示例分析
2019/09/20 PHP
js判断IE6/IE7/FF的代码[XMLHttpRequest]
2011/02/16 Javascript
图片onload事件触发问题解决方法
2011/07/31 Javascript
Extjs4 GridPanel 的几种样式使用介绍
2013/04/18 Javascript
js中点击空白区域时文本框与隐藏层的显示与影藏问题
2013/08/26 Javascript
JavaScript将数组转换成CSV格式的方法
2015/03/19 Javascript
jquery日历插件datepicker用法分析
2016/01/22 Javascript
Vue项目中如何引入icon图标
2018/03/28 Javascript
JS点击动态添加标签、删除指定标签的代码
2018/04/18 Javascript
webpack 样式加载的实现原理
2018/06/12 Javascript
js实现div色块拖动录制
2020/01/16 Javascript
详解vue实现坐标拾取器功能示例
2020/11/18 Vue.js
Python线程指南详细介绍
2017/01/05 Python
浅谈numpy数组的几种排序方式
2017/12/15 Python
Python语言描述连续子数组的最大和
2018/01/04 Python
Python Numpy 实现交换两行和两列的方法
2019/06/26 Python
Python 写了个新型冠状病毒疫情传播模拟程序
2020/02/14 Python
浅析Python迭代器的高级用法
2020/07/16 Python
python画图时设置分辨率和画布大小的实现(plt.figure())
2021/01/08 Python
实例教程 利用html5和css3打造一款创意404页面
2014/10/20 HTML / CSS
英国航空官网:British Airways
2016/09/11 全球购物
迪梵英国官方网站:Darphin英国
2017/12/06 全球购物
自考生毕业自我鉴定
2013/10/10 职场文书
环境工程专业个人求职信
2013/12/05 职场文书
车间组长岗位职责
2013/12/20 职场文书
面料业务员岗位职责
2013/12/26 职场文书
体育专业个人求职信范文
2013/12/27 职场文书
幼儿园教师自我评价
2015/03/04 职场文书
年度考核登记表个人总结
2015/03/06 职场文书
高考1977观后感
2015/06/04 职场文书
考生诚信考试承诺书(2016版)
2016/03/25 职场文书
使用 DataAnt 监控 Apache APISIX的原理解析
2022/07/07 Servers