Project II: Bank Account
Your team will create an Account
class, which models a bank account.
The Account class allows deposits and withdrawals. Instead of warning about a balance that's too low, however, it merely disallows a withdrawal request for more money than the account contains.
The following table contains a sample code execution sequence and the corresponding results:
Account a1 = new Account(100);
Create a new account with initial balance as ¥100.
a1.deposit(50);
Add ¥50 to the account a1.
a1.getBalance();
150
Return the number of money in the account a1, which is 150.
a1.deposit(150);
Add ¥150 to the account a1.
a1.getBalance();
300
Return the number of money in the account a1, which is ¥300.
a1.withdraw(50);
true
Since a1 contains more money than the amount of withdrawal, the withdrawal succeeds.
a1.getBalance();
250
Return the number of money in the a1, which is ¥250.
a1.withdraw(300);
false
Since a1 contains less money than the amount of withdrawal, the withdrawal fails.
a1.getBalance();
250
Return the number of money in the a1, which is still ¥250.
Account a2 = new Account(500);
Create a new account a2with initial balance as ¥500.
a1.getBalance();
250
Return the number of money in the a1, which is still ¥250.
Besides, Account class also has a variable called noAccount
to keep record of how many accounts haven been opened so far. So if there are two accounts created, then noAccount should be 2:
Bonus Point: transferTo()
The account can also transfer money to another account, as long as it contains enough money. You can implement a method called transferTo()
, which return true if the tranfer succeeds, otherwise return false.
The following table contains the sample execution following the previous table:
a1.transferTo(a2, 100)
true
a1 transfer ¥100 to a2, which succeeds since a1 contains enough money.
a1.getBalance();
150
Return the number of money in the a1, which is ¥150.
a2.getBalance();
600
Return the number of money in the a2, which is ¥600.
a2.transferTo(a1, 800)
false
a2 intends to transfer ¥800 to a1, which fails since a2 doesn't contain enough money.
a1.getBalance();
150
Return the number of money in the a1, which is ¥50.
a2.getBalance();
600
Return the number of money in the a2, which is ¥600.
Your task
Write the complete Account class, including the constructor and any required instance variables and methods. Your implementation must meet all specifications and conform to the example.
Your code should also include a main() method to test all methods in Account class. I recommend you put your main() method in another class other than Account.
In your code, you should follow "data encapsulation" standard: Keep All Instance Variables Private. Violate this standard would cause you lose points.
Friendly Note: System.out.println() is for testing the method in Account class, so it should only be in your testing class, more specifically, your main() method.
Last updated