Subscribe For Free Updates!

We'll not spam mate! We promise.

Thursday, 21 December 2017

Saving Account To calculate monthly interest

QN. Create a class Saving Account. Use a static variable annualInterestRate to store the annual interest rate for all account holders. Each object of the class contains a private instance variable savingsBalance indicating the amount the saver currently has on deposit.Provide method calculateMonthlyInterest to calculate the monthly interest by multiplying the savingsBalance by annualInterestRate divided by 12. This interest should be added to savingsBalance. Provide a static method modifyInterestRate that sets the annualInterestRate to a new value. Write a program to test class SavingsAccount capability. Instantiate two savingsAccount objects, saver1 and saver2, with balances of TSh 20,000.00 and TSh 30,000.00,respectively. Set annualInterestRate to 4%, then calculate the monthly interest and print the new balances for both savers. Then set the annualInterestRate to 5%, calculate the next month's interest and print the new balances for both savers.

ANSWER
SavingAccount.java

package savingaccount;

/**
 *
 * @author Eng.Mghase
 * @email mghase.sadick1@gmail.com
 * @contact 
 * @web http://qksoftz.com
 */
public class SavingAccount {

    private static float annuallInterestRate;
    private float savingsBalance;
    private float monthlyInterest;

    public SavingAccount(float savingsBalance) {
        this.savingsBalance = savingsBalance;

    }

    public void calculateMonthlyInterest() {
        this.monthlyInterest = (savingsBalance * annuallInterestRate) / 12;
        System.out.println("The monthly interest is TSh: " + this.monthlyInterest);
    }

    public static void modifyInterestRate(float interestRate) {
        annuallInterestRate = interestRate;
    }

    private void calculateSavings() {
        savingsBalance += this.monthlyInterest;
    }

    public void displaySavings() {
        calculateSavings();
        System.out.println("The total balance is : TSh " + savingsBalance);
    }

}


SavingAccount.java
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package savingaccount;
/**
 *
 * @author Eng.Mghase
 * @email mghase.sadick1@gmail.com
 * @contact 0768454641
 * @web http://qksoftz.com
 */
public class TestClass{

    
    public static void main(String[] args) {
      // SavingAccount.annuallInterestRate=0.25;
       
       SavingAccount saver1=new SavingAccount(20000);
        SavingAccount saver2=new SavingAccount(30000);
       
       SavingAccount.modifyInterestRate(4);
		
       saver1.calculateMonthlyInterest();
		saver2.calculateMonthlyInterest();
		saver1.displaySavings();
		saver2.displaySavings();
		
                SavingAccount.modifyInterestRate(5);
		saver1.calculateMonthlyInterest();
		saver2.calculateMonthlyInterest();
		saver1.displaySavings();
		saver2.displaySavings();
          
          
    }
    
}

Socialize
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

0 comments:

Post a Comment