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("==========================================");
}
}