From ec68dc6815c30ff8227e483d5a350ade8fe7cbd1 Mon Sep 17 00:00:00 2001 From: Aadit Agrawal Date: Sat, 14 Sep 2024 09:37:06 +0530 Subject: [PATCH] Update OOP/Java/Lab/Week6/Account/AccountManagement.java --- .../Lab/Week6/Account/AccountManagement.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/OOP/Java/Lab/Week6/Account/AccountManagement.java b/OOP/Java/Lab/Week6/Account/AccountManagement.java index 5056833..e277730 100644 --- a/OOP/Java/Lab/Week6/Account/AccountManagement.java +++ b/OOP/Java/Lab/Week6/Account/AccountManagement.java @@ -12,6 +12,10 @@ class Account { this.accountType = accountType; } + protected double getBalanceInternal() { + return balance; + } + public void deposit(double amount) { balance += amount; } @@ -39,12 +43,12 @@ class SavingsAccount extends Account { } public void computeInterest() { - double interest = balance * interestRate / 100; + double interest = getBalanceInternal() * interestRate / 100; deposit(interest); } public void withdraw(double amount) { - if (amount > getBalance()) { + if (amount > getBalanceInternal()) { System.out.println("Insufficient balance"); } else { super.withdraw(amount); @@ -64,14 +68,14 @@ class CurrentAccount extends Account { } public void checkMinimumBalance() { - if (getBalance() < minimumBalance) { + if (getBalanceInternal() < minimumBalance) { withdraw(serviceTax); System.out.println("Service tax imposed"); } } public void withdraw(double amount) { - if (amount > getBalance()) { + if (amount > getBalanceInternal()) { System.out.println("Insufficient balance"); } else { super.withdraw(amount); @@ -86,15 +90,19 @@ public class AccountManagement { SavingsAccount saurabhAccount = new SavingsAccount("Saurabh", 1234, 10000, 5); CurrentAccount akhilAccount = new CurrentAccount("Akhil", 5678, 5000, 1000, 500); - saurabhAccount.deposit(5000); + System.out.println("Initial balances:"); System.out.println("Saurabh's balance: " + saurabhAccount.getBalance()); + System.out.println("Akhil's balance: " + akhilAccount.getBalance()); + + saurabhAccount.deposit(5000); + System.out.println("Saurabh's balance after deposit: " + saurabhAccount.getBalance()); saurabhAccount.computeInterest(); System.out.println("Saurabh's balance after interest: " + saurabhAccount.getBalance()); saurabhAccount.withdraw(2000); System.out.println("Saurabh's balance after withdrawal: " + saurabhAccount.getBalance()); akhilAccount.deposit(2000); - System.out.println("Akhil's balance: " + akhilAccount.getBalance()); + System.out.println("Akhil's balance after deposit: " + akhilAccount.getBalance()); akhilAccount.withdraw(3000); System.out.println("Akhil's balance after withdrawal: " + akhilAccount.getBalance()); akhilAccount.checkMinimumBalance();