|
| 1 | +class BankAccount { |
| 2 | + protected String accountHolder; |
| 3 | + protected double balance; |
| 4 | + |
| 5 | + public BankAccount(String accountHolder, double balance) { |
| 6 | + this.accountHolder = accountHolder; |
| 7 | + this.balance = balance; |
| 8 | + } |
| 9 | + |
| 10 | + public void deposit(double amount) { |
| 11 | + if (amount > 0) { |
| 12 | + balance += amount; |
| 13 | + System.out.println(amount + " deposited. New balance: " + balance); |
| 14 | + } else { |
| 15 | + System.out.println("Invalid deposit amount."); |
| 16 | + } |
| 17 | + } |
| 18 | + |
| 19 | + public void withdraw(double amount) { |
| 20 | + if (amount <= balance) { |
| 21 | + balance -= amount; |
| 22 | + System.out.println( amount + " withdrawn. New balance: " + balance); |
| 23 | + } else { |
| 24 | + System.out.println("Insufficient balance."); |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + public void displayBalance() { |
| 29 | + System.out.println(accountHolder + "Balance: " + balance); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +class SavingsAccount extends BankAccount { |
| 34 | + |
| 35 | + public SavingsAccount(String accountHolder, double balance) { |
| 36 | + super(accountHolder, balance); |
| 37 | + } |
| 38 | + |
| 39 | + @Override |
| 40 | + public void withdraw(double amount) { |
| 41 | + if (balance - amount < 100) { |
| 42 | + System.out.println("Withdrawal denied. Balance cannot go below 100 in Savings Account."); |
| 43 | + } else { |
| 44 | + super.withdraw(amount); |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +class CurrentAccount extends BankAccount { |
| 50 | + private final double overdraftLimit = 5000; |
| 51 | + |
| 52 | + public CurrentAccount(String accountHolder, double balance) { |
| 53 | + super(accountHolder, balance); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void withdraw(double amount) { |
| 58 | + if (balance - amount < -overdraftLimit) { |
| 59 | + System.out.println("Withdrawal denied. Overdraft limit of 5000 exceeded in Current Account."); |
| 60 | + } else { |
| 61 | + balance -= amount; |
| 62 | + System.out.println(amount + " withdrawn. New balance: " + balance); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +public class BankDemo { |
| 68 | + public static void main(String[] args) { |
| 69 | + // Creating instances |
| 70 | + SavingsAccount savings = new SavingsAccount("Alice", 1000); |
| 71 | + CurrentAccount current = new CurrentAccount("Bob", 2000); |
| 72 | + |
| 73 | + // Transactions on SavingsAccount |
| 74 | + System.out.println("\n--- Savings Account Transactions ---"); |
| 75 | + savings.displayBalance(); |
| 76 | + savings.deposit(500); |
| 77 | + savings.withdraw(1400); // Should fail |
| 78 | + savings.withdraw(200); // Should succeed |
| 79 | + savings.displayBalance(); |
| 80 | + |
| 81 | + // Transactions on CurrentAccount |
| 82 | + System.out.println("\n--- Current Account Transactions ---"); |
| 83 | + current.displayBalance(); |
| 84 | + current.withdraw(6000); // Should succeed (within overdraft) |
| 85 | + current.withdraw(2000); // Should fail (overdraft exceeded) |
| 86 | + current.deposit(3000); |
| 87 | + current.displayBalance(); |
| 88 | + } |
| 89 | +} |
| 90 | + |
0 commit comments