Reputation: 49
This is my final call
public final class TransferRequest implements Serializable {
private static final long serialVersionUID = 1L;
/**
* Returns a new builder for creating a transfer request.
*
* @return the first build step
*/
public static ReferenceStep builder() {
return new Builder();
}
public interface ReferenceStep {
/**
* @param transactionRef client defined transaction reference
* @return the next build step
*/
TypeStep reference(String transactionRef);
}
public interface TypeStep {
/**
* @param transactionType the transaction type for grouping transactions or other purposes
* @return the next build step
*/
AccountStep type(String transactionType);
}
public interface AccountStep {
/**
* @param accountRef the client defined account reference
* @return the next build step
*/
AmountStep account(String accountRef);
}
public interface AmountStep {
/**
* @param money the transfer amount for the account
* @return the final build step
*/
BuildStep amount(Money money);
}
public interface BuildStep {
AmountStep account(String accountRef);
TransferRequest build();
}
private static final class Builder implements ReferenceStep, TypeStep, AccountStep, AmountStep, BuildStep {
private final TransferRequest request = new TransferRequest();
private String accountRef;
@Override
public TypeStep reference(String transactionRef) {
request.transactionRef = transactionRef;
return this;
}
@Override
public AccountStep type(String transactionType) {
request.transactionType = transactionType;
return this;
}
@Override
public AmountStep account(String accountRef) {
this.accountRef = accountRef;
return this;
}
@Override
public BuildStep amount(Money money) {
request.legs.add(new TransactionLeg(accountRef, money));
accountRef = null;
return this;
}
@Override
public TransferRequest build() {
if (request.legs.size() < 2) {
throw new IllegalStateException("Expected at least 2 legs");
}
return request;
}
}
private String transactionRef;
private String transactionType;
private final List<TransactionLeg> legs = new ArrayList<TransactionLeg>();
private TransferRequest() {
}
public List<TransactionLeg> getLegs() {
return Collections.unmodifiableList(legs);
}
public String getTransactionRef() {
return transactionRef;
}
public String getTransactionType() {
return transactionType;
}
}
This is my interface which contains two methods
public interface TransferService {
void transferFunds(TransferRequest transferRequest)
throws InsufficientFundsException, AccountNotFoundException;
List<Transaction> findTransactions(String accountRef)
throws AccountNotFoundException;
}
In Implementation class how can i get "getLegs()
,"getTransactionRef()
" & getTransactionType()
" data?
public void transferFunds(TransferRequest transferRequest)
throws InsufficientFundsException, AccountNotFoundException {
// Validating the legs amounts
List<TransactionLeg> legs = transferRequest.getLegs();
TransactionDO transaction = new TransactionDO(transferRequest.getTransactionRef(),transferRequest.getTransactionType(), new Date());
for (TransactionLeg leg : legs) {
AccountDO account = accountRepository.findByAccountRef(leg.getAccountRef());
if (account == null) {
throw new AccountNotFoundException("the account '"
+ leg.getAccountRef() + "' does not exist");
}
if(!account.isActive())
{
throw new AccountNotFoundException("the account '"
+ leg.getAccountRef() + "' is closed");
}
TransactionEntryDO entry = new TransactionEntryDO(transaction,
account.getAccountRef(), leg.getAmount().getAmount(), leg
.getAmount().getCurrency().toString());
Money balanceToBe;
try {
String accountCurrencyCode = account.getCurrency();
balanceToBe = MoneyUtils.add(
MoneyUtils.toMoney(account.getBalance().toString(),
accountCurrencyCode), MoneyUtils.toMoney(leg.getAmount().getAmount().toString(), accountCurrencyCode));
} catch (Exception e) {
throw new IllegalArgumentException(e.getMessage());
}
account.setBalance(balanceToBe.getAmount());
accountRepository.save(account);
transaction.getTransactionEntries().add(entry);
}
transactionRepository.save(transaction);
}
how can call service impl class from test method....
Upvotes: 0
Views: 54
Reputation: 20648
Somewhat I am puzzled. You posted the complete code of the TransferRequest
class, but yet you do not know how to instantiate it? I assume that you do not know the builder pattern. Your class is instantiated with a builder:
TransferRequest tr = TransferRequest.builder()
.reference(...)
.type(...)
.account(...)
.amount(...)
.build();
Note, that the several interfaces serve the following purpose: You can only call the method reference(...)
on the returned object from method builder()
. Then you can only call the method type(...)
, and so on. These builder steps force you to use the building methods in the correct order. Note also, that you can repeat the calls to account(...)
and amount(...)
.
Upvotes: 1