Java Interface: Banking system classes - Bank Account, Savings Account, and Current Account
Java Interface: Exercise-4 with Solution
Write a Java programming to create a banking system with three classes - Bank, Account, SavingsAccount, and CurrentAccount. The bank should have a list of accounts and methods for adding them. Accounts should be an interface with methods to deposit, withdraw, calculate interest, and view balances. SavingsAccount and CurrentAccount should implement the Account interface and have their own unique methods.
Sample Solution:
Java Code:
// Account.java
// Interface Account
interface Account {
void deposit(double amount);
void withdraw(double amount);
double getBalance();
}
// SavingsAccount.java
// Class SavingsAccount
class SavingsAccount implements Account {
private double balance;
private double interestRate;
public SavingsAccount(double initialDeposit, double interestRate) {
this.balance = initialDeposit;
this.interestRate = interestRate;
}
@Override
public void deposit(double amount) {
balance += amount;
}
@Override
public void withdraw(double amount) {
balance -= amount;
}
@Override
public double getBalance() {
return balance;
}
// Applying interest rate 1.25% for 1 year:
public void applyInterest() {
balance += balance * interestRate/100;
}
}
// CurrentAccount.java
// Class CurrentAccount
class CurrentAccount implements Account {
private double balance;
private double overdraftLimit;
public CurrentAccount(double initialDeposit, double overdraftLimit) {
this.balance = initialDeposit;
this.overdraftLimit = overdraftLimit;
}
@Override
public void deposit(double amount) {
balance += amount;
}
@Override
public void withdraw(double amount) {
if (balance + overdraftLimit >= amount) {
balance -= amount;
}
}
@Override
public double getBalance() {
return balance;
}
public void setOverdraftLimit(double overdraftLimit) {
this.overdraftLimit = overdraftLimit;
}
}
// Bank.java
// Class Bank
import java.util.ArrayList;
import java.util.List;
class Bank {
private List accounts;
public Bank() {
accounts = new ArrayList<>();
}
public void addAccount(Account account) {
accounts.add(account);
}
public void removeAccount(Account account) {
accounts.remove(account);
}
public void deposit(Account account, double amount) {
account.deposit(amount);
}
public void withdraw(Account account, double amount) {
account.withdraw(amount);
}
public void printAccountBalances() {
for (Account account : accounts) {
System.out.println("Account balance: " + account.getBalance());
}
}
}
// Main.java
// Class Main
public class Main {
public static void main(String[] args) {
Bank bank = new Bank();
SavingsAccount savingsAccount = new SavingsAccount(1000.0, 1.25);
System.out.println("Savings Account:\nInitial Deposit: $1000.00\nInterest rate: 1.25%");
CurrentAccount currentAccount = new CurrentAccount(5000.0, 1000.0);
System.out.println("\nCurrent Account:\nInitial Deposit: $5000.00\nOverdraftLimit: $1000.00");
bank.addAccount(savingsAccount);
bank.addAccount(currentAccount);
System.out.println("\nNow deposit $100 to Savings Account.");
bank.deposit(savingsAccount, 100.0);
System.out.println("Now deposit $500 to Current Account.");
bank.deposit(currentAccount, 500.0);
System.out.println("Withdraw $150 from Savings Account.\n");
bank.withdraw(savingsAccount, 150.0);
System.out.println("\nSavings A/c and Current A/c.:");
bank.printAccountBalances();
savingsAccount.applyInterest();
System.out.println("\nAfter applying interest on Savings A/c for 1 year:");
System.out.println("Savings A/c and Current A/c.:");
bank.printAccountBalances();
}
}
Sample Output:
Savings Account: Initial Deposit: $1000.00 Interest rate: 1.25% Current Account: Initial Deposit: $5000.00 OverdraftLimit: $1000.00 Now deposit $100 to Savings Account. Now deposit $500 to Current Account. Withdraw $150 from Savings Account. Savings A/c and Current A/c.: Account balance: 950.0 Account balance: 5500.0 After applying interest on Savings A/c for 1 year: Savings A/c and Current A/c.: Account balance: 961.875 Account balance: 5500.0
Flowchart of Interface Account:

Flowchart of Class SavingsAccount:

Flowchart of Class CurrentAccount:

Flowchart of Class Bank:

Flowchart of Class Main:

Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Implement the Flyable interface.
What is the difficulty level of this exercise?
Java: Tips of the Day
How do I remove repeated elements from ArrayList?
If you don't want duplicates in a Collection, you should consider why you're using a Collection that allows duplicates. The easiest way to remove repeated elements is to add the contents to a Set (which will not allow duplicates) and then add the Set back to the ArrayList:
Set<String> set = new HashSet<>(yourList); yourList.clear(); yourList.addAll(set);
Of course, this destroys the ordering of the elements in the ArrayList.
Ref: https://bit.ly/3bYIjNC
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook