问:我有一个包含多个贷款帐户的 Bank 类(LoanAccount 类)。我创建了一个具有 CRUD 功能的 LoanAccountService 。我担心的是我如何实现更新功能。
银行
public class Bank {
private List<LoanAccount> loanAccounts;
}
贷款账户
public class LoanAccount {
private String id;
private Integer numberOfInstallments;
private LoanAccountType type;
private Date creationDate;
private BigDecimal loanAmount;
}
服务
public class LoanAccountService{
private Bank bank;
public LoanAccountService(Bank bank) {
this.bank = bank;
}
public LoanAccount update(LoanAccount loanAccount) {
Optional<LoanAccount> account = bank.getLoanAccounts()
.stream()
.filter(la -> la.getId().equals(loanAccount.getId()))
.findAny();
if (account.isPresent()) {
account.get().setCreationDate(loanAccount.getCreationDate());
account.get().setLoanAmount(loanAccount.getLoanAmount());
account.get().setNumberOfInstallments(loanAccount.getNumberOfInstallments());
account.get().setType(loanAccount.getType());
} else {
throw new IllegalArgumentException("The object does not exist.");
}
return loanAccount;
}
}
当使用包含 LoanAccounts 列表中已存在的 ID 的 LoanAccount 调用方法更新时,我想使用作为参数给出的对象 LoanAccount 来更新现有对象。
以上是我的实现,但我觉得应该有更好的方法来做到这一点。
烙印99
暮色呼如
慕森卡
相关分类