| 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.admin_pwd = admin_pwd |
| |
| self.accounts = dict() |
| self.api_map = None |
| |
| def add_order_set(self, api_map): |
| setattr(self, "api_map", api_map) |
| self.query_support_func(custom=True) |
| |
| def api(self, input_order): |
| if not isinstance(self.api_map, dict): |
| print("请先添加接口指令集!") |
| return |
| func_tuple = self.api_map.get(input_order) |
| if func_tuple: |
| func_tuple[0]() |
| else: |
| print(f"指令 {input_order} 暂未收录, 请在先在接口指令集中添加指令!") |
| |
| def checkout_account_by_id(self, account_id=None, *args): |
| if account_id in self.accounts: |
| return self.accounts[account_id] |
| else: |
| print(f"未查询到该账号在本银行的账户信息!请先开户或重新确认银行账号!") |
| return None |
| |
| def check_pwd_correct_by_AccountID(self, account_id=None, input_num=3, *args): |
| account_id = account_id or input("请输入账户ID: ") |
| account = self.checkout_account_by_id(account_id) |
| |
| for _ in range(input_num): |
| pwd = input("请输入操作密码: ") |
| if account.password == pwd: |
| return True |
| else: |
| print(f"账户 {account.user_id} 密码输出错误, 请重新输入!") |
| |
| else: |
| print("密码输入错误超过尝试次数!") |
| return False |
| |
| def query_all_user(self, input_num=2, *args): |
| for _ in range(input_num): |
| pwd = input("请输入管理员密码: ") |
| if self.admin_pwd == pwd: |
| print(f"共计 {len(self.accounts)} 名用户;名单如下: ") |
| for index, account in enumerate(self.accounts.values()): |
| print(f"{index + 1} >>【账户ID】: {account.user_id} 【账户名】: {account.name} 【账户余额】: {account.money}") |
| return |
| else: |
| print(f"密码输出错误, 请重新输入!") |
| |
| else: |
| print("密码输入错误超过尝试次数!") |
| |
| def query_support_func(self, custom=False, *args): |
| if not custom: |
| print(f""" |
| ===== 欢迎进入【{self.name}银行】系统 ===== |
| 【本银行支持以下业务】: |
| <查询可办理的业务> |
| <查询余额> |
| <存取款> |
| <转账> |
| <账户开户> |
| <账户销户> |
| """) |
| elif custom and isinstance(self.api_map, dict): |
| menu_str = f""" |
| ===== 欢迎进入【{self.name}银行】系统 ===== |
| <本银行支持以下业务>: |
| |
| 【指令】\t\t=>\t\t【功能说明】 |
| """ |
| for custom_order, func_tuple in self.api_map.items(): |
| if isinstance(custom_order, (int, str)) and len(func_tuple) == 2 and isinstance(func_tuple[1], str): |
| menu_str += f"""{custom_order}\t\t=>\t\t"{func_tuple[1]}"\n\t\t\t""" |
| else: |
| print(""" |
| 自定义指令集格式错误,请按如下格式设置指令集: |
| { |
| 操作指令1:(功能函数1, 功能描述1), |
| 操作指令2:(功能函数2, 功能描述2), |
| ..... |
| } |
| """"") |
| print(menu_str) |
| else: |
| print("自定义指令集格式错误") |
| |
| def query_money(self, account_id=None, *args): |
| print("\n-------------------【正在查询】-------------------") |
| account_id = account_id or input("请输入查询账户ID: ") |
| account = self.checkout_account_by_id(account_id) |
| |
| if not account or not self.check_pwd_correct_by_AccountID(account_id): |
| return |
| |
| print(f""" |
| 【账户ID】: {account.user_id} |
| 【账户名】: {account.name} |
| 【账户余额】: {account.money} |
| """) |
| |
| def add_money(self, account_id=None, a_money=None, *args): |
| print("\n-------------------【正在存入】-------------------") |
| account_id = account_id or input("请输入存入账户ID: ") |
| account = self.checkout_account_by_id(account_id) |
| |
| if not account: |
| return |
| |
| 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=None, r_money=None, *args): |
| print("\n-------------------【正在支取】-------------------") |
| account_id = account_id or input("请输入支取账户ID: ") |
| account = self.checkout_account_by_id(account_id) |
| money = r_money or float(input("请输入支取金额: ")) |
| |
| if not account or not self.check_pwd_correct_by_AccountID(account_id): |
| return |
| |
| if account.money < money: |
| print("账户余额不足!") |
| return |
| |
| account.money -= money |
| print(f"用户 {account.name} 账户【{account_id}】成功支取 {money} 元,【账户余额】:{account.money}") |
| return money |
| |
| def transfer_money(self, *args): |
| print("\n-------------------【转账中..... 】-------------------") |
| transfer_out_account_id = input("请输入金额转出账户ID: ") |
| transfer_out_account = self.checkout_account_by_id(transfer_out_account_id) |
| if not transfer_out_account: |
| return |
| |
| transfer_in_account_id = input("请输入金额转入账户ID: ") |
| transfer_in_account = self.checkout_account_by_id(transfer_in_account_id) |
| if not transfer_in_account: |
| 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, *args): |
| name = input("请输入账户名: ") |
| password = input("请输入账户密码: ") |
| account = Account(name, password, money=0) |
| self.accounts[account.user_id] = account |
| return account |
| |
| def del_account(self, *args): |
| account_id = input("请输入账户ID: ") |
| account = self.checkout_account_by_id(account_id) |
| |
| if not account or not self.check_pwd_correct_by_AccountID(account_id): |
| return |
| |
| if account.money != 0: |
| print("账户余额不为0,请先取出余额以后再操作!") |
| return |
| |
| self.accounts.pop(account_id) |
| print(f"账户【{account_id}】成功销户") |
| return True |
| |
| |
| def custom(): |
| print("这里是自定义功能函数!") |
| |
| |
| kylin = Bank("麒麟", "999999") |
| func_map = { |
| |
| "admin": (kylin.query_all_user, "管理员操作"), |
| "业务办理": (kylin.query_support_func, "查询可办理的业务"), |
| "余额查询": (kylin.query_money, "查询余额"), |
| "存款": (kylin.add_money, "存款"), |
| "取款": (kylin.reduce_money, "取款"), |
| "转账": (kylin.transfer_money, "转账"), |
| "开户": (kylin.create_account, "账户开户"), |
| "销户": (kylin.del_account, "账户销户"), |
| "其他业务": (custom, "其他业务办理"), |
| } |
| kylin.add_order_set(func_map) |
| while True: |
| order = input("请选择您需要办理的业务: ") |
| kylin.api(order) |
| |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义