4

I have an issue in my code where my input data is not printing correctly. The program asks the user to input the initial balance of each account. However, when that input is printed, the two values always print out the same. No matter what I input for the first account, it will always print out the same value as the input for the second account. The program should be printing out the separate values that were input for each account.

As well as this, the program isn't outputting the correct values for the calculated interest after it is compounded. I have looked over the code and can't find any error. Any help would be greatly appreciated.

Input data:

Enter the customer’s full name: John
Enter the number of years: 4
Enter initial balance for the saving account: 10000.05
Enter initial balance for the check account: 20000.00

Expected output:

========================================== 
Initial account balances 
========================================== 
John’s Account           Rate Balance 
========================================== 
Saving                   2.95% $10000.05 
Check                    2.10% $20000.00 
========================================== 
========================================== 
Final account balances after 4 years 
========================================== 
John’s Account          Rate   Balance 
========================================== 
Saving                  2.95%  $11250.87 
Check.                  2.10%  $21750.98 
==========================================

My Output:

========================================== 
Initial account balances 
========================================== 
John’s Account           Rate Balance 
========================================== 
Saving                   2.95% $20000.0 
Check                    2.10% $20000.0 
========================================== 
========================================== 
Final account balances after 4 years 
========================================== 
John’s Account          Rate   Balance 
========================================== 
Saving                  2.95%  $22501.623657381748 
Check.                  2.10%  $24471.619413096105 
==========================================

Here is my code:

import java.util.Scanner;

class Account{

private static double balance;

private static int NUM_COMPOUND_TIMES = 12;

Account(double Balance) { //constructor with parameter 

    balance = Balance;
}

public double getInitialBalance(){ //get the initial balance method

    return balance;
}

public double getFinalBalance(double rate, int numYears) { //get the final balance method

    balance = getInitialBalance()*Math.pow((1+(rate/NUM_COMPOUND_TIMES)), (NUM_COMPOUND_TIMES*numYears));
    return balance;
}

}

class Customer{

private String customerName; 
private static Account checkAccount;
private static Account savingAccount;


public Customer(String custName, Account savingAcc, Account checkAcc){ //constructor with parameters

    customerName = custName;

    savingAccount = savingAcc;

    checkAccount = checkAcc;

}

public Account getSavingAccount(){

    return savingAccount;
}

public Account getCheckAccount() {

    return checkAccount;
}

public String getCustomerName() {


    return customerName;
}

}

public class Bank {


private static Customer customerObj;
private static int numYears; 
private static double INTEREST_SAVING;
private static double INTEREST_CHEQUE;


public static void main(String[] args) {

    createCustomerAccounts();

    printInitialBalances();

    printFinalBalances();

}


public static void createCustomerAccounts() {

    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter the customer's full name: ");
    String custName = keyboard.nextLine();
    System.out.println("Enter the number of years: ");
    numYears = keyboard.nextInt();
    System.out.println("Enter initial balance for the savings account: ");
    double ibSavings = keyboard.nextDouble();
    System.out.println("Enter initial balance for the cheque account: ");
    double ibCheck = keyboard.nextDouble();
    keyboard.close(); 

    Account savingAcc = new Account(ibSavings);
    Account checkAcc = new Account(ibCheck);
    customerObj = new Customer(custName, savingAcc, checkAcc);  
}


public static void printInitialBalances() {

    System.out.println("==========================================");
    System.out.println("Initial account balances");
    System.out.println("==========================================");
    System.out.println(customerObj.getCustomerName() + "'s Account      Rate    Balance");
    System.out.println("==========================================");
    System.out.println("Saving          2.95%   " + "$" + customerObj.getSavingAccount().getInitialBalance());
    System.out.println("Check           2.10%   " + "$" + customerObj.getCheckAccount().getInitialBalance());
    System.out.println("==========================================");

}

public static void printFinalBalances() {

    INTEREST_SAVING = customerObj.getSavingAccount().getFinalBalance(0.0295,numYears);
    INTEREST_CHEQUE = customerObj.getCheckAccount().getFinalBalance(0.0210,numYears); 

    System.out.println("==========================================");
    System.out.println("Final account balances after " + numYears + " years");
    System.out.println("==========================================");
    System.out.println(customerObj.getCustomerName() + "'s Account      Rate    Balance");
    System.out.println("==========================================");
    System.out.println("Saving          2.95%   " + "$" + INTEREST_SAVING);
    System.out.println("Check           2.10%   " + "$" + INTEREST_CHEQUE);
    System.out.println("==========================================");
}

}

Noods
  • 445
  • 4
  • 13
  • You really shouldn't use [floating point arithmetic](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) if accuracy is required such as with currency. Consider using a class like [BigDecimal](https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html) instead – D.B. May 08 '20 at 00:02

1 Answers1

3

Your mistake is in the Account class. You have defined the balance as a static variable, which means only a single reference of the balance value is shared in memory:

private static double balance;

The result s that anytime balance is updated for any account, the value of balance is overwritten. Remove the static keyword so that the balance is local to a single instance of account:

private double balance;

Also, the format of your balance when printing out is not in proper currency format because it is printing out the the double values using the default format. An easy way to fix that is to use NumberFormat's currency formatter. To use it, add this line at the top of your Bank class to import NumberFormat:

import java.text.NumberFormat;

Then add the currency format as a variable at the top of the Bank class:

private static NumberFormat currency = NumberFormat.getCurrencyInstance();

Now, in your print balance methods you can use the currency format to get a String representation as a proper dollars/cents format, including the dollar sign:

public static void printInitialBalances() {

    String myCurrency = currency.format(123.5);
    System.out.println("==========================================");
    System.out.println("Initial account balances");
    System.out.println("==========================================");
    System.out.println(customerObj.getCustomerName() + "'s Account      Rate    Balance");
    System.out.println("==========================================");
    System.out.println("Saving          2.95%   " + currency.format(customerObj.getSavingAccount().getInitialBalance()));
    System.out.println("Check           2.10%   " + currency.format(customerObj.getCheckAccount().getInitialBalance()));
    System.out.println("==========================================");

}


public static void printFinalBalances() {

    INTEREST_SAVING = customerObj.getSavingAccount().getFinalBalance(0.0295,numYears);
    INTEREST_CHEQUE = customerObj.getCheckAccount().getFinalBalance(0.0210,numYears);

    System.out.println("==========================================");
    System.out.println("Final account balances after " + numYears + " years");
    System.out.println("==========================================");
    System.out.println(customerObj.getCustomerName() + "'s Account      Rate    Balance");
    System.out.println("==========================================");
    System.out.println("Saving          2.95%   " + currency.format(INTEREST_SAVING));
    System.out.println("Check           2.10%   " + currency.format(INTEREST_CHEQUE));
    System.out.println("==========================================");
}

Which yields you the output you were hoping for:

==========================================
Initial account balances
==========================================
John's Account      Rate    Balance
==========================================
Saving          2.95%   $10,000.05
Check           2.10%   $20,000.00
==========================================
==========================================
Final account balances after 4 years
==========================================
John's Account      Rate    Balance
==========================================
Saving          2.95%   $11,250.87
Check           2.10%   $21,750.98
==========================================
pczeus
  • 7,709
  • 4
  • 36
  • 51
  • Thank you for that. Do you know why the initial balance of the check account is only printing to one decimal place? – Noods May 07 '20 at 23:21
  • 1
    I updated the answer for you to include formatting your currencies – pczeus May 07 '20 at 23:43