7 Replies - 494 Views - Last Post: 02 December 2018 - 09:53 PM Rate Topic: -----

#1 StudentLoansss   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-November 18

Interest Earned not displaying any values

Posted 02 December 2018 - 05:17 PM

Im not getting a value for interest earned, but interest is still being applied to total balance. any soloutions?


 public class SavingsAccount{
  
  private double interestRate;
  private double balance;
  private double lastInterest;
  
  public SavingsAccount(double accountBalance, double annualInterestRate) {
    
    balance = accountBalance;
    interestRate = annualInterestRate;
    lastInterest = 0.0;
  }
  
  public void withdraw(double withdrawAmount) {
    balance -= withdrawAmount;
  }
  
  public void deposit(double depositAmount) {
    balance += depositAmount;
  }
  
  public void addInterest() {
    double monthlyInterestRate = interestRate / 12;
    double lastInterest = monthlyInterestRate * balance;
    balance = balance + lastInterest;
  }
  
  public double getBalance() {
    return balance;
  }
  
  public double getInterestRate() {
    return interestRate;
  }
  
  public double getLastInterest() {
    return lastInterest;
  }
}



 import java.util.Scanner;
import java.text.DecimalFormat;

public class SavingsAccountDemo{

  public static void main(String args[]) {

  double monthlyDeposit;
  double monthlyWithdrawl;
  double interestEarned = 0.0;
  double totalDeposits = 0;
  double totalWithdrawn = 0;
  
  System.out.print("StudentLoansss 12/2/2018");
  
  System.out.print("\n");
  
  Scanner keyboard = new Scanner(System.in);
  System.out.print("\nEnter the savings account's  starting balance: ");
  double startingBalance = keyboard.nextDouble();

  System.out.print("Enter the savings account's  annual interest rate: ");
  double annualInterestRate = keyboard.nextDouble();
  
  SavingsAccount savingsAccount = new SavingsAccount( startingBalance, annualInterestRate);

  System.out.print("How many months have passed since the account was opened? ");
  double months = keyboard.nextInt();

    for (int i = 1; i <= months; i++) {

        System.out.print("Enter the amount deposited during month " + i + ": ");
        monthlyDeposit = keyboard.nextDouble();
        totalDeposits += monthlyDeposit;

        savingsAccount.deposit(monthlyDeposit);

        System.out.print("Enter the amount withdrawn during month " + i + ": ");
        monthlyWithdrawl = keyboard.nextDouble();
        totalWithdrawn += monthlyWithdrawl;

        savingsAccount.withdraw(monthlyWithdrawl);

        savingsAccount.addInterest();

        interestEarned += savingsAccount.getLastInterest();
    }

  DecimalFormat dollar = new DecimalFormat("#,##0.00");

  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(savingsAccount.getBalance()));
  }
}


This post has been edited by modi123_1: 02 December 2018 - 10:40 PM
Reason for edit:: Name removed.


Is This A Good Question/Topic? 0
  • +

Replies To: Interest Earned not displaying any values

#2 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 870
  • View blog
  • Posts: 6,695
  • Joined: 25-December 13

Re: Interest Earned not displaying any values

Posted 02 December 2018 - 05:48 PM

Can you copy the program's output and paste it here so we can see what you are talking about?
Please add some comments where the output is wrong that show what the output should be.
Was This Post Helpful? 0
  • +
  • -

#3 StudentLoansss   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-November 18

Re: Interest Earned not displaying any values

Posted 02 December 2018 - 06:09 PM

View PostNormR, on 02 December 2018 - 05:48 PM, said:

Can you copy the program's output and paste it here so we can see what you are talking about?
Please add some comments where the output is wrong that show what the output should be.


i guess the main issues is that "interestEarned" is not bringing me the total interest from the 3 months being calculated. I added the output aswell.

 51
	  System.out.println("Total deposited: $" + dollar.format(totalDeposits));
52
	  System.out.println("Total withdrawn: $" + dollar.format(totalWithdrawn));
53
	  System.out.println("Interest earned: $" + dollar.format(interestEarned)); //this should display interest
54
	  System.out.println("Ending balance: $" + dollar.format(savingsAccount.getBalance()));
55
	  }
56
	}

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#4 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 870
  • View blog
  • Posts: 6,695
  • Joined: 25-December 13

Re: Interest Earned not displaying any values

Posted 02 December 2018 - 06:46 PM

What printed output is wrong? Add some comments to the output showing
What should the printed output be?
Was This Post Helpful? 0
  • +
  • -

#5 StudentLoansss   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-November 18

Re: Interest Earned not displaying any values

Posted 02 December 2018 - 06:55 PM

View PostNormR, on 02 December 2018 - 06:46 PM, said:

What printed output is wrong? Add some comments to the output showing
What should the printed output be?


I attached the output file that needs to be matched with mine. as you can see the interest is added to total balance but total interest is not displaying the amount added.

View PostStudentLoansss, on 02 December 2018 - 06:54 PM, said:

View PostNormR, on 02 December 2018 - 06:46 PM, said:

What printed output is wrong? Add some comments to the output showing
What should the printed output be?


I attached the output file that needs to be matched with mine. as you can see the interest is added to total balance but total interest is not displaying the amount added.

forgot to add output

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#6 NormR   User is offline

  • D.I.C Lover
  • member icon

Reputation: 870
  • View blog
  • Posts: 6,695
  • Joined: 25-December 13

Re: Interest Earned not displaying any values

Posted 02 December 2018 - 07:05 PM

Can you copy the program's output and paste it here as text not as an image.
Add some comments to the output saying what the correct output should be.

I can not copy the text from the posted images to include in this response so I will create an example:

Quote

value1: 1233
value2: 333 <<<< **** wrong this should be 222


The variable: lastInterest is defined in two places: at the class level and locally in the addInterest method. The variable at the local level hides the one at the class level.
Remove the definition for lastInterest from the addInterest method so that the variable at the class level is given values.

This post has been edited by NormR: 02 December 2018 - 07:25 PM

Was This Post Helpful? 1
  • +
  • -

#7 StudentLoansss   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-November 18

Re: Interest Earned not displaying any values

Posted 02 December 2018 - 08:01 PM

wrong output. this is the one

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#8 StudentLoansss   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-November 18

Re: Interest Earned not displaying any values

Posted 02 December 2018 - 09:53 PM

Fixed, thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1