From 6dbbd684f98db63b947e93447f8961d9d02ef9c2 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Wed, 4 Feb 2026 15:27:23 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9B=E9=98=B6day05-=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=E5=BC=82=E5=B8=B8-=E9=93=B6=E8=A1=8C=E6=A1=88?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../inmind/custom_exception06/Account.java | 52 ++++++++++++ .../AccountLockedException.java | 7 ++ .../com/inmind/custom_exception06/Bank.java | 45 ++++++++++ .../InvalidAmountException.java | 7 ++ .../NotEnoughException.java | 8 ++ .../com/inmind/custom_exception06/Test12.java | 82 +++++++++++++++++++ .../TransferLimitException.java | 8 ++ 7 files changed, 209 insertions(+) create mode 100644 javaSE-day05/src/com/inmind/custom_exception06/Account.java create mode 100644 javaSE-day05/src/com/inmind/custom_exception06/AccountLockedException.java create mode 100644 javaSE-day05/src/com/inmind/custom_exception06/Bank.java create mode 100644 javaSE-day05/src/com/inmind/custom_exception06/InvalidAmountException.java create mode 100644 javaSE-day05/src/com/inmind/custom_exception06/NotEnoughException.java create mode 100644 javaSE-day05/src/com/inmind/custom_exception06/Test12.java create mode 100644 javaSE-day05/src/com/inmind/custom_exception06/TransferLimitException.java diff --git a/javaSE-day05/src/com/inmind/custom_exception06/Account.java b/javaSE-day05/src/com/inmind/custom_exception06/Account.java new file mode 100644 index 0000000..167a72d --- /dev/null +++ b/javaSE-day05/src/com/inmind/custom_exception06/Account.java @@ -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); + } + + +} diff --git a/javaSE-day05/src/com/inmind/custom_exception06/AccountLockedException.java b/javaSE-day05/src/com/inmind/custom_exception06/AccountLockedException.java new file mode 100644 index 0000000..17f5e8f --- /dev/null +++ b/javaSE-day05/src/com/inmind/custom_exception06/AccountLockedException.java @@ -0,0 +1,7 @@ +package com.inmind.custom_exception06; +// 自定义异常3:账户锁定异常 +public class AccountLockedException extends Exception{ + public AccountLockedException(String message) { + super(message); + } +} diff --git a/javaSE-day05/src/com/inmind/custom_exception06/Bank.java b/javaSE-day05/src/com/inmind/custom_exception06/Bank.java new file mode 100644 index 0000000..1db403e --- /dev/null +++ b/javaSE-day05/src/com/inmind/custom_exception06/Bank.java @@ -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 + "元成功"); + } +} \ No newline at end of file diff --git a/javaSE-day05/src/com/inmind/custom_exception06/InvalidAmountException.java b/javaSE-day05/src/com/inmind/custom_exception06/InvalidAmountException.java new file mode 100644 index 0000000..80f1a3c --- /dev/null +++ b/javaSE-day05/src/com/inmind/custom_exception06/InvalidAmountException.java @@ -0,0 +1,7 @@ +package com.inmind.custom_exception06; +// 自定义异常2:无效金额异常 +public class InvalidAmountException extends Exception{ + public InvalidAmountException(String message) { + super(message); + } +} diff --git a/javaSE-day05/src/com/inmind/custom_exception06/NotEnoughException.java b/javaSE-day05/src/com/inmind/custom_exception06/NotEnoughException.java new file mode 100644 index 0000000..dcdb257 --- /dev/null +++ b/javaSE-day05/src/com/inmind/custom_exception06/NotEnoughException.java @@ -0,0 +1,8 @@ +package com.inmind.custom_exception06; + +//余额不足异常 +public class NotEnoughException extends Exception{ + public NotEnoughException(String message) { + super(message); + } +} diff --git a/javaSE-day05/src/com/inmind/custom_exception06/Test12.java b/javaSE-day05/src/com/inmind/custom_exception06/Test12.java new file mode 100644 index 0000000..ab87cb8 --- /dev/null +++ b/javaSE-day05/src/com/inmind/custom_exception06/Test12.java @@ -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()); + } + } +} diff --git a/javaSE-day05/src/com/inmind/custom_exception06/TransferLimitException.java b/javaSE-day05/src/com/inmind/custom_exception06/TransferLimitException.java new file mode 100644 index 0000000..1abf6a5 --- /dev/null +++ b/javaSE-day05/src/com/inmind/custom_exception06/TransferLimitException.java @@ -0,0 +1,8 @@ +package com.inmind.custom_exception06; + +// 自定义异常4:转账超限异常 +public class TransferLimitException extends Exception{ + public TransferLimitException(String message) { + super(message); + } +}