Python银行管理系统代码
时间: 2024-03-28 18:31:52 浏览: 84
以下是一个简单的Python银行管理系统代码,其中包括管理员类、ATM类、人类、银行卡类和主函数main()代码:
```python
# 银行卡类
class Card(object):
def __init__(self, cardid, cardpasswd, cardmoney):
self.cardid = cardid # 信用卡号
self.cardpasswd = cardpasswd # 信用卡密码
self.cardmoney = cardmoney # 卡内余额
self.cardlock = False # 是否被锁定
# 人类
class Person(object):
def __init__(self, name, idcard, phone, card):
self.name = name # 姓名
self.idcard = idcard # 身份证号
self.phone = phone # 手机号
self.card = card # 银行卡
# ATM类
class ATM(object):
def __init__(self, allusers):
self.allusers = allusers # 所有用户字典
# 开户
def createUser(self):
name = input("请输入您的姓名:")
idcard = input("请输入您的身份证号:")
phone = input("请输入您的手机号:")
prestoreMoney = int(input("请输入预存款金额:"))
if prestoreMoney < 0:
print("预存款输入有误!开户失败...")
return -1
onePasswd = input("请设置密码:")
if not self.checkPasswd(onePasswd):
print("密码输入错误!开户失败...")
return -1
cardid = self.randomCardId()
card = Card(cardid, onePasswd, prestoreMoney)
user = Person(name, idcard, phone, card)
self.allusers[cardid] = user
print("开户成功!请记住您的卡号(%s)..." % cardid)
# 查询
def searchUserInfo(self):
cardnum = input("请输入您的卡号:")
user = self.allusers.get(cardnum)
if not user:
print("该卡号不存在!查询失败...")
return -1
if user.card.cardlock:
print("该卡已被锁定!请解锁后再进行其他操作...")
return -1
print("姓名:%s\n身份证号:%s\n手机号:%s\n卡号:%s\n余额:%d" % (user.name, user.idcard, user.phone, user.card.cardid, user.card.cardmoney))
# 取款
def getMoney(self):
cardnum = input("请输入您的卡号:")
user = self.allusers.get(cardnum)
if not user:
print("该卡号不存在!取款失败...")
return -1
if user.card.cardlock:
print("该卡已被锁定!请解锁后再进行其他操作...")
return -1
money = int(input("请输入取款金额:"))
if money > user.card.cardmoney:
print("余额不足!取款失败...")
return -1
user.card.cardmoney -= money
print("取款成功!余额:%d" % user.card.cardmoney)
# 存款
def saveMoney(self):
cardnum = input("请输入您的卡号:")
user = self.allusers.get(cardnum)
if not user:
print("该卡号不存在!存款失败...")
return -1
if user.card.cardlock:
print("该卡已被锁定!请解锁后再进行其他操作...")
return -1
money = int(input("请输入存款金额:"))
if money < 0:
print("存款金额输入有误!存款失败...")
return -1
user.card.cardmoney += money
print("存款成功!余额:%d" % user.card.cardmoney)
# 改密
def changePasswd(self):
cardnum = input("请输入您的卡号:")
user = self.allusers.get(cardnum)
if not user:
print("该卡号不存在!改密失败...")
return -1
if user.card.cardlock:
print("该卡已被锁定!请解锁后再进行其他操作...")
return -1
oldPasswd = input("请输入旧密码:")
if user.card.cardpasswd != oldPasswd:
print("密码输入错误!改密失败...")
return -1
newPasswd = input("请输入新密码:")
if not self.checkPasswd(newPasswd):
print("密码输入错误!改密失败...")
return -1
user.card.cardpasswd = newPasswd
print("改密成功!")
# 锁定
def lockUser(self):
cardnum = input("请输入您的卡号:")
user = self.allusers.get(cardnum)
if not user:
print("该卡号不存在!锁定失败...")
return -1
user.card.cardlock = True
print("锁定成功!")
# 解锁
def unlockUser(self):
cardnum = input("请输入您的卡号:")
user = self.allusers.get(cardnum)
if not user:
print("该卡号不存在!解锁失败...")
return -1
user.card.cardlock = False
print("解锁成功!")
# 补卡
def newCard(self):
cardnum = input("请输入您的卡号:")
user = self.allusers.get(cardnum)
if not user:
print("该卡号不存在!补卡失败...")
return -1
if user.card.cardlock:
print("该卡已被锁定!请解锁后再进行其他操作...")
return -1
newcardpasswd = input("请输入新卡密码:")
if not self.checkPasswd(newcardpasswd):
print("密码输入错误!补卡失败...")
return -1
user.card.cardid = self.randomCardId()
user.card.cardpasswd = newcardpasswd
print("补卡成功!请记住您的新卡号(%s)..." % user.card.cardid)
# 随机生成卡号
def randomCardId(self):
while True:
str = ""
for i in range(6):
ch = chr(random.randrange(ord('0'), ord('9') + 1))
str += ch
if not self.allusers.get(str):
return str
# 检查密码是否合法
def checkPasswd(self, passwd):
if len(passwd) != 6:
return False
if not passwd.isdigit():
return False
return True
# 管理员类
class Admin(object):
def __init__(self, name, passwd, atm):
self.name = name # 姓名
self.passwd = passwd # 密码
self.atm = atm # ATM机
# 管理员登录
def adminLogin(self):
name = input("请输入管理员姓名:")
passwd = input("请输入管理员密码:")
if self.name != name or self.passwd != passwd:
print("管理员姓名或密码错误!登录失败...")
return -1
print("管理员登录成功!")
while True:
print("请选择操作:\n1.开户\n2.查询\n3.取款\n4.存款\n5.改密\n6.锁定\n7.解锁\n8.补卡\n9.退出")
choice = int(input())
if choice == 1:
self.atm.createUser()
elif choice == 2:
self.atm.searchUserInfo()
elif choice == 3:
self.atm.getMoney()
elif choice == 4:
self.atm.saveMoney()
elif choice == 5:
self.atm.changePasswd()
elif choice == 6:
self.atm.lockUser()
elif choice == 7:
self.atm.unlockUser()
elif choice == 8:
self.atm.newCard()
elif choice == 9:
print("管理员退出!")
break
else:
print("输入有误,请重新输入!")
# 主函数
def main():
allusers = {}
atm = ATM(allusers)
admin = Admin("admin", "admin", atm)
admin.adminLogin()
if __name__ == '__main__':
main()
```
阅读全文