Banking System Example
This program models a simple banking system.
Problem Statement
Write a Java program to model Account and Transaction classes with deposit and withdraw operations.
Source Code
| 1 | class Account { |
| 2 | String owner; double balance; |
| 3 | Account(String o,double b){ owner=o; balance=b; } |
| 4 | void deposit(double amt){ balance+=amt; } |
| 5 | void withdraw(double amt){ balance-=amt; } |
| 6 | void show(){ System.out.println(owner+"'s balance: "+balance); } |
| 7 | } |
| 8 | class Transaction { |
| 9 | void perform(Account a,String type,double amt){ |
| 10 | if(type.equals("deposit")) a.deposit(amt); |
| 11 | else if(type.equals("withdraw")) a.withdraw(amt); |
| 12 | } |
| 13 | } |
| 14 | public class BankingSystem { |
| 15 | public static void main(String[] args) { |
| 16 | System.out.println("Eduinq Banking System"); |
| 17 | Account a=new Account("Arun",1000); |
| 18 | Transaction t=new Transaction(); |
| 19 | t.perform(a,"deposit",500); |
| 20 | t.perform(a,"withdraw",200); |
| 21 | a.show(); |
| 22 | } |
| 23 | } |
Program Output
Eduinq Banking System Arun's balance: 1300.0
Explanation
Account models owner and balance. - Transaction performs deposit/withdraw operations. - Separation of responsibilities improves cohesion. - This demonstrates how OOP models banking systems with encapsulation and interaction. Conclusion: Shows how to model banking system using OOP.