更新列表中的现有对象

问:我有一个包含多个贷款帐户的 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 来更新现有对象。


以上是我的实现,但我觉得应该有更好的方法来做到这一点。


长风秋雁
浏览 93回答 3
3回答

烙印99

使用 Builder 进行 getter 和 setterpublic class LoanAccount {&nbsp; &nbsp; private String id;&nbsp; &nbsp; private Integer numberOfInstallments;// add other propertiespublic String getId() {&nbsp; &nbsp; return id;}public LoanAccount setId(String id) {&nbsp; &nbsp; this.id = id;&nbsp; &nbsp; return this;}public Integer getNumberOfInstallments() {&nbsp; &nbsp; return numberOfInstallments;}public LoanAccount setNumberOfInstallments(Integer numberOfInstallments) {&nbsp; &nbsp; this.numberOfInstallments = numberOfInstallments;&nbsp; &nbsp; return this;}使用这个作为更新方法public LoanAccount update(LoanAccount loanAccount) {&nbsp; &nbsp; return bank.getLoanAccounts()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(la -> la.getId().equals(loanAccount.getId()))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .findFirst().orElseThrow(IllegalArgumentException::new)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setCreationDate(loanAccount.getCreationDate())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setLoanAmount(loanAccount.getLoanAmount())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setNumberOfInstallments(loanAccount.getNumberOfInstallments())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .setType(loanAccount.getType());}

暮色呼如

你使用什么样的持久层?为什么需要遍历所有银行账户?您是否从存储库中获取了所有帐户并在服务层上循环?如果是这样为什么?为什么不从存储库中获取相应的单个记录并更新?为什么不使用查找和更新记录而不是使用以上几点呢?这些问题也许会给你一个想法。如果你回答的话!!!如果没有让我们更深入地讨论

慕森卡

您可以使用 HashMap,其中 TKey 是 LoanAccount.id 的类型。然后调用loanAccounts.put(id, object) This 将更新对象(如果已有 Id),如果没有则添加新对象。这是一种廉价、肮脏的方式。另一种方法是让您的 LoanAccount 类实现Comparable并在compareTo()方法中进行基于 id 的比较。做同样的事情来覆盖你的equals(),你应该准备好了。@Overridepublic boolean equals(object obj) {&nbsp; &nbsp; if (obj == null) return false;&nbsp; &nbsp; return ((LoanAccount)obj).getId() == this.getId();}类似的事情。(由内存编写的代码,可能有错误并且缺乏数据类型等验证)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java