/**
SavingsAccount class
Chapter 6, Programming Challenge 10
*/
public class SavingsAccount
{
double balance;
double interestRate;
double totalInterest;
double totalDeposit;
double totalWithdraw;
/**
The constructor initializes an object with a
balance and an annual interest rate.
@param bal The account balance.
@param intRate The annual interest rate.
*/
public SavingsAccount(double bal, double intRate)
{
balance = bal;
interestRate = intRate;
totalDeposit = 0.00;
totalWithdraw = 0.00;
totalInterest = 0.00;
}
/**
The withdraw method withdraws an amount from
the account.
@param amount The amount to withdraw.
*/
public void withdraw(double amount)
{
totalWithdraw += amount;
balance -= amount;
}
/**
The deposit method deposits an amount into
the account.
@param amount The amount to deposit.
*/
public void deposit(double amount)
{
totalDeposit += amount;
balance += amount;
}
/**
The addInterest method calculates the monthly
interest and adds it to the account balance.
*/
public void addInterest()
{
// Get the monthly interest rate.
totalInterest += balance * ((interestRate/12));
// Calculate the last amount of interest earned.
balance += balance * ((interestRate/12));
}
/**
The getBalance method returns the account balance.
@return The account balance.
*/
public double getBalance()
{
return balance;
}
/**
The getInterestRate method returns the annual
interest rate.
@return The annual interest rate.
*/
public double getInterestRate()
{
return interestRate;
}
/**
The getLastInterest method returns the last amount
of interest earned.
@return The last amount of interest earned.
*/
public double getLastInterest()
{
return totalInterest;
}
}
And here is the second test program. Like I said the problem I'm getting seems to be with the SavingsAccount object.
import java.util.Scanner;
import java.text.DecimalFormat;
public class SavingsAccountTest
{
public static void main(String args[])
{
double interestRate; // Annual interest rate
double balance; // Starting balance
double deposits; // Amount of deposits for a month
double withdrawn; // Amount withdrawn in a month
double interestEarned = 0.0; // Interest earned
double totalDeposits = 0; // Deposit accumulator
double totalWithdrawn = 0; // Withdrawal accumulator
double months; // Months that have passed
// Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get the starting balance.
System.out.print("Enter the savings account's " +
"starting balance: ");
balance = keyboard.nextDouble();
// Get the annual interest rate.
System.out.print("Enter the savings account's " +
"annual interest rate: ");
interestRate = keyboard.nextDouble();
// Create a SavingsAccount object.
SavingsAccount savings =
new SavingsAccount(balance, interestRate);
// Get the number of months that have passed.
System.out.print("How many months have passed since " +
"the account was opened? ");
months = keyboard.nextInt();
// Get the deposits and withdrawals for each month.
for (int i = 1; i <= months; i++)
{
// Get the deposits.
System.out.print("Enter the amount deposited " +
"during month " + i + ": ");
deposits = keyboard.nextDouble();
totalDeposits += deposits;
// Get the withdrawals.
System.out.print("Enter the amount withdrawn " +
"during month " + i + ": ");
withdrawn = keyboard.nextDouble();
totalWithdrawn += withdrawn;
// Add the deposits to the account.
savings.deposits(deposits);
// Subtract the withdrawals.
savings.withdrawn(withdrawn);
// Add the monthly interest.
savings.addInterest();
// Accumulate the amount of interest earned.
interestEarned += savings.getLastInterest();
}
// Create a DecimalFormat object for formatting output.
DecimalFormat dollar = new DecimalFormat("#,##0.00");
// Display the totals and the balance.
System.out.println("Total deposited: $" +
dollar.format(totalDeposits));
System.out.println("Total withdrawn: $" +
dollar.format(totalWithdrawn));
System.out.println("Interest earned: $" +
dollar.format(interestEarned));
System.out.println("Ending balance: $" +
dollar.format(savings.getBalance()));
}
}

New Topic/Question
Reply


MultiQuote




|