github.com/ismailbayram/bigpicture@v0.0.0-20231225173155-e4b21f5efcff/internal/browser/javaproject/src/main/com/shashi/servlets/UserViewTrainFwd.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("/userviewtrainfwd")
    23  public class UserViewTrainFwd extends HttpServlet {
    24  
    25  	TrainService trainService = new TrainServiceImpl();
    26  
    27  	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    28  		res.setContentType("text/html");
    29  		PrintWriter pw = res.getWriter();
    30  		TrainUtil.validateUserAuthorization(req, UserRole.CUSTOMER);
    31  		try {
    32  			List<TrainBean> trains = trainService.getAllTrains();
    33  			if (trains != null && !trains.isEmpty()) {
    34  				RequestDispatcher rd = req.getRequestDispatcher("UserViewTrains.html");
    35  				rd.include(req, res);
    36  				pw.println("<div class='main'><p1 class='menu'>Running Trains</p1></div>");
    37  				pw.println("<div class='tab'><table><tr><th>Train Name</th><th>Train Number</th>"
    38  						+ "<th>From Station</th><th>To Station</th><th>Time</th><th>Seats Available</th><th>Fare (INR)</th><th>Booking</th></tr>");
    39  
    40  				for (TrainBean train : trains) {
    41  					int hr = (int) (Math.random() * 24);
    42  					int min = (int) (Math.random() * 60);
    43  					String time = (hr < 10 ? ("0" + hr) : hr) + ":" + ((min < 10) ? "0" + min : min);
    44  					pw.println("" + "<tr> " + "" + "<td><a href='view?trainNo=" + train.getTr_no() + "&fromStn="
    45  							+ train.getFrom_stn() + "&toStn=" + train.getTo_stn() + "'>" + train.getTr_name()
    46  							+ "</a></td>" + "<td>" + train.getTr_no() + "</td>" + "<td>" + train.getFrom_stn() + "</td>"
    47  							+ "<td>" + train.getTo_stn() + "</td>" + "<td>" + time + "</td>" + "<td>" + train.getSeats()
    48  							+ "</td>" + "<td>" + train.getFare() + " RS</td>" + "<td><a href='booktrainbyref?trainNo="
    49  							+ train.getTr_no() + "&fromStn=" + train.getFrom_stn() + "&toStn=" + train.getTo_stn()
    50  							+ "'><div class='red'>Book Now</div></a></td></tr>");
    51  				}
    52  				pw.println("</table></div>");
    53  			} else {
    54  				RequestDispatcher rd = req.getRequestDispatcher("UserViewTrains.html");
    55  				rd.include(req, res);
    56  				pw.println("<div class='main'><p1 class='menu red'> No Running Trains</p1></div>");
    57  			}
    58  		} catch (Exception e) {
    59  			throw new TrainException(422, this.getClass().getName() + "_FAILED", e.getMessage());
    60  		}
    61  
    62  	}
    63  
    64  }