I have to write a program using accessors and mutators and a constructor that stores a savings account's annual interest rate and balance. The class constructor needs to accept the amount of the savings account's starting balance. The class needs to have methods for subtracting withdrawals, adding deposits, and adding the amount of monthly interest rate to the balance. The program should ask the user for the amound deposited into the account during the month, ask the user for teh amount withdrawn from the account during the month, and use the class method to calculate the monthly interest. After the last iteration, the program needs to display teh ending balance, the total amount of deposits, the total amount of withdrawals, and the total interest earned. The code below is what I have so far, and I could use some input as to where to go from here:

public class SavingsAccount // Begin public class SavingsAccount
{   // Instance Variables
    private double balance;     // Account Balance
    private double withdrawals; // Account Withdrawals
    private double deposits;    // Account Deposits
    private double rate;        // Interest Rate

    // Constructor Method Here
    public SavingsAccount(double balance, double withdrawals, double deposits, double rate)
    {   // Begin public SavingsAccount(double balance, double withdrawals, double deposits, double rate)
        balance = 0;
        withdrawals = 0;
        deposits = 0;
        rate = 0;
    }   // End public SavingsAccount(double balance, double withdrawals, double deposits, double rate)

    // Accessor- Gets the beginning balance
    public double getBalance()
    {
        return balance;
    }   // End public double getBalance()

    // Accessor- Gets monthly deposits
    public double getDeposits()
    {
        return deposits;
    }   // End public double getDeposits()

    // Accesor- Gets monthly withdrawals
    public double getWithdrawals()
    {
        return withdrawals;
    }   // End public double getWithdrawals()

    // Accessor- Gets interest rate
    public double getRate()
    {
        return rate;
    }   // End public double getRate()

    // Calculate balance with deposits
    public void setBalance(double deposits)
    {
        balance += balance;
    }   // End public void setBalance(double deposits)

    // Calculate balance with withdrawals
    public void setBalance(double withdrawals)
    {
        balance -= balance;
    }   // End public void setBalance(double withdrawals)

    // Calculate monthly interest rate
    public void setBalance(double rate)
    {
        balance += rate;
    }   // End public void setBalance(double rate)

}   // End public class SavingsAccount

The class you created is a start, though you missed the requirement for the constructor. Your requirement is to accept one parameter, you are passing all. Also, the constructor should set your private attributes. You did that except, since you are passing the balanace, you should set the balanace based on what was passed in.

From that point, you need to create a program to make use of the SavingsAccount. You should create a new class, which is your application. That class will have a main method which instantiates your SavingsAccount class and then makes use of a BufferedReader with System.in to use the readline() method and System.out.println() to communicate with the end user.

Your best bet at this point would be to read up on creating a main method and using those two method calls on the System.in and System.out objects.

Ok, in my second file, this is what I have to this point:

public class SavingsAccountTest
{  // Begin public class SavingsAccountTest

    int months;                     // Number of months account has been open
    double balance;                 // Account balance
    double deposit;                 // Deposit amount
    double withdrawal;              // Withdrawal amount
    double yearlyRate = 0.00;       // Yearly interest rate double monthlyRate = 0.00;
    double monthlyRate = 0.00;      // Monthly interest rate

   public static void main(String[] args)
   {
      String input;    // To hold user input

      // Create a DecimalFormat object for displaying dollars.
      DecimalFormat dollar = new DecimalFormat("#,###.00");

      // Get the starting balance.
      input = JOptionPane.showInputDialog("What is your " +
                            "account's starting balance?");

      // Create a BankAccount object.
      SavingsAccount account = new SavingsAccount(input);

      // Get the amount of pay.
      input = JOptionPane.showInputDialog("How much were " +
                                   "you paid this month? ");

      // Calculate monthly interest rate
       monthlyRate = yearlyRate / 12;


      // Insert loop to repeat for 12 months
      for (int months = 1; months <= 12; months++)
      {
          SavingsAccount.getBalance();
          System.out.println(SavingsAccount.toString());
      }

      // Deposit the user's pay into the account.
      account.deposit(input);

      // Display the new balance.
      JOptionPane.showMessageDialog(null,
                        "Your pay has been deposited.\n" +
                        "Your current balance is $ " +
                        dollar.format(account.getBalance()));

      // Withdraw some cash from the account.
      input = JOptionPane.showInputDialog("How much would " +
                                   "you like to withdraw? ");
      account.withdraw(input);

      // Display the new balance
      JOptionPane.showMessageDialog(null,
                     "Now your balance is $" +
                     dollar.format(account.getBalance()));

      System.exit(0);
   }
}   // End public SavingsAccountTest

When I am trying to compile the program, it keeps giving me 4 errors, all stating pretty much the same thing:
non-static variable monthlyRate cannot be referenced from a static context monthlyRate = yearlyRate / 12;

