Advertisement
vvccs

E2_Savingaccount

Nov 8th, 2023
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. class SavingsAccount {
  2.     private static double annualInterestRate;
  3.     private double savingsBalance;
  4.  
  5.     public SavingsAccount(double savingsBalance) {
  6.         this.savingsBalance = savingsBalance;
  7.     }
  8.  
  9.     public void calculateMonthlyInterest() {
  10.         double monthlyInterest = (savingsBalance * annualInterestRate) / 12;
  11.         savingsBalance += monthlyInterest;
  12.     }
  13.  
  14.     public static void modifyInterestRate(double newAnnualInterestRate) {
  15.         annualInterestRate = newAnnualInterestRate;
  16.     }
  17.  
  18.     public double getSavingsBalance() {
  19.         return savingsBalance;
  20.     }
  21. }
  22.  
  23. public class SavingsAccountTest {
  24.     public static void main(String[] args) {
  25.         SavingsAccount.modifyInterestRate(0.04); // Set annualInterestRate to 4%
  26.  
  27.         SavingsAccount saver1 = new SavingsAccount(2000.0);
  28.         SavingsAccount saver2 = new SavingsAccount(3000.0);
  29.  
  30.         // Calculate and print the new balances for both savers
  31.         saver1.calculateMonthlyInterest();
  32.         saver2.calculateMonthlyInterest();
  33.  
  34.         System.out.println("Month 1 - Saver 1: " + saver1.getSavingsBalance());
  35.         System.out.println("Month 1 - Saver 2: " + saver2.getSavingsBalance());
  36.  
  37.         SavingsAccount.modifyInterestRate(0.05); // Set annualInterestRate to 5%
  38.  
  39.         // Calculate and print the new balances for both savers in the next month
  40.         saver1.calculateMonthlyInterest();
  41.         saver2.calculateMonthlyInterest();
  42.  
  43.         System.out.println("Month 2 - Saver 1: " + saver1.getSavingsBalance());
  44.         System.out.println("Month 2 - Saver 2: " + saver2.getSavingsBalance());
  45.     }
  46. }
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement