0% found this document useful (0 votes)
15 views2 pages

Java BankAccount Class Example

The document defines a Java class named BankAccount that models a simple bank account with methods to deposit, withdraw, and check the balance. It includes a private variable for the balance, a constructor to initialize it, and methods to manage transactions while ensuring proper validations. The main method demonstrates creating an account, performing transactions, and displaying the current balance.

Uploaded by

msh.tces
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views2 pages

Java BankAccount Class Example

The document defines a Java class named BankAccount that models a simple bank account with methods to deposit, withdraw, and check the balance. It includes a private variable for the balance, a constructor to initialize it, and methods to manage transactions while ensuring proper validations. The main method demonstrates creating an account, performing transactions, and displaying the current balance.

Uploaded by

msh.tces
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

public class BankAccount {

// Declares a public class named BankAccount that represents a simple bank account

private double balance;


// Declares a private instance variable named balance
// This variable stores the current amount of money in the bank account
// It is private to protect it from being accessed or modified directly from outside the
class

public BankAccount(double initialBalance) {


// Declares a constructor for the BankAccount class
// This constructor is called automatically when a new BankAccount object is
created
// It receives an initial balance as a parameter

balance = initialBalance;
// Assigns the value passed into the constructor to the balance variable
// This sets the starting balance for the new bank account
}

public void deposit(double amount) {


// Declares a public method named deposit
// This method allows money to be added to the account

if (amount > 0) balance += amount;


// Checks that the deposit amount is greater than zero
// If valid, the amount is added to the current balance
}

public void withdraw(double amount) {


// Declares a public method named withdraw
// This method allows money to be taken out of the account

if (amount > 0 && amount <= balance)


// Checks that the withdrawal amount is positive
// Also ensures the amount does not exceed the current balance

balance -= amount;
// Subtracts the withdrawal amount from the balance if conditions are met
else
// Executes if the withdrawal amount is invalid or exceeds the balance

[Link]("Insufficient balance.");
// Displays an error message indicating that the withdrawal cannot be completed
}

public double getBalance() {


// Declares a public method that returns the current account balance
// This provides controlled access to the private balance variable

return balance;
// Returns the value of the balance variable to the caller
}

public static void main(String[] args) {


// The main method serves as the entry point of the program

BankAccount account = new BankAccount(1000);


// Creates a new BankAccount object named account
// The constructor is called with an initial balance of 1000

[Link](500);
// Calls the deposit method to add 500 to the account balance

[Link](300);
// Calls the withdraw method to subtract 300 from the account balance and ensures
that the 300 is gone

[Link]("Current Balance: " + [Link]());


// Prints the current balance by calling the getBalance method
}
}

You might also like