Untitled

mail@pastecode.io avatar
unknown
java
21 days ago
1.5 kB
2
Indexable
Never
package controllers;

import calculation.ConcreteCalculator;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet("/ConcreteServlet")
public class ConcreteServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            double width = Double.parseDouble(request.getParameter("width"));
            double length = Double.parseDouble(request.getParameter("length"));
            double thickness = Double.parseDouble(request.getParameter("thickness"));
            int bagSize = Integer.parseInt(request.getParameter("bagSize"));

            ConcreteCalculator calculator = new ConcreteCalculator();
            calculator.calculate(width, length, thickness, bagSize);

            request.setAttribute("cubicYards", calculator.getFormattedCubicYards());
            request.setAttribute("numBags", calculator.getNumBags());
            request.setAttribute("totalCost", calculator.getFormattedTotalCost());
            request.getRequestDispatcher("/result.jsp").forward(request, response);
        } catch (NumberFormatException e) {
            String errorMessage = "Please fill out all fields correctly.";
            request.setAttribute("errorMessage", errorMessage);
            request.getRequestDispatcher("/index.html").forward(request, response);
        }
    }
}
Leave a Comment