You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
982 B
31 lines
982 B
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());
|
|
}
|
|
}
|
|
}
|