I just wrote this simple methode that must return a double value. It’s a function to withdraw money from the bank account. The function must let the user get the money only if he entries a value smaller than his the amount of money he has in his account.
There is a condition where, if he is a special user, he can take the money even if he tries a value = balance+1000.
As I’m a beginner to java, I’m not yet familiar with this thing of types (I’m a php programmer, so I never really had to worry about this, but I’m getting troubles to make this code work, because I’m returning a double, if it’s ok, but a false, if it’s not, and I can’t do this in java.. This is my methode.. (the variables ‘balance’ and ‘special’ belongs to my class).
public double getMoney (double value) {
if (value <= balance) {
balance = balance - value;
} else {
if (special == true && value < (balance+1000)) {
return balance-value;
} else {
return false;
}
}
}
I know it’s a noob question, but I just started studying java and I was used to code using PHP, where I don’t need to worry about the type of my variables, so I was wondering if you could give me some advices.
Thanks
You could return Double.NEGATIVE_INFINITY, instead of false.
Then the caller explictly checks for that value, and detects ist as invalid withDrawn().
The caller checks that by
If you want to do that more professional you would have to retun a transactionInfo:
Instead of the if you could use a switch statement: