d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Help? (fixed)
Add Reply New Topic New Poll
Member
Posts: 5
Joined: Oct 28 2012
Gold: 0.00
Oct 28 2012 02:25pm
Here's the assignment conditions:


Design a SavingsAccount class that stores a savings account's annual interest rate and balance. the class constructor should accept the amount of the savings account's starting balance. The class should also have methods for subtracting the amount of withdrawal, add the amount of deposit, and adding the amount of monthly interest to the balance. The monthly interest rate is the annual interest rate divided by 12. To add the monthly interest to the balance, multiply the monthly interest rate by the balance, and add the result to the balance. ( this is compound interest ) Use DecimalFormat class to format output.

Test the class in a program that calculates the balance of a savings account at the end of a period of time. It should ask the user for the annual interest rate, the starting balance, and the number of months that have passed since the account was established, or prompt after each loop iteration to continue.



Set up Accumulator variables: totalInterest, totalDeposits, totalWithdrawals
Prompt for starting balance and annual Interest Rate
Create a SavingsAccount object.
Loop through each of the following steps for each set of Input Data:
Prompt the user for the amount deposited into the account during the month. Use the class method to add this amount to account balance.
Prompt the user for the amount withdrawn from the account during the month. Use the class method to subtract the amount from the balance.
Use the class method to calculate the monthly interest.
After the last monthly interest calculation, display the ending balance, the total of deposits, the total of withdrawals, and the total interest.




Here's the code I currently have done so far:

Code

import java.text.DecimalFormat;
import java.util.Scanner;
import javax.swing.JOptionPane;


public class SavingsAccount {

/**
* Program by **myname**
*/

double balance=0, annualIntRate=0, depositAmt, withdrawnAmt, monthlyInt, balancePlusInt;
int months=0;
double totalInterest=0, totalDeposits=0, totalWithdrawals=0;
String input, input2, input3;
char repeat = 'Y';


public SavingsAccount(String input, String input2)
{
input = JOptionPane.showInputDialog("What is your account's starting balance?");

balance = Double.parseDouble(input);

input2 = JOptionPane.showInputDialog("What is the annual interest rate?");

annualIntRate = Double.parseDouble(input2);

}



public void deposit (String input3)
{
input3 = JOptionPane.showInputDialog("Enter deposit for Month:");

depositAmt = Double.parseDouble(input3);

balance += depositAmt;

totalDeposits = totalDeposits + depositAmt;
}

public void withdrawal (String input4)
{
input4 = JOptionPane.showInputDialog("Enter withdrawal for Month:");

withdrawnAmt = Double.parseDouble(input4);

balance -= withdrawnAmt;

totalWithdrawals = totalWithdrawals + withdrawnAmt;
}

public void calcMonthlyInt (double monthlyInt)
{
monthlyInt = annualIntRate/12;
}

public void addMonthlyInt (double balancePlusInt)
{
balancePlusInt = monthlyInt * balance;

balance += balancePlusInt;

totalInterest = totalInterest + balancePlusInt;
}


public double getFinalCalculations()
{
return balance;
return totalDeposits;
return totalWithdrawals;
return totalInterest;
}



}






Here's my questions pertaining to this:

1) How exactly do I connect the while loop with a prompt?
2) How do I even create the JOptionPane prompt that would lead to repeating or exiting out?
3) On the solution photos, there's a 'Month' counter that increases with every run-through, and the prompt that triggers the loop is simply "Calculate Month #" and then the yes or no buttons. How do I get the 'Month #' to increase each time? It that's in its own separate thing or what?
4) While currently in Eclipse, the 'return totalDeposits' is marked as unreachable, yet all the other values are fine. What is wrong with it that's not wrong with the others?
Member
Posts: 4,605
Joined: Sep 15 2011
Gold: 9,464.00
Oct 28 2012 03:02pm
1) you put the prompting code inside the while loop, and either condition the while loop on a particular value that gets affected by what the user enters in the prompt, or you can write a method that encloses the prompt with a while(true) block, returning with the input value when it's done collecting the input

2) see above

3) probably

4) your method for getFinalCalculations has 4 return statements. you realize that a return statement exits the enclosing function that it's in, right?
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll