Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SavingsAccount {
- private static double annualInterestRate;
- private double savingsBalance;
- public SavingsAccount(double savingsBalance) {
- this.savingsBalance = savingsBalance;
- }
- public void calculateMonthlyInterest() {
- double monthlyInterest = (savingsBalance * annualInterestRate) / 12;
- savingsBalance += monthlyInterest;
- }
- public static void modifyInterestRate(double newAnnualInterestRate) {
- annualInterestRate = newAnnualInterestRate;
- }
- public double getSavingsBalance() {
- return savingsBalance;
- }
- }
- public class SavingsAccountTest {
- public static void main(String[] args) {
- SavingsAccount.modifyInterestRate(0.04); // Set annualInterestRate to 4%
- SavingsAccount saver1 = new SavingsAccount(2000.0);
- SavingsAccount saver2 = new SavingsAccount(3000.0);
- // Calculate and print the new balances for both savers
- saver1.calculateMonthlyInterest();
- saver2.calculateMonthlyInterest();
- System.out.println("Month 1 - Saver 1: " + saver1.getSavingsBalance());
- System.out.println("Month 1 - Saver 2: " + saver2.getSavingsBalance());
- SavingsAccount.modifyInterestRate(0.05); // Set annualInterestRate to 5%
- // Calculate and print the new balances for both savers in the next month
- saver1.calculateMonthlyInterest();
- saver2.calculateMonthlyInterest();
- System.out.println("Month 2 - Saver 1: " + saver1.getSavingsBalance());
- System.out.println("Month 2 - Saver 2: " + saver2.getSavingsBalance());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement