I'm working on a basic "banking" program. The most recent change I've made to it is to use inheritance so that instead of just an Account, you can make either a checking or savings account specifically. Everything is working fine, except that the checking account is supposed to have a transaction fee. For some reason, my override function to set the balance including the transaction fee isn't working, if my balance is 0 and my fee is 2.00, a 5.00 deposit results in output of -2.00. So it's subtracting the fee, but not adding the deposit.

Thanks in advance for any help.

package inheritance;

import inheritance.Account;
import java.util.Scanner;



public class Main {



    public static void main(String[] args) {
          Scanner input = new Scanner( System.in);

          float changeAmount, oldBalance, newBalance, fee, iRate;
          int num1;
          String checkingName, savingsName;

          fee = 0;
          iRate = 0;
          newBalance = 0;
          checkingName = "abc";
          savingsName = "def";
          CheckingAccount myCheckingAccount = new CheckingAccount(checkingName, 0, fee);
          SavingsAccount mySavingsAccount = new SavingsAccount(savingsName, 0, fee);

    System.out.println("Please select how you would like to manage your balances:\n"); //Get user's choice
    System.out.println("1. Create Accounts");
    System.out.println("2. Checking Account Deposit");
    System.out.println("3. Checking Account Withdrawal");
    System.out.println("4. Checking Account Balance Query");
    System.out.println("5. Savings Account Deposit");
    System.out.println("6. Savings Account Withdrawal");
    System.out.println("7. Savings Account Balance Query");
    num1 = input.nextInt();

   switch (num1) {

       case 1: //Create one checking and one savings account
           System.out.println("Enter the name of your checking account:");
           checkingName = input.next();
           System.out.println("Enter the transaction fee for this account:");
           fee = input.nextFloat();
           myCheckingAccount.setAccountName(checkingName);
           myCheckingAccount.setFee(fee);
           System.out.println("Enter the name of your savings account:");
           savingsName = input.next();
           System.out.println("Enter the interest rate for this account (in decimal form):");
           iRate = input.nextFloat();
           mySavingsAccount.setAccountName(savingsName);
           mySavingsAccount.setInterestRate(iRate);
         
           //TEST CODE
           /*
           System.out.println(myCheckingAccount.getName());
           System.out.println(myCheckingAccount.getBalance());
           System.out.println(mySavingsAccount.getName());
           System.out.println(mySavingsAccount.getBalance());  */
           break;
       case 2: //Checking Deposit
           System.out.println("Enter the amount to be deposited to the account:");//Get changeAmount
           changeAmount = input.nextFloat();

           oldBalance = myCheckingAccount.getBalance();
           newBalance = oldBalance + changeAmount;
           myCheckingAccount.setBalance(newBalance);

           System.out.print("The new balance is: ");
           System.out.println (myCheckingAccount.getBalance());
           break;
       case 3: //Checking Withdrawal
           System.out.println("How much would you like to withdraw?");
           changeAmount = input.nextFloat();

           oldBalance = myCheckingAccount.getBalance();
           newBalance = oldBalance - changeAmount;
           myCheckingAccount.setBalance(newBalance);

           System.out.print("The new balance is: ");
           System.out.println (myCheckingAccount.getBalance());
           break;
       case 4: //Checking Balance Query
           System.out.print("The checking account balance is: ");
           System.out.println (myCheckingAccount.getBalance());
           break;
       case 5: //Savings Deposit
           System.out.println("Enter the ammount to be deposited to the account:");//Get changeAmount
           changeAmount = input.nextFloat();

           oldBalance = mySavingsAccount.getBalance();
           newBalance = oldBalance + changeAmount;
           mySavingsAccount.setBalance(newBalance);

           System.out.print("The new balance is: ");
           System.out.println (mySavingsAccount.getBalance());
           break;
       case 6: //Savings Withdrawal
           System.out.println("How much would you like to withdraw?");
           changeAmount = input.nextFloat();

           oldBalance = mySavingsAccount.getBalance();
           newBalance = oldBalance - changeAmount;
           mySavingsAccount.setBalance(newBalance);

           System.out.print("The new balance is: ");
           System.out.println (mySavingsAccount.getBalance());
           break;
       case 7: //Savings Balance Query
           System.out.print("The savings account balance is: ");
           System.out.println (mySavingsAccount.getBalance());
           System.out.print("Interest gained:");
           System.out.println (mySavingsAccount.CalculateInterest());
           break;
   }


   }
  }
