Account Class

Your task is to design a class named Account that represents a basic bank account. The Account class will hold information about the account holder's name, account number, and balance. Additionally, you will implement methods to manipulate these details safely and effectively.

Requirement

Account class should contain:

Instance Variables:

  • accountHolder: a String representing the name of the account holder.

  • accountNumber: a String representing the account number.

  • balance: a double representing the current balance of the account.

Constructor:

The constructor should contain THREE parameters to initialize all instance variables.

Accessor Methods (Getters):

  • getAccountHolder(): returns the account holder's name.

  • getAccountNumber(): returns the account number.

  • getBalance(): returns the current balance.

Mutator Methods (Setters):

  • setAccountHolder(String name): sets the account holder's name.

  • setAccountNumber(String number): sets the account number.

Additional Methods:

  • boolean deposit(double amount): adds the specified amount to the balance. The method should return a boolean value, indicating deposit success or failure. If amount is positive, return true; otherwise return false.

  • boolean withdraw(double amount): subtracts the specified amount from the balance. The method should return true is sufficient funds are available. If not, the balance remains unchanged, and the method should return false.

The following table contains a code execution sequence and the corresponding results:

Statement of Expression
Return value
Explanation

Account a1 = new Account("John Doe", "12345678", 100);

N/A

Create a new account a1 with the name "John Doe", account number 12345678, and an initial balance of ¥100.

a1.getBalance();

100.0

Returns the initial balance, which should be ¥100.

a1.deposit(50);

true

Add ¥50 to account a1. The new balance should be ¥150.

a1.getBalance();

150.0

Returns the updated balance after deposit, which is ¥150.

a1.withdraw(20);

true

Subtract ¥20 from account a1. The new balance should be ¥130.

a1.getBalance();

130.0

Returns the updated balance after withdrawal, which is ¥130.

a1.getAccountHolder();

"John Doe"

Returns the name of the account holder, which is "John Doe".

a1.getAccountNumber();

"12345678"

Returns the account number, which is 12345678.

a1.setAccountHolder("Jane Smith");

N/A

Sets the account holder's name to "Jane Smith".

a1.getAccountHolder();

"Jane Smith"

Returns the new name of the account holder, which is "Jane Smith".

a1.withdraw(150);

false

Attempt to withdraw ¥150, which exceeds the balance.

a1.getBalance();

130.0

Returns the balance remaining after failed withdrawal, ¥130.

a1.deposit(-10);

false

Returns false, since amount is a negative number.

a1.getBalance();

130.0

Return the balance which remains 130.0.

Your task

Your task is to implement the Account class in Java and then test it in the main method using the 👆provided execution table. 👆

URL: https://www.onlinegdb.com/classroom/Fd0aduv55

If the expression has return value, PRINT IT. That's how your pass the test.

You need to pass the test to submit.

Example Solution(credit to Sherry Liao):

public class Main{

    public static void main(String[] args){
        Account a1 = new Account("John Doe", "12345678", 100);
        System.out.println(a1.getBalance());
        System.out.println(a1.deposit(50));
        System.out.println(a1.getBalance());
        System.out.println(a1.withdraw(20));
        System.out.println(a1.getBalance());
        System.out.println(a1.getAccountHolder());
        System.out.println(a1.getAccountNumber());
        a1.setAccountHolder("Jane Smith");
        System.out.println(a1.getAccountHolder());
        System.out.println(a1.withdraw(150));
        System.out.println(a1.getBalance());
        System.out.println(a1.deposit(-10));
        System.out.println(a1.getBalance());
        
    }
}
class Account{
    private String accountHolder;
    private String accountNumber;
    private double balance;
    public Account (String holder, String num,double ba){
        accountHolder=holder;
        accountNumber=num;
        balance=ba;
    }
    public String getAccountHolder(){
        return accountHolder;
    }
    public void setAccountHolder(String name){
        accountHolder =name;
    }
    
    public String getAccountNumber(){
        return accountNumber;
    }
    public void setAccountNumber(String number){
        accountNumber =number;
    }
    
    public double getBalance(){
        return balance;
    }
    public  boolean deposit(double amount){
       if (amount >0){
           balance+=amount;
           return true;
       }
       return false;
   }
    public boolean withdraw(double amount){
        if (amount<=balance){
            balance-=amount;
            return true;
        }
        return false;
    }
    
}

Last updated