YO,

I would be EXTREMELY grateful if someone can help me out with some boolean method stuff. I have this assignment where we've been building up this bank account project thing, and at the moment I've been asked to put in a boolean method called inBalance that returns true if the balance is in credit. I thought I had this down but I'm getting an error saying 'missing return statement'. I also need to put something in called canWithdraw that prevents money being drawn if there's enough in the balance, and with that one I don't even know where to begin. I hope it's not going to be too daunting if I paste the whole thing since as a total beginner it'd prolly be best to see everything that's there since I hardly understand what's going on haha. ANY advice would be so greatly appreciated.

package bankaccountex;
import java.util.Date;

public class BankAccount {
    private double balance;
    private double interestRate; // interest Rate for the account
    private int accountNumber;
    private double transFee = .1;
    private int transNumber;
    Date transDate;


    //first constructor
    public BankAccount(int number) {
        balance = 0;
        interestRate = 0.06; // all the accounts have an 6% interest Rate
        accountNumber = number;
        transNumber = 0;


    }
    //second constructor
    public BankAccount(int number, double initialBalance) {
        balance = initialBalance;
        interestRate = 0.06; // all the accounts have an 6% interest Rate
        accountNumber = number;
        transNumber = 0;


    //methods
    }

    public double getBalance() {
            return balance;
    }

    public int showAccountNumber() {
            return accountNumber;
    }

    public int showTransNumber() {
            return transNumber;
    }

    public void deposit (double amount) {
        balance = balance + amount;
        transNumber = transNumber + 1;
        transDate = new Date();

    }

    public void withdraw (double amount) {
        balance = balance - amount;
        transNumber = transNumber + 1;
        transDate = new Date();


    }

    public void deductMonthlyCharge () {
        if (transNumber > 10) balance = balance - (transNumber - 10)  * transFee;
        transNumber = 0;

    }

    public double calculateInterest(){
        return  balance * interestRate;
    }

     public boolean inBalance() {
        if (balance > 0) {
            return true;
        }
    }
    public boolean canWithdraw(double amount) {
        if (balance > ???) {
            return true; 
    }
    }
}

Well, the compiler complains because you have not accounted for all the cases in your boolean functions.

your functions look like:
public boolean first() {
if(something) {
return anotherthing
}
}


what if the condition fails ?. How will the compiler know what to do if the balance <=0 . You must account for the else case as well.

Well, the compiler complains because you have not accounted for all the cases in your boolean functions.

your functions look like:
public boolean first() {
if(something) {
return anotherthing
}
}


what if the condition fails ?. How will the compiler know what to do if the balance <=0 . You must account for the else case as well.

Thanks so much dude, that's taken care of the issue of the first one. Really appreciate your help. Do you by any chance have advice to spare regarding the second one or even umm how and where to use this in the other class?

Thank youuu!

Not sure what other class you're talking about, but as to your second question, replace ??? with amount.

If you post code again in the future, please enclose it in code tags.

Not sure what other class you're talking about, but as to your second question, replace ??? with amount.

If you post code again in the future, please enclose it in code tags.

not sure i phrased it right with 'other class' but basically the other tab/page in the project where ive written the code for creating bank accounts and carrying out the methods. the trouble is the amount is whatever amount is entered manually in that other page using the withdraw method

Ok, I think by other class you mean somewhere where you test your BankAccount, right? If you are having problems with your test class, then post the code and ask a question. I don't understand what you mean by "the trouble is the amount is whatever amount is entered manually in that other page using the withdraw method"

In my previous post, what I meant was, you have included this code in your original post.

public boolean canWithdraw(double amount) {
  if (balance > ???) {
    return true;
  }
}

This is where I am saying you should replace ??? with amount.

Ok, I think by other class you mean somewhere where you test your BankAccount, right? If you are having problems with your test class, then post the code and ask a question. I don't understand what you mean by "the trouble is the amount is whatever amount is entered manually in that other page using the withdraw method"

In my previous post, what I meant was, you have included this code in your original post.

public boolean canWithdraw(double amount) {
  if (balance > ???) {
    return true;
  }
}

This is where I am saying you should replace ??? with amount.

ohhhh i see what you're saying, so would this canWithdraw method replace the withdraw method as a more efficient form of the same thing?

No, one doesn't replace the other. You need to use both. You would do something like this. Say you have a bank account called savings that you have created and deposited some money. Now you want to do a withdrawal.

if (savings.canWithdraw(20)) {
      savings.withdraw(20);
   }

This will prevent the account balance from going negative.

No, one doesn't replace the other. You need to use both. You would do something like this. Say you have a bank account called savings that you have created and deposited some money. Now you want to do a withdrawal.

if (savings.canWithdraw(20)) {
      savings.withdraw(20);
   }

This will prevent the account balance from going negative.

awesome, thank you SO much! i really appreciate the help! :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.