0

Ok,so I am getting a lot of trouble, I am still learning Java and my book has set me a task that I find common over the net, the part that I am stuck on is...

I must create a bank account program, an account holder is given a savings account (which has an interest rate and no overdraft facility), and a checking account (which has an overfraft facility of £100 and no interest).

I am not implementing the overdraft yet and am only half way to getting the withdraw and deposit function ready but my question is with the interest, I have defined in my superclass the savings account balance and the checking account balance so when working out my interest in the savings account class I cannot reference savebalance as I have made it private. I am trying to use the set.name method but i am clearly doing it wrong....

A big smile and a thank you for any one who can help or give advice!

Superclass is as follows:

public class BankDetails
    {
        private String customer;
        private String accountno;
        private double savebalance;
        private double checkbalance;


//Constructor Methods

 public BankDetails(String customerIn, String accountnoIn, double savebalanceIn, double checkbalanceIn)
        {
            customer = customerIn;
            accountno = accountnoIn;
            savebalance = savebalanceIn;
            checkbalance = checkbalanceIn;
        }

      // Get  name
      public String getcustomername()
        {
            return (customer);
        }

      // Get account number
      public String getaccountnumber()
        {
            return (accountno);
        }

       public double getcheckbalanceamount()
        {
            return (checkbalance);
        }

       public double getsavebalanceamount()
        {
            return (savebalance);
        }


public void savewithdraw(double savewithdrawAmountIn)

  {
        savebalance = savebalance - savewithdrawAmountIn;

  }

public void checkwithdraw(double checkwithdrawAmountIn)

  {
        checkbalance = checkbalance - checkwithdrawAmountIn;

  }


 public void savedeposit(double savedepositAmountIn)

  {
        savebalance = savebalance - savedepositAmountIn;

  }

public void checkdeposit(double checkdepositAmountIn)

  {
        checkbalance = checkbalance - checkdepositAmountIn;

  }




    } // End Class BankDetails

Sub Class is as follows:

    import java.util.*; 

public class Savings extends BankDetails
  {

      private String saveaccount;
      private double interest;


      public Savings(String customerIn, String accountnoIn, float interestIn, 
      String saveaccountIn, double savebalanceIn)
        {


            super (customerIn, accountnoIn, savebalanceIn, interestIn);


            saveaccount = saveaccountIn;
            interest = interestIn;

        }


      public String getsaveaccountno()
       {
           return (saveaccount);
       }

      public double getinterestamount()
       {
           return (interest);
       }


      public void interestamount(String[] args)

       {
           BankDetails.getsavebalanceamount(savebalance);
           interest = (savebalance / 100) * 1.75;


       }


       }
stacker
  • 68,052
  • 28
  • 140
  • 210
Phil
  • 1,393
  • 5
  • 23
  • 42

2 Answers2

3

Use the superclass's getSaveBalance() method to access the balance (which is suspiciously-named, since you have a savings account class, but keep the balance elsewhere).

(Currently it's getsavebalanceamount(), I assume a renaming to keep with Java conventions.)


I'd recommend using consistent CamelCase when naming your getters and setters, e.g., getInterestAmount(), getSaveAccountNo(), etc.

I recommend against commenting simple getters/setters, but if you do, use Javadoc conventions, e.g.:

/** Returns current savings account balance. */
public double getSaveBalance() { ... etc ... }

I also recommend avoid unnecessary parentheses, as currently in your getters, e.g.:

public double getSaveBalance() {
    return saveBalance; // No parens required.
}
Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • Ok thank you, suspiciously-named?? this was something I was unsure of whether to do aswell, do you think I should put savebalance and checkbalance in the appropriate sub classes and the withdraw and deposit methods in their with them, I felt it may be better to keep in the supercalsses as they are calculations that will occur to both subclasses. (I am still learning java as well as making things logical lol) – Phil Jan 14 '12 at 19:21
  • Sorry I am still not sure I understand, I thought I was using the getSaveBalance() method from my superclass in my subclass? public void interestamount(String[] args) { BankDetails.getsavebalanceamount(savebalance); interest = (savebalance / 100) * 1.75; } – Phil Jan 14 '12 at 19:28
  • @Phil `savebalance` is not the same as `getSaveBalance()`. One is a variable declared `private` in the superclass, one is a method declared `public`. – Dave Newton Jan 14 '12 at 19:29
  • @Phil `BankDetails` isn't the same as an account; a person's bank details could include any number of accounts, account types, etc. For a simple example, it likely doesn't matter--but understanding when, where, and how to create classes, separate concerns, etc. is one of the most important things to figure out. – Dave Newton Jan 14 '12 at 19:31
  • Ok, so I do not want to reference in the interest method save balance, I want to reference the getter method getsavebalanceamount() which will return to me the value for savebalance? – Phil Jan 14 '12 at 19:33
  • @Phil Correct. I'd caution against adding on the `amount` part, since it's a little redundant, and I'd strongly recommend following normal Java naming conventions. – Dave Newton Jan 14 '12 at 19:35
  • Excellent, so I am just playing with that now, how would I implement that only referencing the method and not the variable? – Phil Jan 14 '12 at 19:37
  • you could also mark your attributes as `protected` instead of `private` if you want to access them directly from your subclasses – jere Jan 14 '12 at 19:43
  • @Phil By calling the method instead of accessing the variable. – Dave Newton Jan 14 '12 at 19:44
  • @Dave it feels like you are trying to get me to work out the final part myself, I have done it now and its all working thank you, – Phil Jan 14 '12 at 19:58
  • @Phil No problem. (Well, you already had already said how to do it, by referencing the getter method :) – Dave Newton Jan 14 '12 at 20:12
  • @Phil No, the method call would be getsavebalanceamount() and you only need an instance if you're not in the class (or a subclass). – Dave Newton Jan 14 '12 at 21:48
2

I suggest you do something like this,

interface Account{
  int getAccountNumber();
  float getBalance();
}

public class SavingAccount implements Account, Interest{
  int accountNumber;
  public int getAccountNumber(){
    return accountNumber;
  }
  float balance;
  public float getBalance(){
    return balance;
  }
  float savingInterestRate;
  public float getInterestRate(){
     return savingInterestRate;
  }
}

public class CheckingAccount implements Account, OverDraft{
  int accountNumber;
  public int getAccountNumber(){
  return accountNumber;
  }
  float balance;
  public float getBalance(){
    return balance;
  }
}

interface Interest{
  float getInterestRate();
}

interface OverDraft{
....
}
Apurv
  • 3,723
  • 3
  • 30
  • 51