Java实现银行账户类及多场景功能测试代码全解析
根据你的需求,我将提供一个Java版本的实现。首先,我们定义`BankAccount`类,然后创建一个测试类`BankAccountTest`来验证各种情况下的行为。 1. BankAccount 类 ```java public class BankAccount { private double balance; public BankAccount(double initialBalance) { this.balance = initialBalance; } public void deposit(double amount) { if (amount > 0) { balance += amount; } } public boolean withdraw(double amount) throws Exception { if (amount > 0 && balance >= amount) { balance -= amount; return true; } else if (balance < amount) { throw new Exception("Insufficient funds"); } return false; } public double getBalance() { return balance; } } ``` 2. BankAccountTest 类 ```java public class BankAccountTest { public static void main(String[] args) { testMethod1(); reset(); testMethod2(); reset(); testMethod3(); reset(); testMethod4(); } private static BankAccount account; private static void reset() { account = new BankAccount(0); } private static void testMethod1() { System.out.println("Starting Test Method 1:"); account = new BankAccount(0); account.deposit(20); try { account.withdraw(19.89); System.out.println("Withdraw successful."); } catch (Exception e) { System.out.println("Withdraw failed: " + e.getMessage()); } System.out.println("Current Balance: " + account.getBalance()); } private static void testMethod2() { System.out.println("\nStarting Test Method 2:"); account = new BankAccount(0); account.deposit(20); try { account.withdraw(21.01); System.out.println("Withdraw successful."); } catch (Exception e) { System.out.println("Withdraw failed: " + e.getMessage()); } System.out.println("Current Balance: " + account.getBalance()); } private static void testMethod3() { System.out.println("\nStarting Test Method 3:"); account.deposit(20); // This will not set the balance since account is not created yet. account = new BankAccount(0); try { account.withdraw(19.89); System.out.println("Withdraw successful."); } catch (Exception e) { System.out.println("Withdraw failed: " + e.getMessage()); } System.out.println("Current Balance: " + account.getBalance()); } private static void testMethod4() { System.out.println("\nStarting Test Method 4:"); try { account.withdraw(20); // This will fail because the account is not created yet. System.out.println("Withdraw successful."); } catch (Exception e) { System.out.println("Withdraw failed: " + e.getMessage()); } account = new BankAccount(0); account.deposit(20.01); try { account.withdraw(19.89); System.out.println("Withdraw successful."); } catch (Exception e) { System.out.println("Withdraw failed: " + e.getMessage()); } System.out.println("Current Balance: " + account.getBalance()); } } ``` 解释 1. BankAccount 类实现了基本的银行账户功能,包括存款、取款和获取余额。 2. BankAccountTest 类包含了四个测试方法,每个方法都按照指定的顺序执行操作,并打印结果。 3. reset 方法 在每个测试方法之间重置账户状态。 4. 异常处理 在取款时检查余额是否足够,并在余额不足时抛出异常。 这个示例应该能帮助你理解和实现所需的功能。 ######[AI写代码神器 | 837点数解答 | 2024-08-06 18:52:51]
- Python 实现简易密码设置系统:多种功能交互与安全要点揭秘(字节豆包 | 286点数解答 | 2025-03-28 10:43:21)355
- C语言实现多功能密码设置系统:含代码及安全注意要点(字节豆包 | 414点数解答 | 2025-03-28 10:43:55)343
- C 语言实现:超简单密码设置系统,含代码与注意要点!(字节豆包 | 519点数解答 | 2025-03-28 12:42:02)384
- C语言实现密码管理系统:含功能代码与安全注意事项(字节豆包 | 409点数解答 | 2025-04-05 18:09:56)333
- C语言实现多功能密码系统:初始化、确认、设置、显示、重置与退出(字节豆包 | 454点数解答 | 2025-04-05 18:48:42)332
- C语言实现密码管理系统:含初始化、确认、设置、显示、重置及退出功能(字节豆包 | 413点数解答 | 2025-04-06 15:37:16)279
- C 语言实现密码管理系统:含初始化、确认、设置、显示、重置及退出功能(字节豆包 | 403点数解答 | 2025-04-06 15:37:54)292
- Java 实现复制粘贴功能:打造简易文本复制粘贴程序(GPT | 1497点数解答 | 2024-08-25 09:40:33)405
- 解决数据库“dm.jdbc.driver.dmexception: 数据溢出”问题的全步骤指南(阿里通义 | 598点数解答 | 2024-10-15 15:48:38)305
- 达梦数据库数据溢出问题排查及多维度解决方法(阿里通义 | 403点数解答 | 2024-10-15 15:48:41)1134
- Python 实现球类:精准计算半径、表面积与体积,附输入验证与异常处理!(阿里通义 | 261点数解答 | 2024-11-28 21:19:39)343
- MATLAB 脚本:精准查找指定学号学生成绩信息及测试(字节豆包 | 971点数解答 | 2025-12-26 09:14:45)31