Advertisement
Gaudenz

BankAccount.java

Feb 20th, 2024
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.60 KB | None | 0 0
  1. //Bangko sa Java SIMPLIFIED
  2. class BankAccount {
  3.  
  4.     private String accountNumber;
  5.     private String accountHolderName;
  6.     private double balance;
  7.  
  8.     BankAccount(String accountNumber, String accountHolderName, double balance)
  9.     {
  10.         this.accountNumber = accountNumber;
  11.         this.accountHolderName = accountHolderName;
  12.         this.balance = balance;
  13.     }
  14.    
  15. // Getter methods to access encapsulated data    
  16.     String getAccountNumber()
  17.     {
  18.         return accountNumber;
  19.     }
  20.     String getAccountHolderName()
  21.     {
  22.         return accountHolderName;
  23.     }
  24.     double balance()
  25.     {
  26.         return balance;
  27.     }
  28.    
  29. // Setter methods to modify encapsulated data
  30.     void setAccountNumber(String accountNumber)
  31.     {
  32.         this.accountNumber = accountNumber;
  33.     }
  34.     void setAccountHolderName(String accountHolderName)
  35.     {
  36.         this.accountHolderName = accountHolderName;
  37.     }
  38.     void setBalance(double balance)
  39.     {
  40.         this.balance = balance;
  41.     }
  42.    void deposit(double amount)
  43.    {
  44.      if (amount <= 0)
  45.         {
  46.             System.out.println("Error: Deposit amount must be positive.");
  47.             return; // Deposit not positive then return/exit method
  48.         }
  49.             balance += amount;
  50.             System.out.println("Deposit: " + amount);
  51.     }
  52.    
  53.     void withdraw(double amount)
  54.     {
  55.         if (amount > 0)
  56.         {
  57.             if (balance >= amount) //Balance greater than/equal amount
  58.             {
  59.                 balance -= amount; //same as (balance = balance - amount;)
  60.                 System.out.println("Withdrawal: " + amount);
  61.             } else {
  62.                 System.out.println("Error: Insufficient funds.");
  63.             }
  64.         } else {
  65.             System.out.println("Error: Withdrawal amount must be positive.");
  66.         }
  67.     }
  68.  
  69.     void displayAccountInfo() {
  70.         System.out.println("Account Number:         " + accountNumber);
  71.         System.out.println("Account Holder Name:    " + accountHolderName);
  72.         System.out.printf("Balance:                Php %6.2f\n\n", balance);
  73.     }
  74.     public static void main(String[] args) {
  75.  
  76.         BankAccount account = new BankAccount("696924069", "Padullon Gaudenz", 1000.00);
  77.        
  78.         account.displayAccountInfo();  
  79.        
  80.         System.out.printf("Try to deposit negative values\n");
  81.         account.deposit(-9000);
  82.        
  83.         account.displayAccountInfo();  
  84.  
  85.         System.out.printf("\nTry to withdraw Php9000\n");
  86.         account.withdraw(90);
  87.        
  88.         account.displayAccountInfo();  
  89.  
  90.    
  91.    }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement