Alex
Alex

Reputation: 434

Java - IF Statement only Returning the Else Clause

So the issue is with my withdraw method, for some reason, my code's outputting:

ADL
826501829
250.0
471.0
Balance is currently:40.0
There aren't enough funds.
There aren't enough funds.

I'm not very familiar with Java, so can't quite see why it's not outputting the right option from the IF statement

public class BankAcc {

    String accountName;
    int accountNumber;
    float accountOverdraft;
    float currentBalance;

    public BankAcc(String accountName, int accountNumber, float accountOverdraft, float currentBalance) {
        System.out.println(accountName);
        System.out.println(accountNumber);
        System.out.println(accountOverdraft);
        System.out.println(currentBalance);
    }

    public void deposit(float depositAmnt){
        currentBalance = currentBalance + depositAmnt;
        System.out.println("Balance is currently:" + currentBalance);
    }

    public void withdraw(float withdrawAmnt){
        if (withdrawAmnt <= currentBalance){
            currentBalance = currentBalance - withdrawAmnt;
            System.out.println("Balance is currently:" + currentBalance);
        } else {
            System.out.println("There aren't enough funds.");
        }
    }

    public static void main(String []args){
        BankAcc myBankAcc = new BankAcc("ADL",826501829, 250, 471);
        myBankAcc.deposit(40);
        myBankAcc.withdraw(99);
        myBankAcc.withdraw(999);
    }
}

Upvotes: 0

Views: 82

Answers (4)

Your code is "working properly". You are comparing the deposit(40) with 99 or 999, so 40 - 99 will be "not enough founds" just like the 40-999.

I would recommend you to use getters and setters, because you will be get confused later on your code.

  • One more important thing save values in constructor.

Upvotes: 0

Joost K
Joost K

Reputation: 1116

You're not saving the values you put into your constructor

public BankAcc(String accountName, int accountNumber, float accountOverdraft, float currentBalance) {
    this.accountName = accountName;
    this.accountNumber = accountNumber;
    this.accountOverdraft = accountOverdraft;
    this.currentBalance = currentBalance;

    System.out.println(accountName);
    System.out.println(accountNumber);
    System.out.println(accountOverdraft);
    System.out.println(currentBalance);
}

Upvotes: 0

Stultuske
Stultuske

Reputation: 9437

public BankAcc(String accountName, int accountNumber, float accountOverdraft, float currentBalance) {
        System.out.println(accountName);
        System.out.println(accountNumber);
        System.out.println(accountOverdraft);
        System.out.println(currentBalance);
    }

You print the values you pass, but don't actually set them.

BankAcc myBankAcc = new BankAcc("ADL",826501829, 250, 471);

You pass 471, which should go into currentBalance, but it doesn't meaning that after your deposit of 40, and you try to extract 99, you indeed have insufficient funds.

Change your constructor to:

public BankAcc(String accountName, int accountNumber, float accountOverdraft, float currentBalance) {
    this.accountName = accountName;
    this.accountNumber = accountNumber;
    this.accountOverdraft = accountOverdraft;
    this.currentBalance = currentBalance;
}

Upvotes: 0

Hugh Natt
Hugh Natt

Reputation: 171

You need to set up your attributes in the constructor like that :

 public BankAcc(String accountName, int accountNumber, float accountOverdraft, float currentBalance) {
    this.accountName = accountName;
    this.accountNumber = accountNumber;
    this.accountOverdraft = accountOverdraft;
    this.currentBalance = currentBalance;
}

The "this" designates the current class from which you access the attributes.

Upvotes: 4

Related Questions