Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The solution works, but the iteration keeps looping to month 1 within the for lo

ID: 3533669 • Letter: T

Question

The solution works, but the iteration keeps looping to month 1 within the for loop. I need to find out how to get the following Output: Enter the savings account's balance: 10000 Enter the savings account's annual interest rate: .02 How many months have passed since the account was opened? 3 Enter the amount deposited during month 1: 1200 Enter the amount withdrawn during month 1: 900 Enter the amount deposited during month 2: 1500 Enter the amount withdrawn during month 2: 1100 Enter the amount deposited during month 3: 2000 Enter the amount withdrawn during month 3: 1800 Total Deposited: $4,700.00 Total withdrawn: $3,800.00 Interest Earned: $53.25 Ending Balance: $10,953.25 What I need help with is having the program iterate 3 times because the user entered 3 for how many months the account has been opened. And the set precision for dollar amounts. This is an intro to Java class so simple code is needed here, I wouldn't understand any advanced code. Thank You!

Explanation / Answer

/**
* SavingsAccount Class
* This class sets up the SavingsAccount class
*/

import java.util.Scanner; // Needed for Scanner
import java.io.*; // Needed for File and IOException

public class SavingsAccount
{
private double balance; // Account Balance
private double withdraw; // Account Withdrawals
private double deposit; // Account Deposits
private double interestRate; // Account annual InterestRate
private double interest; // Account monthly Interest
private double totalDeposits;
private double totalWithdrawn;

/**
* The constructor initializes the balance and interestRate fields
* with the values passed to startBalance and intRate. The interest
* field is assigned 0.0.
*/

public SavingsAccount (double startBalance,
double intRate)
{
balance = startBalance;
interestRate = intRate;
interest = 0.00;
}

// The getBalance method returns the value in the balance field.

public double getBalance()
{
return balance;
}

// The setDeposit method accepts the argument that is stored in
// the deposit field.



// The addDeposit method add the deposit to the balance.

public void addDeposit(double dep)
{deposit = dep;
balance = (balance + dep);
totalDeposits = totalDeposits + deposit;
}


//The subtractWithdraw method subtracts the withdraw from the
// balance.

public void subtractWithdraw(double withAmount)
{withdraw = withAmount;
balance = (balance - withdraw);
totalWithdrawn = totalWithdrawn + withdraw;

}

// The getInterestRate method returns the value in the interestRate
// field.

public double getInterestRate()
{
return interestRate;
}

// The calculateInterest method calculates the monthly interest and adds
// it to the account balance.

public void calculateInterest()
{
interest = (interestRate /100./ 12);
interest = (balance * interest);
}

// The addInterest method adds the moInterest to the

public void addInterest()
{
balance = (balance + interest);
}

// The getInterest method returns the value in the interest field.

public double getInterest()
{
return interest;
}

// The addTotalDeposits method adds all deposits for the months the account
// has been active.


// The getTotalDeposits method returns the value in totalDeposits.
public double getTotalDeposits()
{
return totalDeposits;
}



// The getTotalWithdrawn method returns the value in totalWithdrawn.

public double getTotalWithdrawn()
{
return totalWithdrawn;
}
}

------------------------------------------------------------------------------------------------

/**
* SavingsAccountTest class
* This class sets up the driver class for the SavingsAccount.
*/

import java.util.Scanner; // Needed for Scanner
import java.io.*; // Needed for File and IOException
import java.text.DecimalFormat; // Needed to format the decimals in variable values.

public class SavingsAccountTest
{
public static void main(String[] args) throws IOException
{
SavingsAccount account;
double bal = 0.00;
double intRate = 0.00;
int months = 0;
double depAmount;
double withAmount;
double totalDeposits = 0.00;
double totalWithdrawn = 0.00;

// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);


// Create a Decimal Format object to set $ amouts display.
DecimalFormat dollar = new DecimalFormat("##,##0.00");

// Get the starting balance.
System.out.println("Enter the savings account's starting balance: ");
bal = keyboard.nextDouble();

// Get the annual interest rate.
System.out.println("Enter the savings account's annual interest rate: ");
intRate = keyboard.nextDouble();

// Create a SavingsAccount object.
account = new SavingsAccount(bal, intRate);

// Get the number of month the savings account has been open.
System.out.println("How many months have passed since the account "
+ "was opened? :");
months = keyboard.nextInt();

// Process the information for the months.
for (int month = 1; month <= months; month++)
{

// Get the amount of the deposit.
System.out.println("Enter the amount deposited during month "
+ month + ": ");
depAmount = keyboard.nextDouble();
account.addDeposit(depAmount);

// Running total for deposits.
totalDeposits = (totalDeposits + depAmount);

// Get the amount of the withdraw.
System.out.println("Enter the amount withdrawn during month "
+ month + ": ");
withAmount = keyboard.nextDouble();
account.subtractWithdraw(withAmount);

// Calculate the interest for the month.
account.calculateInterest();
account.addInterest();

}

// Display the total deposited.

System.out.println("Total deposited: $" + dollar.format(account.getTotalDeposits()));


// Display the total withdrawn.

System.out.println("Total withdrawn: $" + dollar.format(account.getTotalWithdrawn()));

// Display the ending balance.
System.out.println("Ending balance: $" + dollar.format(account.getBalance()));
}
}