进阶day05-自定义异常-银行案例

This commit is contained in:
2026-02-04 15:27:23 +08:00
parent 8d3c857be3
commit 6dbbd684f9
7 changed files with 209 additions and 0 deletions

View File

@@ -0,0 +1,52 @@
package com.inmind.custom_exception06;
//账户类
public class Account {
String id;//账户ID
String name;
double balance;
int pwdErrors;//密码输错次数
boolean locked;//是否是锁定状态
static final int MAX_PWD_ERROR = 3;//最大密码错误次数
static final String PWD = "123456";//默认密码
public Account(String id, String name, double balance) {
this.id = id;
this.name = name;
this.balance = balance;
}
//存款功能
public void saveMoney(double balance) throws AccountLockedException, InvalidAmountException {
if (this.locked) {
throw new AccountLockedException("账户"+this.id+"已锁定");
}
if (balance <= 0) {
throw new InvalidAmountException("存款必须大于0");
}
this.balance += balance;
System.out.println(this.name+"存了"+balance+"元,当前余额为"+this.balance+"");
}
// 取款
public void getMoney(double amount, String pwd) throws Exception {
if (locked) throw new AccountLockedException("账户" + id + "已锁定");
// 密码验证
if (!PWD.equals(pwd)) {
pwdErrors++;
System.out.println("pwdErrors的值" + pwdErrors);
if (pwdErrors >= MAX_PWD_ERROR) {
locked = true;
throw new AccountLockedException("密码错误3次账户" + id + "已锁定");
}
throw new Exception("密码错误,剩余次数:" + (MAX_PWD_ERROR - pwdErrors));
}
if (amount <= 0) throw new InvalidAmountException("取款金额必须大于0");
if (amount > balance) throw new NotEnoughException("余额不足,当前余额:" + balance);
balance -= amount;
System.out.println(name + "取款" + amount + "元,当前余额:" + balance);
}
}

View File

@@ -0,0 +1,7 @@
package com.inmind.custom_exception06;
// 自定义异常3账户锁定异常
public class AccountLockedException extends Exception{
public AccountLockedException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,45 @@
package com.inmind.custom_exception06;
// 银行类
class Bank {
private Account[] accounts; // 账户数组
private int count; // 账户数量
private static final double MAX_TRANSFER = 50000; // 单笔最大转账额
// 构造方法
public Bank(int size) {
accounts = new Account[size];
count = 0;
}
// 创建账户
public void addAccount(Account acc) {
if (count < accounts.length) {
accounts[count++] = acc;
System.out.println("创建账户成功:" + acc.id + "(" + acc.name + ")");
}
}
// 查找账户
public Account findAccount(String id) throws Exception {
for (int i = 0; i < count; i++) {
if (accounts[i].id.equals(id)) {
return accounts[i];
}
}
throw new Exception("账户" + id + "不存在");
}
// 转账
public void transferMoney(String fromId, String toId, double amount, String pwd) throws Exception {
Account from = findAccount(fromId);
Account to = findAccount(toId);
if (amount <= 0) throw new InvalidAmountException("转账金额必须大于0");
if (amount > MAX_TRANSFER) throw new TransferLimitException("超过最大转账额" + MAX_TRANSFER);
from.getMoney(amount, pwd); // 先取款
to.saveMoney(amount); // 再存款
System.out.println("" + from.name + "" + to.name + "转账" + amount + "元成功");
}
}

View File

@@ -0,0 +1,7 @@
package com.inmind.custom_exception06;
// 自定义异常2无效金额异常
public class InvalidAmountException extends Exception{
public InvalidAmountException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,8 @@
package com.inmind.custom_exception06;
//余额不足异常
public class NotEnoughException extends Exception{
public NotEnoughException(String message) {
super(message);
}
}

View File

@@ -0,0 +1,82 @@
package com.inmind.custom_exception06;
import java.sql.SQLOutput;
/*
自定义异常4 个):
NotEnoughException余额不足时抛出
InvalidAmountException金额为负数或零时抛出
AccountLockedException账户锁定时抛出
TransferLimitException转账超过限额时抛出
核心功能:
存款:验证账户状态和金额有效性
取款验证密码3 次错误锁定)、金额和余额
转账:校验双方账户、金额和限额,包含取款 + 存款原子操作
业务规则:
密码错误 3 次自动锁定账户
单笔转账上限 5000 元
不允许负金额交易
锁定账户无法进行任何操作
*/
public class Test12 {
public static void main(String[] args) {
Bank bank = new Bank(10);
try {
// 创建账户
bank.addAccount(new Account("1001", "张三", 10000));
bank.addAccount(new Account("1002", "李四", 5000));
Account zhang = bank.findAccount("1001");
// 正常操作
zhang.saveMoney(2000);
zhang.getMoney(3000, "123456");
// 测试异常场景
try {
zhang.getMoney(20000, "123456");
} // 余额不足
catch (NotEnoughException e) {
System.out.println("异常:" + e.getMessage());
}
try {
bank.transferMoney("1001", "1002", 60000, "123456");
} // 转账超限
catch (TransferLimitException e) {
System.out.println("异常:" + e.getMessage());
}
try {
zhang.getMoney(1000, "111");
} // 密码错误
catch (Exception e) {
System.out.println("异常:" + e.getMessage());
}
try {
zhang.getMoney(1000, "111");
} // 锁定账户
catch (Exception e) {
System.out.println("异常:" + e.getMessage());
}
try {
zhang.getMoney(1000, "111");
} // 锁定账户
catch (AccountLockedException e) {
System.out.println("异常:" + e.getMessage());
}
try {
zhang.getMoney(1000, "111");
} // 锁定账户
catch (AccountLockedException e) {
System.out.println("异常:" + e.getMessage());
}
} catch (Exception e) {
System.out.println("系统错误:" + e.getMessage());
}
}
}

View File

@@ -0,0 +1,8 @@
package com.inmind.custom_exception06;
// 自定义异常4转账超限异常
public class TransferLimitException extends Exception{
public TransferLimitException(String message) {
super(message);
}
}