Is this the correct way to use and initialize the Optional variables .. I get an error "variable might not be initialized " on all these variables when I call calculateAnnualCalculationTotal ..I want to use these variables in the annualCalculationSummaryDto if the insurance.product isn't abc or def otherwise those three fields that am trying to set in the annualCalculationSummaryDto should remain null ..
Optional<CalculatedPaymentsDto> calculatedPayments ;
Optional<PaidPaymentsDTO> paidPayments ;
Optional<CreatePaymentResponseDto> payment ;
if (!insurance.getProduct().equalsIgnoreCase("abc") && !insurance.getProduct().equalsIgnoreCase("def")) {
int totalAnnualCalculationSum = calculateAnnualCalculationTotal(insurance, requestDto.getAnnualCalculationYear(), calculatedPayments, paidPayments);
payment = Optional.ofNullable(createPayment(getCreatePaymentRequestDto(insurance, calculatedPayments.get(), totalAnnualCalculationSum, requestDto.getAnnualCalculationYear())));
}
AnnualCalculationSummaryDto annualCalculationSummaryDto = new AnnualCalculationSummaryDto();
if(calculatedPayments.isPresent() && paidPayments.isPresent() && payment.isPresent()){
annualCalculationSummaryDto.setCalculatedPaymentsDto(calculatedPayments.get());
annualCalculationSummaryDto.setPaidPaymentsDTO(paidPayments.get());
annualCalculationSummaryDto.setPayments(payment.get());
}
annualCalculationSummaryDto.setParam1(inr.getProduct());
annualCalculationSummaryDto.setParam2(requestDto.getTotal());
annualCalculationSummaryRepository.save(annualCalculationSummaryDto);
............. ............. ..............
private int calculateAnnualCalculationTotal (Insurance insurance , int year , Optional<CalculatedPaymentsDto> calculatedPayments , Optional<PaidPaymentsDTO> paidPayments) {
calculatedPayments = Optional.ofNullable(paymentService.getCalculatedPayments(insurance.getId(), year));
paidPayments = Optional.ofNullable(paymentService.getPaidPayments(insurance.getInsuranceYears(), year));
totalSum = calculatedPayments.get().getParam1()+ paidPayments.get().getParam2();
.....
}
Or is there a better way to write this ..