简体   繁体   中英

update an existing object in list

Q: I have a Bank class containing multiple loan accounts (LoanAccount class). I've create a LoanAccountService that have the CRUD functionalities. My concerns are about how I implemented the update functionality.

Bank

public class Bank {
    private List<LoanAccount> loanAccounts;
}

Loan account

public class LoanAccount {
    private String id;
    private Integer numberOfInstallments;
    private LoanAccountType type;
    private Date creationDate;
    private BigDecimal loanAmount;
}

Service

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;
    }
}

When the method update is called with a LoanAccount containing an id that already exists in loanAccounts list, I want to update the existing object with the object loanAccount given as parameter.

Above is my implementation, but I feel like there should be better ways to do it.

  1. Use Builder for getter and setter

     public class LoanAccount { private String id; private Integer numberOfInstallments; // add other properties public String getId() { return id; } public LoanAccount setId(String id) { this.id = id; return this; } public Integer getNumberOfInstallments() { return numberOfInstallments; } public LoanAccount setNumberOfInstallments(Integer numberOfInstallments) { this.numberOfInstallments = numberOfInstallments; return this; }
  2. Use this one for update method

    public LoanAccount update(LoanAccount loanAccount) { return bank.getLoanAccounts().stream().filter(la -> la.getId().equals(loanAccount.getId())).findFirst().orElseThrow(IllegalArgumentException::new).setCreationDate(loanAccount.getCreationDate()).setLoanAmount(loanAccount.getLoanAmount()).setNumberOfInstallments(loanAccount.getNumberOfInstallments()).setType(loanAccount.getType()); }

You could use a HashMap where the TKey is the type of your LoanAccount.id. Then call loanAccounts.put(id, object) This will update the object if there is already an Id and add a new object if not.

This is a cheap, dirty way. Another way of doing it would be to make your LoanAccount class implement Comparable and in the compareTo() method make a id based comparation. Do the same thing overriding your equals() and you should be ready to go.

@Override
public boolean equals(object obj) {
    if (obj == null) return false;

    return ((LoanAccount)obj).getId() == this.getId();
}

something like that. (code wrote by memory, can have errors and lacks validations like the data type)

  1. What kind of persistence layer do you use?
  2. why do you need to loop through all of the bank accounts?
  3. Did you fetch all the accounts from the repository and loop over the service layer? If so why?
  4. why not you fetch the corresponding single record from repository and update?
  5. Why not you use to find and update the records instead of using the above points?

    These questions may give you an idea. If you answering it !!! If not let we discuss deeper

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM