From a96d6dae79e42c248100d021705f6ad0273dcefb Mon Sep 17 00:00:00 2001 From: GaoGeng <3123557312@qq.com> Date: Mon, 1 Jun 2026 09:12:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20'W7'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- W7/Account.java | 31 ++++++++++++++++++++++++++++++ W7/InsufficientFundsException.java | 12 ++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 W7/Account.java create mode 100644 W7/InsufficientFundsException.java diff --git a/W7/Account.java b/W7/Account.java new file mode 100644 index 0000000..8fca49f --- /dev/null +++ b/W7/Account.java @@ -0,0 +1,31 @@ +public class Account { + private double balance; + + public Account(double balance) { + this.balance = balance; + } + + public void withdraw(double amount) throws InsufficientFundsException { + if (amount > balance) { + throw new InsufficientFundsException(balance); + } + balance -= amount; + System.out.println("取款成功,当前余额: " + balance); + } + + public static void main(String[] args) { + Account account = new Account(1000.0); + + try { + account.withdraw(1500.0); + } catch (InsufficientFundsException e) { + System.out.println("取款失败: " + e.getMessage() + ",当前余额: " + e.getBalance()); + } + + try { + account.withdraw(500.0); + } catch (InsufficientFundsException e) { + System.out.println("取款失败: " + e.getMessage() + ",当前余额: " + e.getBalance()); + } + } +} \ No newline at end of file diff --git a/W7/InsufficientFundsException.java b/W7/InsufficientFundsException.java new file mode 100644 index 0000000..0dbc1f2 --- /dev/null +++ b/W7/InsufficientFundsException.java @@ -0,0 +1,12 @@ +public class InsufficientFundsException extends Exception { + private double balance; + + public InsufficientFundsException(double balance) { + super("余额不足"); + this.balance = balance; + } + + public double getBalance() { + return balance; + } +} \ No newline at end of file