I'm trying to construct an else-if statement here and am getting a little confused with the "return" values and the boolean result that we get from the condition.
Is it possible to have two statements return true in this case or will it exit after the first block?
Is there a better way to write this? (maybe using a switch?)
This code works with withdrawals of a checking account. I want it to return false if the "amount" to be withdrawn is == or < 0. But return true if "amount" to be withdrawn > 0. And also check and return true if "amount" to be withdrawn > balance. (but only one time! Since I want to allow an account to overdrawn only 1 time.)
public boolean withdraw(double amount) {
//amount = amount of money asked to be withdrawn
if (amount > balance) {
setBalance(balance - amount - overdraftFee);
return true;
}
else if (amount == 0) {
System.out.println("Withdrawal amount cannot be $0.00!");
return false;
}
else if (amount < 0) {
System.out.println("Withdrawal amount cannot be a negative amount!");
return false;
}
else {
setBalance(balance - amount);
return true;
}
}
Aucun commentaire:
Enregistrer un commentaire