public class BankAccount{
private double balance;
private boolean closed;
public BankAccount(){
balance = 0;
closed = false;
}
public double getBalance(){
return balance;
}
// deposit requires the following condition
// amount>=0 && !isClosed()
public void deposit(double amount) {
if (closed) {
System.out.println("Cannot deposit because the account is closed!");
return; // throwing an exception would be better
}
if (amount>=0) {
balance = balance + amount;
}
}
// withdraw requires the following condition
// getBalance()>0 && amount>0 && getBalance() - amount>=0 && !isClosed()
public void withdraw(double amount) {
// error!
// if (closed) {
// System.out.println("Cannot withdraw because the account is closed!");
// return; // throwing an exception would be better
// }
if (balance>0 && amount>0 && balance-amount>=0) {
balance = balance - amount;
}
}
public void close() {
if (balance>=0) {
withdraw (balance);
closed = true;
}
}
public boolean isClosed() {
return closed;
}
}