MIT-Curricular/OOP/Java/Lab/Week6/Account/AccountManagement.java

111 lines
3.5 KiB
Java
Raw Normal View History

// Account class
class Account {
private String customerName;
private int accountNumber;
private double balance;
private String accountType;
public Account(String customerName, int accountNumber, double balance, String accountType) {
this.customerName = customerName;
this.accountNumber = accountNumber;
this.balance = balance;
this.accountType = accountType;
}
protected double getBalanceInternal() {
return balance;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
balance -= amount;
}
public double getBalance() {
return balance;
}
public String getAccountType() {
return accountType;
}
}
// SavingsAccount class
class SavingsAccount extends Account {
private double interestRate;
public SavingsAccount(String customerName, int accountNumber, double balance, double interestRate) {
super(customerName, accountNumber, balance, "Savings");
this.interestRate = interestRate;
}
public void computeInterest() {
double interest = getBalanceInternal() * interestRate / 100;
deposit(interest);
}
public void withdraw(double amount) {
if (amount > getBalanceInternal()) {
System.out.println("Insufficient balance");
} else {
super.withdraw(amount);
}
}
}
// CurrentAccount class
class CurrentAccount extends Account {
private double minimumBalance;
private double serviceTax;
public CurrentAccount(String customerName, int accountNumber, double balance, double minimumBalance, double serviceTax) {
super(customerName, accountNumber, balance, "Current");
this.minimumBalance = minimumBalance;
this.serviceTax = serviceTax;
}
public void checkMinimumBalance() {
if (getBalanceInternal() < minimumBalance) {
withdraw(serviceTax);
System.out.println("Service tax imposed");
}
}
public void withdraw(double amount) {
if (amount > getBalanceInternal()) {
System.out.println("Insufficient balance");
} else {
super.withdraw(amount);
checkMinimumBalance();
}
}
}
// Main function
public class AccountManagement {
public static void main(String[] args) {
SavingsAccount saurabhAccount = new SavingsAccount("Saurabh", 1234, 10000, 5);
CurrentAccount akhilAccount = new CurrentAccount("Akhil", 5678, 5000, 1000, 500);
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 after deposit: " + akhilAccount.getBalance());
akhilAccount.withdraw(3000);
System.out.println("Akhil's balance after withdrawal: " + akhilAccount.getBalance());
akhilAccount.checkMinimumBalance();
System.out.println("Akhil's balance after checking minimum balance: " + akhilAccount.getBalance());
}
}