The text below is selected, press Ctrl+C to copy to your clipboard. (⌘+C on Mac) No line numbers will be copied.
Guest
Bank
By Guest on 9th September 2024 09:58:21 AM | Syntax: PYTHON | Views: 40



New Paste New paste | Download Paste Download | Toggle Line Numbers Show/Hide line no. | Copy Paste Copy text to clipboard
  1. // Base class representing a general bank account
  2. public class BankAccount {
  3.     private double balance;
  4.  
  5.     // Constructor to initialize the account with a starting balance
  6.     public BankAccount(double initialBalance) {
  7.         this.balance = initialBalance;
  8.     }
  9.  
  10.     // Method to deposit money into the account
  11.     public void deposit(double amount) {
  12.         if (amount > 0) {
  13.             balance += amount;
  14.             System.out.println("Deposited: $" + amount);
  15.         } else {
  16.             System.out.println("Deposit amount must be positive.");
  17.         }
  18.     }
  19.  
  20.     // Method to withdraw money from the account
  21.     public void withdraw(double amount) {
  22.         if (amount > 0 && amount <= balance) {
  23.             balance -= amount;
  24.             System.out.println("Withdrew: $" + amount);
  25.         } else {
  26.             System.out.println("Insufficient funds or invalid amount.");
  27.         }
  28.     }
  29.  
  30.     // Method to check the current balance
  31.     public double getBalance() {
  32.         return balance;
  33.     }
  34. }
  35.  
  36. // Subclass representing a savings account
  37. public class SavingsAccount extends BankAccount {
  38.  
  39.     // Constructor to initialize the savings account with a starting balance
  40.     public SavingsAccount(double initialBalance) {
  41.         super(initialBalance);
  42.     }
  43.  
  44.     // Overridden withdraw method to prevent withdrawals if balance falls below $100
  45.     @Override
  46.     public void withdraw(double amount) {
  47.         if (amount > 0 && (getBalance() - amount) >= 100) {
  48.             super.withdraw(amount);
  49.         } else {
  50.             System.out.println("Withdrawal denied. Balance would fall below $100.");
  51.         }
  52.     }
  53. }
  54.  
  55. // Main class to test the BankAccount and SavingsAccount classes
  56. public class Main {
  57.     public static void main(String[] args) {
  58.         // Create a SavingsAccount with an initial balance
  59.         SavingsAccount mySavings = new SavingsAccount(500);
  60.  
  61.         // Deposit some money
  62.         mySavings.deposit(200);
  63.  
  64.         // Attempt to withdraw an amount
  65.         mySavings.withdraw(550); // This should be denied as it would drop the balance below $100
  66.  
  67.         // Check the current balance
  68.         System.out.println("Current Balance: $" + mySavings.getBalance());
  69.  
  70.         // Withdraw an allowed amount
  71.         mySavings.withdraw(200); // This should be successful
  72.  
  73.         // Check the final balance
  74.         System.out.println("Final Balance: $" + mySavings.getBalance());
  75.     }
  76. }





bank