package inheritance;

/**
 *
 * @author David
 */
public class Account {
    //Private fields
    private float Balance;
    private String accountName;

    //Constructor
    public Account(String Name, float Balance)
    {
        this.accountName = Name;
        this.Balance = 0;
    }

    //Accessors
    public float getBalance()
    {
        return Balance;
    }

    public String getName()
    {
        return accountName;
    }


    //Mutators
    public void setAccountName(String Name)
    {
        accountName = Name;
    }
    public void setBalance(float Balance)
    {
        this.Balance = Balance;
    }

}
package inheritance;

/**
 *
 * @author David
 */
public class CheckingAccount extends Account {

    private float transactionFee, myBalance;

    public CheckingAccount(String Name, float Balance, float transactionFee)
            {
        super(Name, Balance);
        this.transactionFee = (float) 2.00;
    }

   //Set the transaction fee for this account, the fee will apply to both withdrawals and deposits
   public void setFee(float fee)
    {
        transactionFee = fee;
    }

   @Override //Set the Balance to take the transaction fee into account
   public void setBalance(float Balance)
    {
       myBalance = super.getBalance();
       myBalance = myBalance - transactionFee;
       super.setBalance(myBalance);
    }

}
package inheritance;

/**
 *
 * @author David
 */
public class SavingsAccount extends Account {

    private float interestRate;

    //Constructor
    public SavingsAccount(String Name, float Balance, float interestRate)
            {
        super(Name, Balance);
        this.interestRate = (float) 0.02;
    }

    //Change Interest Rate
    public void setInterestRate(float interestRate)
    {
        this.interestRate = interestRate;
    }

    //Calculate the interest on the current balance
    public float CalculateInterest()
    {
        return super.getBalance() * interestRate;
    }
}

You inherit the variable "balance" from the Account class, so it's not a good idea to declare a new one in CheckingAccount. Just use the inherited one, as in

public void setBalance(float Balance)
{
this.Balance = Balance - transactionFee;
}

Normally one would expect deposits to be done via something like a makeDeposit(float amount) method, which just updates the (inherited) balance.
And, finally, the Java convention is that variable names should begin with a lower case letter to help duistinguish them from class names, that should begin with an upper case.

You inherit the variable "balance" from the Account class, so it's not a good idea to declare a new one in CheckingAccount. Just use the inherited one, as in

public void setBalance(float Balance)
{
this.Balance = Balance - transactionFee;
}

Normally one would expect deposits to be done via something like a makeDeposit(float amount) method, which just updates the (inherited) balance.
And, finally, the Java convention is that variable names should begin with a lower case letter to help duistinguish them from class names, that should begin with an upper case.

@James

Buddy the Balance variable is declared private so no way this is going to be inherited.
Also in the setBalance function in checkingAccount class

myBalance = super.getBalance();


This calls the function from supercalss and inside getBalance() in super it uses the variable Balance, which is set to zero in the constructor.

Hope my reply suffices to your resolution.

OOps yes, you're right. It's private. My bad.
J

OOps yes, you're right. It's private. My bad.
J

I'm still a little lost. I'm not sure why it doesn't work still. I have the savings account working, but I'm still not sure about the checking. Could you maybe explain a little more? I know I have it set to 0 in the constructor but I have to don't I?

Thanks, I got it now. I added in an override method to also override the getBalance function, and it works.

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.