i have the below insurance calculator class
what i would expect the program to do is when i click the "calculatePremium" button is that the front-end will move to the next step of the form "payment information" and the program should update the input "payment-amount" with the calculated price
Instead i am getting redirected to http://localhost:8081/?payment-amount=87.84600
how do i fix this?
package ro.fortech.guardianangel.insurance;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.Period;
public class InsuranceCalculator {
private static final BigDecimal STANDARD_INSURANCE_VALUE = BigDecimal.valueOf(50);
public static BigDecimal calculatePremium(LocalDate birthDate, int carYear, String fuelType, int emissions, int cCapacity) {
BigDecimal premium = STANDARD_INSURANCE_VALUE;
// Calculate age factor
int age = Period.between(birthDate, LocalDate.now()).getYears();
if (age >= 18 && age <= 30 || age >= 60) {
premium = premium.multiply(BigDecimal.valueOf(1.2));
} else if (age >= 31 && age <= 59) {
premium = premium.multiply(BigDecimal.valueOf(1.1));
}
// Calculate car year factor
int carAge = LocalDate.now().getYear() - carYear;
if (carAge > 15) {
premium = premium.multiply(BigDecimal.valueOf(1.2));
} else if (carAge >= 6 && carAge <= 14) {
premium = premium.multiply(BigDecimal.valueOf(1.1));
}
// Calculate fuel type factor
if (fuelType.equalsIgnoreCase("petrol")) {
premium = premium.multiply(BigDecimal.valueOf(1.1));
} else if (fuelType.equalsIgnoreCase("diesel")) {
premium = premium.multiply(BigDecimal.valueOf(1.2));
}
// Calculate emissions factor
if (emissions > 150) {
premium = premium.multiply(BigDecimal.valueOf(1.2));
} else if (emissions >= 75 && emissions <= 149) {
premium = premium.multiply(BigDecimal.valueOf(1.1));
}
// Calculate cylinder capacity factor
if (cCapacity >= 2001) {
premium = premium.multiply(BigDecimal.valueOf(1.3));
} else if (cCapacity >= 1500 && cCapacity <= 2000) {
premium = premium.multiply(BigDecimal.valueOf(1.2));
} else if (cCapacity >= 800 && cCapacity <= 1499) {
premium = premium.multiply(BigDecimal.valueOf(1.1));
}
return premium;
}
}
and the InsuranceController class
package ro.fortech.guardianangel.insurance;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
@CrossOrigin(origins="*")
@Controller
public class InsuranceController {
@PostMapping("/calculatePremium")
public String calculatePremium(
@RequestParam("birthDate") String birthDateStr,
@RequestParam("carYear") int carYear,
@RequestParam("fuelType") String fuelType,
@RequestParam("emissions") int emissions,
@RequestParam("cCapacity") int cCapacity,
RedirectAttributes redirectAttributes
) {
try {
LocalDate birthDate = LocalDate.parse(birthDateStr, DateTimeFormatter.ofPattern("dd-MM-yyyy"));
BigDecimal premium = InsuranceCalculator.calculatePremium(birthDate, carYear, fuelType, emissions, cCapacity);
redirectAttributes.addFlashAttribute("premium", premium);
return "redirect:/?payment-amount=" + premium;
} catch (Exception e) {
redirectAttributes.addFlashAttribute("error", "Invalid input data. Please try again.");
return "redirect:/";
}
}
}
i know its not secure, but it doesn't matter, its just a project i need to finish, security is not important
below is the front-end part
</form>
</div>
</nav>
<!-- MultiStep Form -->
<div class="container-fluid" id="grad1">
<div class="row justify-content-center mt-0">
<div
class="col-11 col-sm-9 col-md-7 col-lg-6 text-center p-0 mt-3 mb-2"
>
<div class="card px-0 pt-4 pb-0 mt-0 mb-3">
<h2><strong>Get your car insurance offer now!</strong></h2>
<p>Fill all form field to go to next step</p>
<div class="row">
<div class="col-md-12 mx-0">
<form action="http://localhost:8081/calculatePremium" method="POST" id="msform" enctype="application/json">
<!-- progressbar -->
<ul id="progressbar">
<li class="active" id="pDetails">
<strong>Personal Details</strong>
</li>
<li id="cDetails"><strong>Car Details</strong></li>
<li id="payment"><strong>Payment</strong></li>
<li id="finish"><strong>Finish</strong></li>
</ul>
<!-- fieldsets -->
<fieldset>
<div class="form-card">
<h2 class="fs-title">
Personal Details <small>(car owner)</small>
</h2>
<input
type="text"
name="fName"
placeholder="First Name"
/>
<input
type="text"
name="lName"
placeholder="Last Name"
/>
<input
type="email"
name="email"
placeholder="Email"
/>
<input
type="text"
name="pNumber"
placeholder="Phone number"
/>
<input
type="text"
name="pAddress"
placeholder="Address"
/>
<input
type="text"
name="pId"
placeholder="Personal Identification Number"
/>
<input
type="text"
name="birthDate"
pattern="d{2}-d{2}-d{4}"
placeholder="Date of Birth (dd-mm-yyy)"
/>
</div>
<input
type="button"
name="next"
class="next action-button"
value="Next Step"
/>
</fieldset>
<fieldset>
<div class="form-card">
<h2 class="fs-title">Car Details</h2>
<input
type="text"
name="vin"
placeholder="VIN"
/>
<input
type="text"
name="make"
placeholder="Make"
/>
<input
type="text"
name="model"
placeholder="Model"
/>
<input
type="number"
name="carYear"
min="1886"
max="2023"
placeholder="Year of make"
/>
<input
type="text"
name="fuelType"
placeholder="Fuel Type"
/>
<input
type="text"
name="emissions"
placeholder="CO2 emissions (g/km)"
/>
<input
type="number"
name="cCapacity"
placeholder="Cylinder Capacity"
/>
</div>
<input
type="button"
name="previous"
class="previous action-button-previous"
value="Previous"
/>
<input
type="button"
name="calculatePremium"
id="calculatePremium"
class="next action-button"
value="Calculate Premium"
/>
</fieldset>
<fieldset>
<div class="form-card">
<h2 class="fs-title mt-1">Payment Information</h2>
<label for="payment-amount" class="mt-3">Insurance Policy Price</label></br>
<input type="text" id="payment-amount" class="col-6" name="payment-amount"></br>
<label class="pay mt-1">Card Holder Name</label>
<input
type="text"
name="holdername"
/>
<div class="row">
<div class="col-9">
<label class="pay">Card Number </label>
<input
type="text"
name="cardno"
placeholder="XXXX-XXXX-XXXX-XXXX"
/>
</div>
<div class="col-3">
<label class="pay">CVC/CVV</label>
<input
type="password"
name="cvcpwd"
placeholder="***"
/>
</div>
</div>
<div class="row">
<div class="col-3 mt-1">
<label class="pay">Expiry Date:</label>
</div>
<div class="row">
<div class="" id="month">
<input
class="col-12"
type="month"
name="cardno"
placeholder="Month"
min="2023-03"
value="2023-03"
/>
</div>
</div>
</div>
</div>
<input
type="button"
name="previous"
class="previous action-button-previous"
value="Previous"
/>
<input
type="button"
name="make_payment"
class="next action-button"
value="Confirm"
/>
</fieldset>
<fieldset>
<div class="form-card">
<h2 class="fs-title text-center">Success !</h2>
<br /><br />
<div class="row justify-content-center">
<div class="col-3">
<img
src="https://img.icons8.com/color/96/000000/ok--v2.png"
class="fit-image"
/>
</div>
</div>
<br /><br />
<div class="row justify-content-center">
<div class="col-7 text-center">
<h5>Payment successful,</h5>
<a>Click here </a>
<h5>
to download your policy as a printable pdf file
</h5>
</div>
</div>
</div>
</fieldset>
</form>
also if it helps, these are the debug logs from intellij:
2023-03-18 20:41:55.488 DEBUG 4100 --- [nio-8081-exec-7] o.s.web.servlet.DispatcherServlet : POST "/calculatePremium", parameters={masked}
2023-03-18 20:41:55.488 DEBUG 4100 --- [nio-8081-exec-7] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to ro.fortech.guardianangel.insurance.InsuranceController#calculatePremium(String, int, String, int, int, RedirectAttributes)
2023-03-18 20:41:55.488 DEBUG 4100 --- [nio-8081-exec-7] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2023-03-18 20:41:55.490 DEBUG 4100 --- [nio-8081-exec-7] o.s.web.servlet.view.RedirectView : View name 'redirect:', model {}
2023-03-18 20:41:55.490 DEBUG 4100 --- [nio-8081-exec-7] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2023-03-18 20:41:55.490 DEBUG 4100 --- [nio-8081-exec-7] o.s.web.servlet.DispatcherServlet : Completed 302 FOUND
2023-03-18 20:41:55.494 DEBUG 4100 --- [nio-8081-exec-8] o.s.web.servlet.DispatcherServlet : GET "/?payment-amount=87.84600", parameters={masked}
2023-03-18 20:41:55.496 DEBUG 4100 --- [nio-8081-exec-8] o.s.b.a.w.s.WelcomePageHandlerMapping : Mapped to ParameterizableViewController [view="forward:index.html"]
2023-03-18 20:41:55.496 DEBUG 4100 --- [nio-8081-exec-8] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2023-03-18 20:41:55.496 DEBUG 4100 --- [nio-8081-exec-8] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/xhtml+xml, image/avif, image/webp, image/apng, application/xml;q=0.9, application/signed-exchange;v=b3;q=0.7, */*;q=0.8]
2023-03-18 20:41:55.496 DEBUG 4100 --- [nio-8081-exec-8] o.s.w.servlet.view.InternalResourceView : View name 'forward:', model {premium=87.84600}
2023-03-18 20:41:55.496 DEBUG 4100 --- [nio-8081-exec-8] o.s.w.servlet.view.InternalResourceView : Forwarding to [index.html]
2023-03-18 20:41:55.496 DEBUG 4100 --- [nio-8081-exec-8] o.s.web.servlet.DispatcherServlet : "FORWARD" dispatch for GET "/index.html?payment-amount=87.84600", parameters={masked}
2023-03-18 20:41:55.497 DEBUG 4100 --- [nio-8081-exec-8] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2023-03-18 20:41:55.498 DEBUG 4100 --- [nio-8081-exec-8] o.s.web.servlet.DispatcherServlet : Exiting from "FORWARD" dispatch, status 200
2023-03-18 20:41:55.498 DEBUG 4100 --- [nio-8081-exec-8] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2023-03-18 20:41:55.498 DEBUG 4100 --- [nio-8081-exec-8] o.s.web.servlet.DispatcherServlet : Completed 200 OK
2023-03-18 20:41:55.521 DEBUG 4100 --- [nio-8081-exec-6] o.s.web.servlet.DispatcherServlet : GET "/css/style.css", parameters={}
2023-03-18 20:41:55.521 DEBUG 4100 --- [nio-8081-exec-6] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2023-03-18 20:41:55.521 DEBUG 4100 --- [nio-8081-exec-6] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2023-03-18 20:41:55.524 DEBUG 4100 --- [nio-8081-exec-6] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2023-03-18 20:41:55.524 DEBUG 4100 --- [nio-8081-exec-6] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2023-03-18 20:41:55.524 DEBUG 4100 --- [nio-8081-exec-6] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
2023-03-18 20:41:55.524 DEBUG 4100 --- [nio-8081-exec-6] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error", parameters={}
2023-03-18 20:41:55.524 DEBUG 4100 --- [nio-8081-exec-6] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
2023-03-18 20:41:55.524 DEBUG 4100 --- [nio-8081-exec-6] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2023-03-18 20:41:55.525 DEBUG 4100 --- [nio-8081-exec-6] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'application/json;q=0.1', given [text/css, */*;q=0.1] and supported [application/json, application/*+json, application/json, application/*+json]
2023-03-18 20:41:55.525 DEBUG 4100 --- [nio-8081-exec-6] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing [{timestamp=Sat Mar 18 20:41:55 EET 2023, status=404, error=Not Found, path=/css/style.css}]
2023-03-18 20:41:55.525 DEBUG 4100 --- [nio-8081-exec-6] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2023-03-18 20:41:55.525 DEBUG 4100 --- [nio-8081-exec-6] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404
2023-03-18 20:41:55.528 DEBUG 4100 --- [nio-8081-exec-9] o.s.web.servlet.DispatcherServlet : GET "/pictures/logo.png", parameters={}
2023-03-18 20:41:55.530 DEBUG 4100 --- [nio-8081-exec-9] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped to ResourceHttpRequestHandler [classpath [META-INF/resources/], classpath [resources/], classpath [static/], classpath [public/], ServletContext [/]]
2023-03-18 20:41:55.530 DEBUG 4100 --- [nio-8081-exec-9] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2023-03-18 20:41:55.531 DEBUG 4100 --- [nio-8081-exec-9] o.s.w.s.r.ResourceHttpRequestHandler : Resource not found
2023-03-18 20:41:55.531 DEBUG 4100 --- [nio-8081-exec-9] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2023-03-18 20:41:55.531 DEBUG 4100 --- [nio-8081-exec-9] o.s.web.servlet.DispatcherServlet : Completed 404 NOT_FOUND
2023-03-18 20:41:55.531 DEBUG 4100 --- [nio-8081-exec-9] o.s.web.servlet.DispatcherServlet : "ERROR" dispatch for GET "/error", parameters={}
2023-03-18 20:41:55.532 DEBUG 4100 --- [nio-8081-exec-9] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#error(HttpServletRequest)
2023-03-18 20:41:55.532 DEBUG 4100 --- [nio-8081-exec-9] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor
2023-03-18 20:41:55.532 DEBUG 4100 --- [nio-8081-exec-9] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Using 'application/json;q=0.8', given [image/avif, image/webp, image/apng, image/svg+xml, image/*, */*;q=0.8] and supported [application/json, application/*+json, application/json, application/*+json]
2023-03-18 20:41:55.532 DEBUG 4100 --- [nio-8081-exec-9] o.s.w.s.m.m.a.HttpEntityMethodProcessor : Writing [{timestamp=Sat Mar 18 20:41:55 EET 2023, status=404, error=Not Found, path=/pictures/logo.png}]
2023-03-18 20:41:55.533 DEBUG 4100 --- [nio-8081-exec-9] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor
2023-03-18 20:41:55.533 DEBUG 4100 --- [nio-8081-exec-9] o.s.web.servlet.DispatcherServlet : Exiting from "ERROR" dispatch, status 404