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算法之栈(stack)的实现
Aug 18 Python
跟老齐学Python之dict()的操作方法
Sep 24 Python
深入探究Python中变量的拷贝和作用域问题
May 05 Python
Python3 伪装浏览器的方法示例
Nov 23 Python
Python3的介绍、安装和命令行的认识(推荐)
Oct 20 Python
Django ManyToManyField 跨越中间表查询的方法
Dec 18 Python
python实现简单银行管理系统
Oct 25 Python
pytorch的batch normalize使用详解
Jan 15 Python
python 一维二维插值实例
Apr 22 Python
python 日志模块 日志等级设置失效的解决方案
May 26 Python
python如何爬取动态网站
Sep 09 Python
详解Python类和对象内容
Jun 22 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调用时间格式的参数详解
2013/06/06 PHP
标准版Eclipse搭建PHP环境的详细步骤
2015/11/18 PHP
Laravel4中的Validator验证扩展用法详解
2016/07/26 PHP
Dojo之路:如何利用Dojo实现Drag and Drop效果
2007/04/10 Javascript
JS面向对象编程 for Cookie
2010/09/19 Javascript
javascript中用星号表示预录入内容的实现代码
2011/01/08 Javascript
jQuery源码分析-05异步队列 Deferred 使用介绍
2011/11/14 Javascript
js改变img标签的src属性在IE下没反应的解决方法
2013/07/23 Javascript
javascript实现yield的方法
2013/11/06 Javascript
javascript操作html控件实例(javascript添加html)
2013/12/02 Javascript
js加密解密字符串可自定义密码因子
2014/05/13 Javascript
javascript操作表格排序实例分析
2015/05/06 Javascript
jQuery Ajax页面局部加载方法汇总
2016/06/02 Javascript
JavaScript简单实现弹出拖拽窗口(二)
2016/06/17 Javascript
Bootstrap面板使用方法
2017/01/16 Javascript
js 去掉字符串前后空格实现代码集合
2017/03/25 Javascript
vue写h5页面的方法总结
2019/02/12 Javascript
微信小程序实现上拉加载功能
2019/11/20 Javascript
JavaScript对象原型链原理详解
2020/02/05 Javascript
基于Angular 8和Bootstrap 4实现动态主题切换的示例代码
2020/02/11 Javascript
JavaScript中常用的3种弹出提示框(alert、confirm、prompt)
2020/11/10 Javascript
Win7下搭建python开发环境图文教程(安装Python、pip、解释器)
2016/05/17 Python
Python实现PS滤镜中马赛克效果示例
2018/01/20 Python
pycharm debug功能实现跳到循环末尾的方法
2018/11/29 Python
Python中最好用的命令行参数解析工具(argparse)
2019/08/23 Python
Python操作Elasticsearch处理timeout超时
2020/07/17 Python
MAC平台基于Python Appium环境搭建过程图解
2020/08/13 Python
python logging模块的使用详解
2020/10/23 Python
小学生手册家长评语
2014/04/16 职场文书
论文指导教师评语
2014/04/28 职场文书
孝敬父母的活动方案
2014/08/31 职场文书
个人授权委托书范文
2014/09/21 职场文书
离婚答辩状怎么写
2015/05/22 职场文书
婚礼迎宾词大全
2015/08/10 职场文书
小学生法制教育心得体会
2016/01/14 职场文书
JavaScript 去重和重复次数统计
2021/03/31 Javascript