这是我的最后一次通话
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;
}
}
这是我的界面,其中包含两个方法
public interface TransferService {
void transferFunds(TransferRequest transferRequest)
throws InsufficientFundsException, AccountNotFoundException;
List<Transaction> findTransactions(String accountRef)
throws AccountNotFoundException;
}
在实施课程中,如何才能获得“getLegs()
,”getTransactionRef()
“&amp; getTransactionType()
”数据?
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);
}
如何从测试方法中调用服务impl类....
答案 0 :(得分:1)
我有点困惑。您发布了TransferRequest
类的完整代码,但是您不知道如何实例化它?我假设你不知道builder pattern。您的类使用构建器实例化:
TransferRequest tr = TransferRequest.builder()
.reference(...)
.type(...)
.account(...)
.amount(...)
.build();
请注意,多个接口用于以下目的:您只能从方法reference(...)
调用返回对象上的方法builder()
。然后,您只能调用方法type(...)
,依此类推。这些builder steps强制您按正确的顺序使用构建方法。另请注意,您可以重复调用account(...)
和amount(...)
。