面向对象高级实战演练之银行系统
实现功能:
1. 银行管理员(使用管理员密码)查看所有用户信息
2. 进入银行系统提示功能
3. 用户密码管理
4. 账户开户/销户
5. 存款/取款
6. 用户间转账
7. 用户余额查询
8. 常见错误检查和提示
代码实现:
import random
import string
class Account ( object ) :
def __init__ ( self, name, password, money) :
self. user_id = self. __get_random_char( 6 )
self. name = name
self. password = password
self. money = money
print ( f"""
成功创建账户!
【账户ID】: { self. user_id}
【账户名】: { self. name}
【账户余额】: { self. money}
""" )
@staticmethod
def __get_random_char ( num) :
all_str = string. ascii_uppercase + string. digits
str_list = random. choices( all_str, k= num)
return "" . join( str_list)
class Bank :
def __init__ ( self, name, admin_pwd) :
self. name = name
self. accounts = dict ( )
self. admin_pwd = admin_pwd
self. query_support_func( )
def check_account ( self, account_id) :
if account_id in self. accounts:
return True
else :
print ( f"未查询到该账号在本银行的账户信息!请先开户或重新确认银行账号!" )
return False
def query_all_user ( self) :
for _ in range ( 2 ) :
pwd = input ( "请输入管理员密码: " )
if self. admin_pwd == pwd:
print ( f"共计 { len ( self. accounts) } 名用户;名单如下: " )
for account in self. accounts. values( ) :
print ( f"【账户ID】: { account. user_id} 【账户名】: { account. name} 【账户余额】: { account. money} " )
return
else :
print ( f"密码输出错误, 请重新输入!" )
else :
print ( "密码输入错误超过尝试次数!" )
@staticmethod
def check_pwd_input ( account, num= 3 ) :
for _ in range ( num) :
pwd = input ( "请输入支出账户对应密码: " )
if account. password == pwd:
return True
else :
print ( f"账户 { account. user_id} 密码输出错误, 请重新输入!" )
else :
print ( "密码输入错误超过尝试次数!" )
return False
def query_support_func ( self) :
print (
f"""
欢迎进入【 { self. name} 银行】系统!!!
【本银行支持以下业务】:
0. 查询可办理的业务
00. 查询余额
1. 存钱
2. 取钱
3. 转账
8. 账户开户
9. 账户销户
"""
)
def query_money ( self, account_id) :
print ( "\n-------------------【正在查询】-------------------" )
if not self. check_account( account_id) :
return
account = self. accounts[ account_id]
if not self. check_pwd_input( account) :
return
print ( f"""
【账户ID】: { account. user_id}
【账户名】: { account. name}
【账户余额】: { account. money}
""" )
def add_money ( self, account_id, a_money= None ) :
print ( "\n-------------------【正在存入】-------------------" )
if not self. check_account( account_id) :
return
account = self. accounts[ account_id]
money = a_money or float ( input ( "请输入存款金额: " ) )
account. money += money
print ( f"用户 { account. name} 名下账户【 { account_id} 】成功存入 { money} 元,【账户余额】: { account. money} " )
return money
def reduce_money ( self, account_id, r_money= None ) :
print ( "\n-------------------【正在支取】-------------------" )
if not self. check_account( account_id) :
return
account = self. accounts[ account_id]
if not self. check_pwd_input( account) :
return
money = r_money or float ( input ( "请输入支出金额: " ) )
if account. money < money:
print ( "账户余额不足!" )
return
account. money -= money
print ( f"用户 { account. name} 账户【 { account_id} 】成功支取 { money} 元,【账户余额】: { account. money} " )
return money
def transfer_money ( self, transfer_out_account_id, transfer_in_account_id) :
print ( "\n-------------------【正在转账】-------------------" )
if not self. check_account( transfer_out_account_id) or not self. check_account( transfer_in_account_id) :
return
money = self. reduce_money( transfer_out_account_id)
if money is not None :
self. add_money( transfer_in_account_id, money)
def create_account ( self, name, password) :
account = Account( name, password, money= 0 )
self. accounts[ account. user_id] = account
def del_account ( self, account_id) :
if not self. check_account( account_id) :
return
account = self. accounts[ account_id]
if not self. check_pwd_input( account) :
return
if account. money != 0 :
print ( "账户余额不为0,请先取出余额以后再操作!" )
return
self. accounts. pop( account_id)
print ( f"账户【 { account_id} 】成功销户" )
return True
kylin = Bank( "麒麟" , "999999" )
while True :
order = input ( "请选择您需要办理的业务: " )
works = {
"admin" : kylin. query_all_user,
"0" : kylin. query_support_func,
"00" : kylin. query_money,
"1" : kylin. add_money,
"2" : kylin. reduce_money,
"3" : kylin. transfer_money,
"8" : kylin. create_account,
"9" : kylin. del_account,
}
if order in works:
if order == "0" or order == "admin" :
works[ order] ( )
elif order == "3" :
account1 = input ( "请输入金额转出账户ID: " )
account2 = input ( "请输入金额转入账户ID: " )
works[ order] ( account1, account2)
elif order == "8" :
name = input ( "请输入账户名: " )
password = input ( "请输入账户密码: " )
works[ order] ( name, password)
else :
account = input ( "请输入账户ID: " )
works[ order] ( account)
else :
print ( "暂不支持该业务, 请重新输入!" )
效果展示