github.com/ismailbayram/bigpicture@v0.0.0-20231225173155-e4b21f5efcff/internal/browser/javaproject/src/main/com/shashi/servlets/FareEnq.java (about)

     1  package com.shashi.servlets;
     2  
     3  import java.io.IOException;
     4  import java.io.PrintWriter;
     5  import java.util.List;
     6  
     7  import javax.servlet.RequestDispatcher;
     8  import javax.servlet.ServletException;
     9  import javax.servlet.annotation.WebServlet;
    10  import javax.servlet.http.HttpServlet;
    11  import javax.servlet.http.HttpServletRequest;
    12  import javax.servlet.http.HttpServletResponse;
    13  
    14  import com.shashi.beans.TrainBean;
    15  import com.shashi.beans.TrainException;
    16  import com.shashi.constant.UserRole;
    17  import com.shashi.service.TrainService;
    18  import com.shashi.service.impl.TrainServiceImpl;
    19  import com.shashi.utility.TrainUtil;
    20  
    21  @SuppressWarnings("serial")
    22  @WebServlet("/fareenq")
    23  public class FareEnq extends HttpServlet {
    24  	TrainService trainService = new TrainServiceImpl();
    25  
    26  	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    27  		res.setContentType("text/html");
    28  		PrintWriter pw = res.getWriter();
    29  
    30  		TrainUtil.validateUserAuthorization(req, UserRole.CUSTOMER);
    31  
    32  		try {
    33  			String fromStation = req.getParameter("fromstation");
    34  			String toStation = req.getParameter("tostation");
    35  			List<TrainBean> trains = trainService.getTrainsBetweenStations(fromStation, toStation);
    36  			if (trains != null && !trains.isEmpty()) {
    37  				RequestDispatcher rd = req.getRequestDispatcher("UserHome.html");
    38  				rd.include(req, res);
    39  				pw.println("<div class='main'><p1 class='menu'>Fare for Trains BetWeen Station " + fromStation + " and "
    40  						+ toStation + " is as below</p1></div>");
    41  				pw.println("<div class='tab'><table><tr><th>Train Name</th><th>Train No</th>"
    42  						+ "<th>From Stn</th><th>To Stn</th><th>Time</th><th>Seats</th><th>Fare (INR)</th><th>Action</th></tr>");
    43  				for (TrainBean train : trains) {
    44  					int hr = (int) (Math.random() * 24);
    45  					int min = (int) (Math.random() * 60);
    46  					String time = (hr < 10 ? ("0" + hr) : hr) + ":" + ((min < 10) ? "0" + min : min);
    47  
    48  					pw.println("" + "<tr><td>" + train.getTr_name() + "</td>" + "<td>" + train.getTr_no() + "</td>"
    49  							+ "<td>" + train.getFrom_stn() + "</td>" + "<td>" + train.getTo_stn() + "</td>" + "<td>"
    50  							+ time + "</td>" + "<td>" + train.getSeats() + "</td>" + "<td>" + train.getFare()
    51  							+ " RS</td><td><a href='booktrainbyref?trainNo=" + train.getTr_no() + "&fromStn="
    52  							+ train.getFrom_stn() + "&toStn=" + train.getTo_stn()
    53  							+ "'><div class='red'>Book Now</div></a></td>" + "</tr>");
    54  				}
    55  				pw.println("</table></div>");
    56  			} else {
    57  				RequestDispatcher rd = req.getRequestDispatcher("TrainBwStn.html");
    58  				rd.include(req, res);
    59  				pw.println("<div class='tab'><p1 class='menu'>There are no trains Between " + fromStation + " and "
    60  						+ toStation + "</p1></div>");
    61  			}
    62  		} catch (Exception e) {
    63  			throw new TrainException(422, this.getClass().getName() + "_FAILED", e.getMessage());
    64  		}
    65  
    66  	}
    67  }