w3resource

Creating a Java Account Class with Encapsulation and Transaction Methods

Java Encapsulatiion: Exercise-12 with Solution

Write a Java program to create a class called Account with private instance variables accountNumber, accountHolder, and balance. Provide public getter and setter methods to access and modify these variables. Add a method called deposit() that takes an amount and increases the balance by that amount, and a method called withdraw() that takes an amount and decreases the balance by that amount.

Sample Solution:

Java Code:

Account.java

// Define the Account class
public class Account {
    // Private instance variables
    private String accountNumber;
    private String accountHolder;
    private double balance;

    // Public getter for the accountNumber variable
    public String getAccountNumber() {
        return accountNumber;
    }

    // Public setter for the accountNumber variable
    public void setAccountNumber(String accountNumber) {
        this.accountNumber = accountNumber;
    }

    // Public getter for the accountHolder variable
    public String getAccountHolder() {
        return accountHolder;
    }

    // Public setter for the accountHolder variable
    public void setAccountHolder(String accountHolder) {
        this.accountHolder = accountHolder;
    }

    // Public getter for the balance variable
    public double getBalance() {
        return balance;
    }

    // Public setter for the balance variable
    public void setBalance(double balance) {
        this.balance = balance;
    }

    // Method to deposit an amount and increase the balance
    public void deposit(double amount) {
        if (amount > 0) {
            this.balance += amount;
        }
    }

    // Method to withdraw an amount and decrease the balance
    public void withdraw(double amount) {
        if (amount > 0 && this.balance >= amount) {
            this.balance -= amount;
        }
    }  
}

Main.java

public class Main {
    // Main method to test the Account class
    public static void main(String[] args) {
        // Create a new Account object
        Account account = new Account();

        // Set the account number, account holder, and initial balance
        account.setAccountNumber("123456789");
        account.setAccountHolder("Rodolfo Desiree");
        account.setBalance(1000.0);

        // Deposit an amount to the account
        account.deposit(500.0);

        // Withdraw an amount from the account
        account.withdraw(200.0);

        // Print the details of the account
        System.out.println("Account Number: " + account.getAccountNumber());
        System.out.println("Account Holder: " + account.getAccountHolder());
        System.out.println("Balance: $" + account.getBalance());
    }
 }

Output:

Account Number: 123456789
Account Holder: Rodolfo Desiree
Balance: $1300.0

Explanation:

  • Private Instance Variables: The accountNumber, accountHolder, and balance variables are declared as private to ensure encapsulation.
  • Public Getters and Setters: These methods provide controlled access to the private variables.
    • getAccountNumber(): Returns the account number.
    • setAccountNumber(String accountNumber): Sets the account number.
    • getAccountHolder(): Returns the account holder.
    • setAccountHolder(String accountHolder): Sets the account holder.
    • getBalance(): Returns the balance.
    • setBalance(double balance): Sets the balance.
  • deposit Method: Takes an amount and increases the balance by that amount if it's positive.
  • withdraw Method: Takes an amount and decreases the balance by that amount if it's positive and less than or equal to the current balance.
  • Main Method: Tests the functionality of the Account class by creating an instance, setting its properties, performing transactions, and printing the details.

Note on Encapsulation

Encapsulation works in the above exercise by:

  • Hiding Data: The private instance variables accountNumber, accountHolder, and balance are not accessible directly from outside the class.
  • Controlled Access: The public getter and setter methods provide controlled access to the private variables, allowing for validation and modification when necessary.
  • Data Integrity: Encapsulation helps maintain data integrity by ensuring that the internal state of the object can only be changed through well-defined methods.

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Creating a Java House Class with Encapsulation and Price Calculation.
Next: Creating a Java Movie Class with Encapsulation and Details Method

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/java-exercises/encapsulation/java-encapsulation-exercise-12.php