I have created a basic CRUD API using Spring Boot , in that I have created a service class for my controller.
The following is my service method of the Controller.
Services
public Customer updateCustomer(Customer newCustomer, Long customerId) throws ResourceNotFoundException {
return customerRepo.findById(customerId)
.map(customer -> {
if (newCustomer.getName() != null)
customer.setName(newCustomer.getName());
if (newCustomer.getGstin() != null)
customer.setGstin(newCustomer.getGstin());
if (newCustomer.getPhoneNumber() != null)
customer.setPhoneNumber(newCustomer.getPhoneNumber());
if (newCustomer.getAddress() != null)
customer.setAddress(newCustomer.getAddress());
if (newCustomer.getOutstandingBalance() != 0.0f)
customer.setOutstandingBalance(newCustomer.getOutstandingBalance());
return customerRepo.save(customer);
}).orElseThrow(() -> new ResourceNotFoundException());
}
My question is: Is it possible to simplify the code which is Using multiple if?
If there is, can anyone suggest a simplification to handle this logic..??