Write a BankAccount class with a private balance, a daily withdrawal limit of ₹20,000 and a private transaction list; expose deposit, withdraw, getBalance and a read-only statement.
import java.util.ArrayList;
import java.util.List;
class BankAccount {
private static final double DAILY_LIMIT = 20_000;
private final String holder;
private double balance;
private double withdrawnToday;
private final List<String> transactions = new ArrayList<>();
BankAccount(String holder) { this.holder = holder; }
public boolean deposit(double amt) {
if (amt <= 0) return false;
balance += amt;
transactions.add("+" + amt);
return true;
}
public boolean withdraw(double amt) {
if (amt <= 0 || amt > balance || withdrawnToday + amt > DAILY_LIMIT) return false;
balance -= amt;
withdrawnToday += amt;
transactions.add("-" + amt);
return true;
}
public double getBalance() { return balance; }
public List<String> statement() { return List.copyOf(transactions); }
public String getHolder() { return holder; }
}
public class Main {
public static void main(String[] args) {
BankAccount acc = new BankAccount("Asha");
acc.deposit(50000);
System.out.println(acc.withdraw(15000) + " " + acc.withdraw(8000) + " " + acc.withdraw(5000) + " " + acc.deposit(-10));
System.out.println(acc.getHolder() + " " + acc.getBalance() + " " + acc.statement());
}
}
true false true false Asha 30000.0 [+50000.0, -15000.0, -5000.0]
8000 would take the day's total to 23,000, above the limit. The statement is a copy, so callers cannot rewrite history.