Using JUnit: Bank Account Java Class, Study notes of Engineering

We've created a BankAccount class that (as the name suggests) represents a bank account. For our program, the BankAccount class must meet several ...

Typology: Study notes

2021/2022

Uploaded on 08/01/2022

fioh_ji
fioh_ji 🇰🇼

4.5

(70)

814 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Using JUnit Lesson: Bank Account Java Class 1
Using JUnit: Bank Account Java Class
We've created a BankAccount class that (as the name suggests) represents a bank account. For
our program, the BankAccount class must meet several requirements:
Each account has an account number that is set when the account is created and cannot later
be changed. The number can be retrieved with the getAccountNum() method.
Each account also has a balance. For this practice, all balances are whole dollar amounts
(integers). The accounts balance starts at $0, and can be retrieved by the getBalance()
method.
Money can be added to the account by the deposit() method and removed by the
withdraw() method. Both methods return true if the transaction is successful and false if
not. Deposits are always successful as long as the deposit amount is greater than $0, but
withdrawals only succeed if enough money is present in the account to cover the withdrawal.
The account must remember the dollar amount of the last successful transaction, and return it
from the getLastTransAmount() method. (Deposits are returned as positive amounts, and
withdrawals as negative amounts.)
Below is the source code of the BankAccount class:
public class BankAccount
{
public BankAccount(int acctNum)
{
this.acctNum = acctNum;
}
private int getAccountNum()
{
return acctNum;
}
private boolean deposit(int amount)
{
if (amount <= 0)
return false;
balance += amount;
lastTransaction = amount;
return true;
}
private boolean withdraw(int amount)
{
if (amount > 0 && balance >= amount)
{
balance -= amount;
lastTransaction = -amount;
return true;
}
(continued on next page)
pf2

Partial preview of the text

Download Using JUnit: Bank Account Java Class and more Study notes Engineering in PDF only on Docsity!

Using JUnit Lesson: Bank Account Java Class 1

Using JUnit: Bank Account Java Class

We've created a BankAccount class that (as the name suggests) represents a bank account. For

our program, the BankAccount class must meet several requirements:

 Each account has an account number that is set when the account is created and cannot later

be changed. The number can be retrieved with the getAccountNum() method.

 Each account also has a balance. For this practice, all balances are whole dollar amounts

(integers). The account’s balance starts at $0, and can be retrieved by the getBalance()

method.

 Money can be added to the account by the deposit() method and removed by the

withdraw() method. Both methods return true if the transaction is successful and false if

not. Deposits are always successful as long as the deposit amount is greater than $0, but

withdrawals only succeed if enough money is present in the account to cover the withdrawal.

 The account must remember the dollar amount of the last successful transaction, and return it

from the getLastTransAmount() method. (Deposits are returned as positive amounts, and

withdrawals as negative amounts.)

Below is the source code of the BankAccount class:

public class BankAccount { public BankAccount(int acctNum) { this.acctNum = acctNum; } private int getAccountNum() { return acctNum; } private boolean deposit(int amount) { if (amount <= 0) return false; balance += amount; lastTransaction = amount; return true; } private boolean withdraw(int amount) { if (amount > 0 && balance >= amount) { balance -= amount; lastTransaction = -amount; return true; }

(continued on next page)

Using JUnit Lesson: Bank Account Java Class 2 else return false; } private int getBalance() { return balance; } private int getLastTransAmount() { return lastTransaction; } @Override public String toString() { return String.format("Account %d (balance $%d)", acctNum, balance); } private int acctNum; private int balance; private int lastTransaction; }