// Account class (Base class)
class Account {
private int accountNumber;
private double balance;
public Account(int accountNumber) {
this.accountNumber = accountNumber;
this.balance = 0.0;
}
public int getAccountNumber() {
return accountNumber;
}
public double getBalance() {
return balance;
}
public void deposit(double amount) {
balance += amount;
System.out.println("Deposited: $" + amount);
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
System.out.println("Withdrawn: $" + amount);
} else {
System.out.println("Insufficient balance");
}
}
}
// Subclasses of Account
class SavingsAccount extends Account {
// Additional features specific to savings account
public SavingsAccount(int accountNumber) {
super(accountNumber);
}
}
class CheckingAccount extends Account {
// Additional features specific to checking account
public CheckingAccount(int accountNumber) {
super(accountNumber);
}
}
// Customer class
class Customer {
private String name;
private Account account;
public Customer(String name, Account account) {
this.name = name;
this.account = account;
}
public void deposit(double amount) {
account.deposit(amount);
}
public void withdraw(double amount) {
account.withdraw(amount);
}
public double checkBalance() {
return account.getBalance();
}
}
// Employee class
class Employee {
private String name;
public Employee(String name) {
this.name = name;
}
public void processTransaction(Customer customer, double amount,
String type) {
if (type.equalsIgnoreCase("Deposit")) {
customer.deposit(amount);
} else if (type.equalsIgnoreCase("Withdraw")) {
customer.withdraw(amount);
} else {
System.out.println("Invalid transaction type");
}
}
}
// Main class for testing
public class BankingApplication {
public static void main(String[] args) {
// Create accounts for customers
SavingsAccount savingsAccount = new SavingsAccount(1001);
CheckingAccount checkingAccount = new CheckingAccount(2001);
// Create customers and link accounts
Customer customer1 = new Customer("Alice", savingsAccount);
Customer customer2 = new Customer("Bob", checkingAccount);
// Create bank employees
Employee employee1 = new Employee("Eve");
// Employee processing transactions for customers
employee1.processTransaction(customer1, 1000, "Deposit");
employee1.processTransaction(customer2, 500, "Withdraw");
// Checking customer balances after transactions
System.out.println("Customer 1 Balance: $" +
customer1.checkBalance());
System.out.println("Customer 2 Balance: $" +
customer2.checkBalance());
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter