-4

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
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 4
    How you tried to use debuger? – KunLun Jul 11 '18 at 16:52
  • 1
    Welcome to SO. Please post [mcve]. I find [MCVE](https://stackoverflow.com/help/mcve) a very useful technique. Not only it makes helping much easier, it is a powerful debugging tool. It many case, while preparing one, you are likely to find the problem. – c0der Jul 11 '18 at 16:53
  • Welcome to Stack Overflow! For this sort of problem, your best bet is to step through the code in the debugger built into your IDE. That will help you understand the logic. Learning to use the debugger is a really important step for a beginning programmer. – T.J. Crowder Jul 11 '18 at 16:53
  • You should read up on comparing double and other floating numbers, here is a [similar question](https://stackoverflow.com/questions/8081827/how-to-compare-two-double-values-in-java) – Joakim Danielson Jul 11 '18 at 16:54
  • Debugger help you to understand how your code work and eventually find the problems which appear at runtime. – KunLun Jul 11 '18 at 16:56
  • 1
    Your first `withdraw` call is working, because `-1000.0 >= -1000.0` is `true`, so you see the `Balance: -1000.0` output. But your **second** one is failing, because the withdrawal amount is `11001.0`. So `(getAccountbalance() - ammount) >= limit` is `(10000.0 - 11001.0) >= -1000.0` which is `-1001.0 >= -1000.0` which is `false`. That's why the second one fails. – T.J. Crowder Jul 11 '18 at 16:59
  • 2
    @EastonBornemeier - Not in this case (see comment above). But Jannis, beware that using `double` in financial situations is not best practice (that's putting it a bit mildly), it isn't nearly precise enough. It may be good enough for a programming course, but just don't plan to use it in real life in that way. – T.J. Crowder Jul 11 '18 at 17:00
  • 2
    If you want to do 'financial' computing use `BigInteger`. `float` and `double` cannot be used for this kind of application. – M. le Rutte Jul 11 '18 at 17:01
  • @M. le Rutte it is fine to use `double`, but you have to take care of rounding the result of calculations yourself. There is a lot of discussion that can be found on this topic. – NickL Jul 11 '18 at 19:38

1 Answers1

7

Let's take step by step.

1. Start with 10000.0;
2. withdraw(11000.0) -> -1000.0
3. Print balance -> "Balance: -1000.0"
4. deposit(11000.0) -> 10000.0
5. withdraw(11001.0); -> -1001.0 < -1000.0
6. Overdrawn (enters else block) -> "Error - Account limit is exceeded"
7. Print balance -> "Balance: 10000.0"

If I'm wrong please correct me.

KunLun
  • 3,109
  • 3
  • 18
  • 65