废话不多说,代码如下:
"""" 预设值三个账户 定义银行账户类 定义ATM机类 self.accounts = { '1122334455667788': {'password': '123321', 'balance': 1200.0, 'valid': True}, '1122334455667789': {'password': '123456', 'balance': 54321.0, 'valid': True}, '1122334455667790': {'password': '147258', 'balance': 0.0, 'valid': False} } """ import time class AccountCard: def __init__(self, card_no, expiry_date, card_type="储蓄卡"): self.expiry_card = expiry_date # 截止日期 self.card_no = card_no # 卡号 self.card_type = card_type # 银行卡类型 def __repr__(self): """打印信息""" return f'银行卡类型:{self.card_no}\n' \ f'有效日期:{self.expiry_card}\n' \ f'银行卡类型:{self.card_type}' class ATM: def __init__(self): self.accounts = { '1122334455667788': {'password': '123321', 'balance': 1200.0, 'valid': True}, '1122334455667789': {'password': '123456', 'balance': 54321.0, 'valid': True}, '1122334455667790': {'password': '147258', 'balance': 0.0, 'valid': False} } self.current_card = None # 当前ATM机识别的银行卡 self.current_account = None # 当前ATM机内的账户 def read_card(self, card): """读卡""" if card.card_no in self.accounts: self.current_account = self.accounts[card.card_no] for _ in range(3): password = input("请输入账户密码:") if password == self.current_account["password"]: # 若输入密码成功,当前ATM识别的银行卡为当前插入的卡 self.current_card = card return True else: print(f"密码错误,您还有{2-_}次尝试的机会!") else: print("该账户被冻结24小时,如有疑问,请联系工作人员。") else: print("该账户不存在!") return False def show_balance(self): """查询余额""" if self.current_account: print(f"账余额为:{self.current_account['balance']}RMB") def deposited(self, money): """存钱""" if self.current_account and money > 0: self.current_account['balance'] += money print("正在处理,请稍后...") time.sleep(3) print("存款成功") else: return False def drawback(self, money): """取款""" if self.current_account and money <= self.current_account['balance']: self.current_account['balance'] -= money print("正在处理,请稍后...") time.sleep(3) print("请取走您的钞票") return True else: print("账户余额不足!!!") return False def transfer_money(self, other_card_no, money): """转账""" if other_card_no in self.accounts: other_account = self.accounts[other_card_no] if money <= self.current_account['balance']: other_account['balance'] += money self.current_account['balance'] -= money print("正在处理,请稍后...") time.sleep(3) print("转账成功") return True else: print("账户余额补不足。") return False else: print("该账户不存在。") return False def card_retrieve(self): """取卡""" print("正在退卡,请稍后...") time.sleep(2) print("请取走您的银行卡。") self.current_account = None self.current_card = None def main(): my_card = AccountCard(card_no='1122334455667788', expiry_date='2020-12-31') print(my_card) """构造器语法""" atm = ATM() if atm.read_card(my_card): while True: select_option = eval(input("\n1、查询余额\t2、存款\t3、取款\t4、转账\t5、退卡\n请输入要进行操作的序号:")) if select_option == 1: atm.show_balance() elif select_option == 2: deposit_money = eval(input("请输入存款金额:")) atm.deposited(money=deposit_money) elif select_option == 3: drawback_money = eval(input("请输入要取走的金额:")) atm.drawback(money=drawback_money) elif select_option == 4: user_name = input("请输入转入账户账号:") trans_money = eval(input("请输入转入金额:")) atm.transfer_money(other_card_no=user_name, money=trans_money) elif select_option == 5: atm.card_retrieve() break else: print("操作有误,请重新输入!!!") if __name__ == "__main__": main()