tenho a aplicação que é uma possível solução para o desafio . Segue o Código:
public interface TypeCustomerLoan {
TypeLoan lending(Integer age, BigDecimal income, String uf);
}
A classe Enum:
@Getter
@Component
public enum TypeLoan {
PERSONAL(4.0),
GUARANTEED(3.0),
CONSIGNMENT(2.0);
private final double interestRate;
TypeLoan(double interestRate) {
this.interestRate = interestRate;
}
}
As classes que implementa TypeCustomerLoan:
@Component
public class LoanConsignment implements TypeCustomerLoan{
@Override
public TypeLoan lending(Integer age, BigDecimal income, String uf) {
if(income.compareTo(new BigDecimal(5000)) >= 0) {
return TypeLoan.CONSIGNMENT;
}
return null;
}
}
@Component
public class LoanPersonal implements TypeCustomerLoan{
@Override
public TypeLoan lending(Integer age, BigDecimal income, String uf) {
if(income.compareTo(new BigDecimal(3000)) <= 0) {
return TypeLoan.PERSONAL;
}else if((income.compareTo(new BigDecimal(3000)) >= 0 && income.compareTo(new BigDecimal(5000)) <= 0)
&& age < 30 && uf.compareToIgnoreCase("SP") == 0){
return TypeLoan.PERSONAL;
}
return null;
}
}
@Component
public class LoanGuaranteed implements TypeCustomerLoan{
@Override
public TypeLoan lending(Integer age, BigDecimal income, String uf) {
if(income.compareTo(new BigDecimal(3000)) <= 0) {
return TypeLoan.GUARANTEED;
}else if((income.compareTo(new BigDecimal(3000)) >= 0 && income.compareTo(new BigDecimal(5000)) <= 0)
&& age < 30 && uf.compareToIgnoreCase("SP") == 0){
return TypeLoan.GUARANTEED;
}
return null;
}
}
E o Service:
@Service
public class CustomerService {
@Autowired
private List<TypeCustomerLoan> listTypeCustomerLoan;
public List<TypeLoan> customerLoan(CustomerDTO dto){
return listTypeCustomerLoan.stream()
.map(loanType -> loanType.lending(dto.age(), dto.income(), dto.uf()))
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
}
Entretanto eu recebo NullPointerException, o por que?