And I'm stuck. I'm trying to model this off a program that a different instructor handed out to his students, and it's not making sense to me.

In your Test program, you have defined some variables to hold your data. A class with main is no different than any other class, so those variables you defined are attributes of the SaveingsAccountTest class. But you neve rinstantiated the class. You are just using main. Main is a static method so it can only reference static attributes.

You have 2 options, you can either instantiate the class in main and call methods to set variables or you need to define the variables as static so that main can reference them.

If this is the final application, the correct way would be to build out the class with getters/setters and assign values to the variables. But if this is just a quick class to test your SavingsAccount class, then making the variables static is ok.

Do you have downloadable files on this problem so that I can study and make a solution for this.

Here is where I am now and I have it so there are no more compiling errors. The problem I am having now though is when I enter in the number of months that the account was open, it just keeps looping and does not display my total. What am I missing here?

import javax.swing.JOptionPane; // For the JOptionPane class
import java.text.DecimalFormat; // For the DecimalFormat class


public class SavingsAccountTest
{  // Begin public class SavingsAccountTest

    private int months = 0;                         // Number of months account has been open
    private double balance = 0.00;                  // Account balance
    private double deposit = 0.00;                  // Deposit amount
    private double withdrawal = 0.00;               // Withdrawal amount



   public static void main(String[] args)
   {

      double yearlyRate;                // Yearly interest rate double monthlyRate = 0.00;
      double monthlyRate;               // Monthly interest rate
      double totalWithdrawal = 0;       // Total amount of withdrawal
      double totalDeposit = 0;          // Total amount of deposit
      double totalInterest = 0;         // Total amount of Interest
      String input = "";                // To hold user input


      // Create a DecimalFormat object for displaying dollars.
                DecimalFormat dollar = new DecimalFormat("#,###.00");
                 SavingsAccount account = new SavingsAccount();

      // Get the starting balance
                    input = JOptionPane.showInputDialog("What is your " +
                                          "account's starting balance?");

                          account.setBalance(input);

      // Insert loop to repeat for 12 months
       for (int months = 1; months <= 12; months++)
      {


                // Get the number of months the account was open
                input = JOptionPane.showInputDialog("How many " +
                                    "months was the account open?");


                // Get the amount of pay.
                input = JOptionPane.showInputDialog("How much did " +
                                             "you deposit this month? ");
                      account.deposit(input);

                // Add deposit to total

                totalDeposit += Double.parseDouble(input);

                // Withdraw some cash from the account
                    input = JOptionPane.showInputDialog("How much did " +
                                                 "you withdraw? ");
                        account.withdraw(input);

                // Add withdrawals
                totalWithdrawal += Double.parseDouble(input);

                // Get monthly interest rate
                        input = JOptionPane.showInputDialog("Enter yearly " +
                                                "interest rate. ");

                        account.monthlyRate(input);

                        totalInterest += account.monthlyInterest();

                // Get account balance
                account.getBalance();
         }



      // Display the total amount of deposits
      JOptionPane.showMessageDialog(null,
                        "Your total deposits are $ " +
                        dollar.format(totalDeposit));

      // Display the amount of withdrawals
      JOptionPane.showMessageDialog(null,
                        "Your total withdrawals are $ " +
                        dollar.format(totalWithdrawal));

      // Display the total interest
      JOptionPane.showMessageDialog(null,
                        "Your total interest earned is $ " +
                        dollar.format(totalInterest));

      // Display the total balance
      JOptionPane.showMessageDialog(null,
                        "Your account balance is $ " +
                        dollar.format(account.getBalance()));

      System.exit(0);
   }
}   // End public SavingsAccountTest

Are you saying that you keep getting asked without seeing a total? here is what your code is doing:
Asks for starting balance.
Then it enters a loop, so the following is done 12 times:
Asks how many months the account was open (it doesn't do anything with this information)
Asks for deposits and adds them to the account
Asks for withdrawals and adds them to account
Asks for interest and adds to the account
Calls getBalance, but doesn't do anything with that information
End loop (does the above 12 times)

When done display total deposit, withdrawals, interest and account balance.

You did not include your final SavingsAccount code so I can't tellif it works correctly.

What do you see happening when you run this?

Whenever I try to run it, it just keeps asking me to enter the starting balance, the yearly interest rate, the deposits, and the withrawals. Even if I enter it to run for 3 months, it just keeps going and does not give a running total at the end of the total months that the account was open.

Not sure what you mean by enter it to run for 3 months. The code you posted will loop 12 times. It has nothing to do with the number of months you enter in the JOPtionPane prompt. So are you saying that you modified your code to only loop 3 times?

At this point, probably best to post all of your code (both classes).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.