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
: aString
representing the name of the account holder.accountNumber
: aString
representing the account number.balance
: adouble
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:
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):
Last updated