'''
银行管理员页面系统设计:
card 类: cardId password money
user 类: username phone id cards[]
银行的工作人员: 账号 密码 登录 展现管理系统所有功能
Bank类:
开户
取钱
存钱
转账
销户
查账
解锁
...
'''
import pickle
import random
# 银行卡
import time
#card 类
class Card:
def __init__(self, cardid, password, money):
self.cardid = cardid
self.password = password
self.money = money
self.cardlock = False
def __str__(self):
return '卡号:{},余额:{}'.format(self.cardid, self.money)
# 创建用户类
class User:
def __init__(self, uid, name, phone, card): # 此时的card,而是一个card对象
self.uid = uid
self.name = name
self.phone = phone
self.card = card
def __str__(self):
return '用户名:{},联系方式:{}'.format(self.name, self.phone)
#创建银行工作人员类
class BankWorker:
__login_name = 'admina'
__password = 'icbc111'
def printWelcomePage(self):
print('----------------------------')
print(' 欢迎使用ICBC管理系统 ')
print('----------------------------')
def workerLogin(self):
login_name = input('输入账户名:')
password = input('输入密码:')
if self.__login_name != login_name:
print('账户名输入有误!')
return -1
if self.__password != password:
print('密码输入有误!')
return -1
print('登录成功,加载中....')
time.sleep(2)
return 0
def bankFunction(self):
s = '''
功能如下:
1. 开户 2. 取钱 3. 存钱
4. 转账 5. 销户 6. 查账
7. 解锁 8. 改密 9. 修改信息
10. 退出
'''
print(s)
# 创建Bank类
class Bank:
def __init__(self, users):
self.users = users
def create_user(self):
# uid, name, phone, card
uid = input('输入身份证号码:')
name = input('输入姓名:')
phone = input('输入手机号码:')
# 对card 进行操作
# 产生卡号
cardid = self.generate_cardid()
# 输入密码
password = input('请输入密码:')
repassword = input('再次输入密码:')
if password and repassword:
if password == repassword:
print('密码设置成功!')
else:
print('两次密码不一致!')
return -1
else:
print('必须输入密码!')
return -1
# money
money = 0
card = Card(cardid, password, money)
user = User(uid, name, phone, card)
# 通过字典将卡号与用户进行绑定
self.users[cardid] = user
print('恭喜{}开户成功!卡号是:{}'.format(name,cardid))
def generate_cardid(self):
no = '62'
for i in range(3):
n = random.randint(0, 9)
no += str(n)
return no
def save_money(self):
islogin, user = self.user_login()
if not islogin:
return -1
# 输入存款金额
money = int(input('验证成功!请输入存款金额:'))
# 存钱成功
if money <= 0:
print('存款金额必须大于0,存款失败!')
return -1
user.card.money += money
print('您成功存款:{},账户余额是:{}'.format(money, user.card.money))
def withdraw_money(self):
islogin, user = self.user_login()
if not islogin:
return -1
#
# 输入取款金额
money = int(input('验证成功!请输入取款金额:'))
if money <= 0:
print('取款金额必须要大于0!取款失败!')
return -1
if money > user.card.money:
print('卡中余额不足,取款失败!')
return -1
user.card.money -= money
print('您成功取款:{},账户余额是:{}'.format(money, user.card.money))
def transfer_accounts(self):
islogin, user = self.user_login()
if not islogin:
return -1
# 输入转账金额
money = int(input('验证成功!请输入转账金额:'))
if money < 0 or money > user.card.money:
print("金额输入有误或者账户金额不足,转账失败!")
return -1
cardid = input('输入收款人的卡号:')
other_user = self.users.get(cardid)
# 有没有这个卡号对应用户
if not other_user:
print('不存在此卡号,转账失败!')
return -1
# 如果有这个用户,此卡是否被锁定
if other_user.card.cardlock:
print('该卡已经锁定,无法完成转账')
return -1
print('转账中请稍后.....')
user.card.money -= money
other_user.card.money += money
time.sleep(1)
print('成功转账:{},卡中余额是:{}'.format(money, user.card.money))
def user_login(self):
cardid = input('输入银行卡号:')
user = self.users.get(cardid, None)
# 有没有这个卡号对应用户
if not user:
print('不存在此卡号,存款失败!')
return False, user
# 如果有这个用户,此卡是否被锁定
if user.card.cardlock:
print('该卡已经锁定,请解锁...')
return False, user
# 如果没有锁定则需要输入密码,如果密码输入错误三次则锁定此卡
r = self.check_password(user.card.password)
if not r:
# 锁定账户
print('密码连续3次输入错误,此卡锁定!')
return False, user
return True, user
def check_password(self, password):
for i in range(3):
pwd = input('输入登录密码:')
if pwd == password:
return True
return False
def update_password(self):
islogin, user = self.user_login()
if not islogin:
return -1
# 修改密码
new_pwd = input('输入新密码:')
renew_pwd = input('再次输入新密码:')
if not new_pwd or not renew_pwd:
print('密码不能为空,修改密码失败!')
return -1
if new_pwd != renew_pwd:
print('密码不一致,修改密码失败!!')
return -1
user.card.password = new_pwd
print('修改密码成功!')
def update_msg(self):
islogin, user = self.user_login()
if not islogin:
return -1
# 修改手机号
new_phone = input('输入新密码:')
renew_phone= input('再次输入新密码:')
if not new_phone or not renew_phone:
print('手机号不能为空,修改失败!')
return -1
if new_phone != renew_phone:
print('输入不一致,修改失败!!')
return -1
user.card.phone = new_phone
print('修改手机号成功!')
def del_user(self):
islogin, user = self.user_login()
if not islogin:
return -1
answer = input('确认要注销用户吗?(yes/no)')
if answer == 'yes':
del self.users[user.card.cardid]
time.sleep(1)
print('账户注销成功!')
def select_user(self):
islogin, user = self.user_login()
if not islogin:
return -1
print(user)
print('卡号:{}的账户金额是:{}'.format(user.card.cardid, user.card.money))
def unlock_user(self):
cardid = input('输入银行卡号:')
user = self.users.get(cardid, None)
# 有没有这个卡号对应用户
if not user:
print('不存在此卡号,存款失败!')
return -1
uid = input('输入身份证号码:')
if user.uid != uid:
print('无效的身份证号码!解锁失败!')
return -1
user.card.cardlock = False
print('解锁成功!')
# 入口
def main():
# 1. 创建银行工作人员类
bworker = BankWorker()
# 2.加载欢迎页面
bworker.printWelcomePage()
# 3.登录
r = bworker.workerLogin()
if r == -1:
return -1
# 加载数据库中的数据:
# with open('users.txt', 'rb') as rs:
# users = pickle.load(rs)
users = {}
# print(users)
# 516237458697000
bank = Bank(users)
while True:
bworker.bankFunction()
choice = input('请选择功能:')
if choice == '1':
print('----------开户----------')
bank.create_user()
elif choice == '2':
# 取钱
print('----------取钱----------')
bank.withdraw_money()
elif choice == '3':
# 存钱
print('----------存钱----------')
bank.save_money()
elif choice == '4':
# 转账
print('----------转账----------')
bank.transfer_accounts()
elif choice == '5':
print('----------销户----------')
bank.del_user()
elif choice == '6':
print('----------查账----------')
bank.select_user()
elif choice == '7':
print('----------解锁----------')
bank.unlock_user()
elif choice == '8':
print('----------改密----------')
bank.update_password()
elif choice == '9':
print('----------修改手机号----------')
bank.update_msg()
elif choice == '10':
# 退出
answer = input('是否确定退出?(yes/no)')
if answer.lower() == 'yes':
print('退出中.......')
time.sleep(2)
with open('users.txt', 'wb') as ws:
pickle.dump(bank.users, ws)
print('退出完成!')
break
if __name__ == '__main__':
main()