Java Reference
In-Depth Information
Figure 7.1 In this simple bank account example, we use a mock object to test an account
transfer method.
Listing 7.1 presents a simple
Account
object with two properties: an account
ID
and
a balance.
Listing 7.1
Account.java
[...]
public class
Account {
private
String accountId;
private long
balance;
public
Account(String accountId,
long
initialBalance) {
this
.accountId = accountId;
this
.balance = initialBalance;
}
public void
debit(
long
amount) {
this
.balance -= amount;
}
public void
credit(
long
amount) {
this
.balance += amount;
}
public long
getBalance() {
return this
.balance;
}
}
The
AccountManager
interface that follows manages the lifecycle and persistence of
Account
objects. (We're limited to finding accounts by
ID
and updating accounts.)
[...]
public interface AccountManager {
Account findAccountForUser(String userId);
void updateAccount(Account account);
}
Listing 7.2 shows the
transfer
method for transferring money between two accounts.
It uses the
AccountManager
interface we previously defined to find the debit and
credit accounts by
ID
and to update them.