Advertisement
xi_arma

Bank Account Class Java

Nov 28th, 2024
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.40 KB | Source Code | 0 0
  1. public class BankAccount {
  2.     private int accountNumber;
  3.     private String accountHolderName;
  4.     private double balance;
  5.  
  6.     // Constructor
  7.     public BankAccount(int accountNumber, String accountHolderName) {
  8.         this.accountNumber = accountNumber;
  9.         this.accountHolderName = accountHolderName;
  10.         this.balance = 0.0;
  11.     }
  12.  
  13.     // Getters and Setters
  14.     public int getAccountNumber() {
  15.         return accountNumber;
  16.     }
  17.  
  18.     public String getAccountHolderName() {
  19.         return accountHolderName;
  20.     }
  21.  
  22.     public double getBalance() {
  23.         return balance;
  24.     }
  25.  
  26.     // Methods
  27.     public void deposit(double amount) {
  28.         if (amount > 0) {
  29.             balance += amount;
  30.             System.out.println("Deposited: $" + amount);
  31.         } else {
  32.             System.out.println("Invalid deposit amount.");
  33.         }
  34.     }
  35.  
  36.     public void withdraw(double amount) {
  37.         if (amount > 0 && amount <= balance) {
  38.             balance -= amount;
  39.             System.out.println("Withdrawn: $" + amount);
  40.         } else {
  41.             System.out.println("Insufficient balance or invalid withdrawal amount.");
  42.         }
  43.     }
  44.  
  45.     public void displayAccountInfo() {
  46.         System.out.println("Account Number: " + accountNumber);
  47.         System.out.println("Account Holder Name: " + accountHolderName);
  48.         System.out.println("Balance: $" + balance);
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement