Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.
Add your voice to the tech community where 5M+ people just like you are talking about what matters.
/******************************************************************************
* 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
}
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() {
}
}
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
}
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.