I was working on an exercise in a Java programming book for beginners where I had to create a bank account with a withdraw limit of up to -1000 Euros. Somehow, even though the amount is -1000 Euros, the code executes the else
code, which for me makes little sense. It shouldn't output the error in my opinion, but it does.
Here is the account code:
public class Account {
private String accountnumber;
protected double accountbalance;
Account(String an, double ab) {
accountnumber = an;
accountbalance = ab;
}
double getAccountbalance() {
return accountbalance;
}
String getAccountnumber() {
return accountnumber;
}
void deposit(double ammount) {
accountbalance += ammount;
}
void withdraw(double ammount) {
accountbalance -= ammount;
}
}
The extended account:
public class GiroAccount extends Account{
double limit;
GiroAccount(String an, double as, double l) {
super(an, as);
limit = l;
}
double getLimit() {
return limit;
}
void setLimit(double l) {
limit = l;
}
void withdraw(double ammount) {
if ((getAccountbalance() - ammount) >= limit) {
super.withdraw(ammount);
} else {
System.out.println("Error - Account limit is exceded!");
}
}
}
The code to test the accounts:
public class GiroAccountTest {
public static void main(String[] args) {
GiroAccount ga = new GiroAccount("0000000001", 10000.0, -1000.0);
ga.withdraw(11000.0);
System.out.println("Balance: " + ga.getAccountbalance());
ga.deposit(11000.0);
ga.withdraw(11001.0);
System.out.println("Balance: " + ga.getAccountbalance());
}
}
The output:
Balance: -1000.0
Error - Account limit is exceded!
Balance: 10000.0