Java Savings Program help

Write a program that establishes two savings accounts with saver1 having account number 10002 with an initial balance of $2,000, and saver2 having account 10003 with an initial balance of $3,000. Set a common rate of interest at 5% per year. At the end of each month, update the balance by adding one months interest to the balance, so that the balance compounds monthly. Print an output that displays the month number and the account number and the balance for each of the two accounts. Use month 0 to display the initial balances, month 1 to display the balances after the first months interest, and so on. At the end of the year, display the total balance for both accounts combined, like this:

 

Output:

Monthly balances for one year with 0.05 annual interest:

Month Account #   Balance Account #   Balance
----- ---------   ------- ---------   -------
    0     10002   2000.00     10003   3000.00
    1     10002   2008.33     10003   3012.50
    2     10002   2016.70     10003   3025.05
    3     10002   2025.10     10003   3037.66
    4     10002   2033.54     10003   3050.31
    5     10002   2042.02     10003   3063.02
    6     10002   2050.52     10003   3075.79
    7     10002   2059.07     10003   3088.60
    8     10002   2067.65     10003   3101.47
    9     10002   2076.26     10003   3114.39
   10     10002   2084.91     10003   3127.37
   11     10002   2093.60     10003   3140.40
   12     10002   2102.32     10003   3153.49

Final balance of both accounts combined: 5255.81

Use two classes, a SavingsAccount class and a YourNameProg4 as your Driver class.
 8
 
SavingsAccount.java class

In the SavingsAccount class, declare a class variable called annualInterestRate, an instance constant called ACCOUNT_NUMBER, and an instance variable called balance. Provide a two-parameter constructor to initialize the instance constant and instance variable, and provide accessors for the instance constant and instance variable. Provide an addMonthlyInterest method to update the balance, by adding (balance * annualInterestRate / 12) to the current balance.

 

You are also required to provide a class method that sets the annual interest rate.

 
 32
 
YourNameProg4.java class

In the YourNameProg4 Driver class, instantiate the saver1 and saver2 objects. Set the annual interest rate at 0.05. Print the table heading lines. Use a for loop to print the initial account numbers and balances and the account numbers and balances each month after that months interest has been added. After the last months printout, compute and display the total of both balances.    
 24
 
Your driver should generate the following outputs:

 

Output:

Monthly balances for one year with 0.05 annual interest:

Month Account #   Balance Account #   Balance
----- ---------   ------- ---------   -------
    0     10002   2000.00     10003   3000.00
    1     10002   2008.33     10003   3012.50
    2     10002   2016.70     10003   3025.05
    3     10002   2025.10     10003   3037.66
    4     10002   2033.54     10003   3050.31
    5     10002   2042.02     10003   3063.02
    6     10002   2050.52     10003   3075.79
    7     10002   2059.07     10003   3088.60
    8     10002   2067.65     10003   3101.47
    9     10002   2076.26     10003   3114.39
   10     10002   2084.91     10003   3127.37
   11     10002   2093.60     10003   3140.40
   12     10002   2102.32     10003   3153.49

Final balance of both accounts combined: 5255.81
 
bigshane50Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kevin CrossChief Technology OfficerCommented:
What is the question?  Are you getting an error in your classes compiling or are the results not coming out as they should?  Please post what you have already and describe what you are stuck on.
0
bigshane50Author Commented:
Here is what I have...It works but it does not go inline with what the teacher is requesting
/******************************************************************************
 * SavingsAccount.java
 * 
 * Shane Bartholomew
 * 
 * This program works with ShaneBartholomewProg4.java to establish two savings
 * accounts with saver1 having account number 10002 with an initial balance of 
 * $2,000, and saver2 having account 10003 with an initial balance of $3,000. 
 * Settting a common rate of interest at 5% per year. At the end of each month, 
 * it will update the balance by adding one months interest to the balance, 
 * so that the balance compounds monthly. Finally it will print an output that 
 * displays the month number and the account number and the balance for each 
 * of the two accounts.
 ******************************************************************************/
 
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.math.MathContext;
 
public class SavingsAccount {
 
    public static void main(String[] argv) throws Exception{
    }
 
    public static BigDecimal Calculate(BigDecimal balance, BigDecimal interest){
        //Declare variables for the math calculations
        BigDecimal numMonth = new BigDecimal(12);
        BigDecimal monthlyAPR = new BigDecimal(0.00);
        BigDecimal newbalance = new BigDecimal(0.00);
        MathContext size = new MathContext(7);
 
        //Perform math calculations to determine monthly interest
        interest = interest.setScale(2, RoundingMode.HALF_UP);
        monthlyAPR = balance.multiply(interest).divide(numMonth, size).setScale(2, RoundingMode.HALF_UP);
        newbalance = balance.add(monthlyAPR).setScale(2, RoundingMode.HALF_UP);
 
        //Return the new balance back to Main Prog for output
        return newbalance;
    }//End Calculate
}
 
 
AND the driver
 
/******************************************************************************
 * ShaneBartholomewProg4.java
 * 
 * Shane Bartholomew
 * 
 * This program establishes two savings accounts with saver1 having account 
 * number 10002 with an initial balance of $2,000, and saver2 having account 
 * 10003 with an initial balance of $3,000. Set's a common rate of interest at 
 * 5% per year. At the end of each month, it will update the balance by adding 
 * one months interest to the balance, so that the balance compounds monthly. 
 * Finally it will print an output that displays the month number and the 
 * account number and the balance for each of the two accounts.
 ******************************************************************************/
 
import java.math.BigDecimal;
import java.math.RoundingMode;
 
 
public class ShaneBartholomewProg4 {
 
    public static void main(String[] argv) throws Exception{
        //Prints the table heading lines.
        System.out.println("Monthly balances for one year with 0.05 annual interest:" + "\r\n");
        System.out.printf("%2s %14s \t %8s %1s \t %8s \n","Month","Account #","Balance","Account #","Balance");
        System.out.printf("%2s %14s \t %8s %1s \t %8s \n","-----","---------","-------","---------","-------");
 
      /* Declare variables for saver1 and saver2 accounts, also using BigDecimal
       * rather than double will allow the programmmer access to math functions 
       * embedded in java */
        int Months = 12;
        String saver1 = "10002";
        String saver2 = "10003";
        BigDecimal sav1rnd = new BigDecimal(2000.00);
        BigDecimal sav2rnd = new BigDecimal(3000.00);
 
        //Interest rate and total variables with setscale for 2 decimal places
        BigDecimal apr = new BigDecimal(0.05);
        BigDecimal Total = new BigDecimal(0.00);
        sav1rnd = sav1rnd.setScale(2, RoundingMode.HALF_UP);
        sav2rnd = sav2rnd.setScale(2, RoundingMode.HALF_UP);
 
        /* A for loop to print the initial account numbers and balances and 
         * the account numbers and balances each month after that months 
         * interest has been added.*/
         
        for(int i = 0;i < Months; i++){
            System.out.printf("%5s %14s \t %8s %8s \t %8s \n",i,saver1,sav1rnd,saver2,sav2rnd);
            sav1rnd = (SavingsAccount.Calculate(sav1rnd,apr));
            sav2rnd = (SavingsAccount.Calculate(sav2rnd,apr));
            Total = sav1rnd.add(sav2rnd);
        }
 
        //Last months printout, compute and display the total of both balances.
        System.out.println("\n" + "Final balance of both accounts combined: " + Total);
 
        //Exit program
        System.exit(0);
    }//End Main
}

Open in new window

0
sirtamCommented:
I see you're using a 'main' in both classes.

Did you have another class to drive both these classes?

If so, drop the main in both and use the starting class to make object to these two.
None of the two provided classes is communicating, so please provide the rest of the code if possible
0
Become a CompTIA Certified Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

bigshane50Author Commented:
this is all I have...It prints out right, but I need help curtailing it to the above requirements.
0
Kevin CrossChief Technology OfficerCommented:
Firstly start with the structure of your SavingsAccount per the instructions of your assignment.  Please find below a stub that you will need to fill in with code.  I have made comments to help guide you.  Feel free to use BigDecimal instead of double, it is just what I typed off hand.
public class SavingsAccount {
	// define class variables using static keyword.
	private static double annualInterestRate;
 
	/**
	 * Set the value of the annual interest rate for all SavingsAccount instances.
	 *  
	 * @param annualInterestRate
	 */
	public static void setAnnualInterestRate(double annualInterestRate) {
		// set value of annualInterestRate variable
	}
	
	// define instance variables/constants
	private final String ACCOUNT_NUMBER; // constants defined using final keyword.
	private double balance;
 
	/**
	 * Two parameter constructor!
	 * 
	 * @param accountNumber
	 * @param beginningBalance
	 */
	public SavingsAccount(String accountNumber, double beginningBalance) {
		// TODO: set values of instance variables here!
	}
 
	/**
	 * @return current balance.
	 */
	public double getBalance() {
		
	}
 
	/**
	 * Set current balance.
	 * 
	 * @param balance 
	 */
	public void setBalance(double balance) {
		
	}
 
	/**
	 * Get the account number of current instance of SavingsAccount.
	 */
	public String getACCOUNT_NUMBER() {
		
	}
	
	/**
	 * update the balance, by adding (balance * annualInterestRate / 12) to the current balance. 
	 */
	public void addMonthlyInterest() {
		
	}
}

Open in new window

0
Kevin CrossChief Technology OfficerCommented:
Then you use your SavingsAccount class in your driver program's main method like this (again, left comments where you can fill in your own code so you learn the concept versus just getting answer).
public class ShaneBartholomewProg4 {
 
	public static void main(String[] argv) throws Exception {
		// Set the annual interest rate to 0.05
		SavingsAccount.setAnnualInterestRate(0.05);
 
		// Instantiate two SavingsAccount objects
		SavingsAccount saver1 = new SavingsAccount("10002", 2000);
		SavingsAccount saver2 = new SavingsAccount("10003", 3000);
		
		// Optionally, use NumberFormat to format output of balance.
 
		// Prints the table heading lines.
		
		System.out.printf("%2s %14s \t %8s %1s \t %8s \n", "Month",
				"Account #", "Balance", "Account #", "Balance");
		System.out.printf("%2s %14s \t %8s %1s \t %8s \n", "-----",
				"---------", "-------", "---------", "-------");
		
		// Print initial balances of both accounts for month 0.
		
		for (int month=1; month<=12; month++) {
			// Compound interest for current month for each saver object
			
			// Print monthly balances
		}
		
		// Display final total.
 
		// Exit program
		System.exit(0);
	}// End Main
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bigshane50Author Commented:
Thanks for all the help...I can manage from here.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.