class LimitException extends Exception {
private Double remainingAmount;
public LimitException(String message, Double remainingAmount) {
super(message);
this.remainingAmount = remainingAmount;
}
public Double getRemainingAmount() {
return remainingAmount;
}
}
class BankAccount {
private Double amount;
public BankAccount(Double amount) {
this.amount = amount;
}
public Double getAmount() {
return amount;
}
public void deposit(Double sum) {
amount += sum;
}
public void withDraw(Double sum) throws LimitException {
if (sum > amount) {
throw new LimitException("Not enough funds on the account", amount);
}
amount -= sum;
}
}
class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount(20000.0);
account.deposit(20000.0);
try {
while (true) {
account.withDraw(6000.0);
System.out.println("Withdrawn 6000");
}
} catch (LimitException e) {
System.out.println("LimitException caught: " + e.getMessage());
Double remainingAmount = e.getRemainingAmount();
account.withDraw(remainingAmount);
System.out.println("Withdrawn remaining " + remainingAmount);
break;
}
}
}