Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package matematika;
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.ArrayList;
- import java.util.Objects;
- import java.util.Random;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.servlet.ServletException;
- import javax.servlet.annotation.WebServlet;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- /**
- *
- * @author qasko
- */
- @WebServlet(name = "Main", urlPatterns = {"/Main"})
- public class Main extends HttpServlet {
- private ArrayList<Integer> correctResults;
- private ArrayList<Integer> userResults;
- private ArrayList<String> arrayList;
- private final String driver = "com.mysql.jdbc.Driver";
- private Connection con = null;
- private Statement stmt = null;
- private ResultSet rs = null;
- private final String username = "root";
- private final String password = "";
- private final String URL = "jdbc:mysql://localhost/math";
- private int MIN_NUMBER;
- private int MAX_NUMBER;
- private int a;
- private int b;
- private int c;
- private int count;
- private String o;
- @Override
- public void init() throws ServletException {
- super.init();
- try {
- Class.forName(driver);
- con = DriverManager.getConnection(URL, username, password);
- } catch (ClassNotFoundException | SQLException ex) {
- Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
- }
- try {
- stmt = con.createStatement();
- rs = stmt.executeQuery("select count from problems_count");
- rs.next();
- count = rs.getInt(1);
- } catch (SQLException ex) {
- Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
- }
- correctResults = new ArrayList<>();
- userResults = new ArrayList<>();
- arrayList = new ArrayList<>();
- MIN_NUMBER = 0;
- MAX_NUMBER = 100;
- a = 0;
- b = 0;
- c = 0;
- o = null;
- }
- public int randInt(int min, int max) {
- Random rand = new Random();
- int randomNum = rand.nextInt((max - min) + 1) + min;
- return randomNum;
- }
- public int mathProblem() {
- int plusMinus = randInt(0, 1);
- if (plusMinus == 0) {
- a = randInt(MIN_NUMBER, MAX_NUMBER);
- b = randInt(MIN_NUMBER, MAX_NUMBER);
- while ((a + b) > 100) {
- a = randInt(MIN_NUMBER, MAX_NUMBER);
- b = randInt(MIN_NUMBER, MAX_NUMBER);
- }
- c = a + b;
- o = "+";
- //kontrola
- System.out.println(a + o + b + "=" + c);
- //listy
- correctResults.add(c);
- arrayList.add(a + " " + o + " " + b + " = ");
- return c;
- } else {
- a = randInt(MIN_NUMBER, MAX_NUMBER);
- b = randInt(MIN_NUMBER, a);
- c = a - b;
- o = "-";
- System.out.println(a + o + b + "=" + c);
- //listy
- correctResults.add(c);
- arrayList.add(a + " " + o + " " + b + " = ");
- return c;
- }
- }
- /**
- * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
- * methods.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- try (PrintWriter out = response.getWriter()) {
- /* TODO output your page here. You may use following sample code. */
- out.println("<!DOCTYPE html>");
- out.println("<html>");
- out.println("<head>");
- out.println("<title>Servlet Main</title>");
- out.println("<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/css/bootstrap.min.css\" integrity=\"sha384-y3tfxAZXuh4HwSYylfB+J125MxIs6mR5FOHamPBG064zB+AFeWH94NdvaCBm8qnd\" crossorigin=\"anonymous\">");
- out.println("</head>");
- out.println("<body>");
- out.println("<div style='margin-top: 5em' class='container'>");
- if (request.getContentType() != null) {
- String button = request.getParameter("button");
- double correct = 0;
- if (button.equals("Check")) {
- out.println("<div class='row'>");
- for (int i = 0; i < count; i++) {
- out.println("<div class='col-md-6'>");
- try {
- userResults.add(Integer.parseInt(request.getParameter("problem" + i)));
- } catch (NumberFormatException ex) {
- userResults.add(0);
- }
- if (Objects.equals(userResults.get(i), correctResults.get(i))) {
- out.println("<p><h3 style='color: green'>" + arrayList.get(i) + userResults.get(i) + "</h3></p>"); //spravne
- correct++;
- } else {
- out.println("<p><h3 style='color: red'>" + arrayList.get(i) + correctResults.get(i) + " (" + userResults.get(i) + ")</h3></p>"); //nespravne
- }
- out.println("</div>");
- }
- out.println("<h2><div class='col-md-12'><hr>Success: " + Math.round((correct / count) * 100) + "%</div></h2>");
- out.println("</div>");
- }
- }
- if (request.getContentType() == null) {
- arrayList.clear();
- correctResults.clear();
- userResults.clear();
- for (int i = 0; i < count; i++) {
- mathProblem();
- }
- out.println("<form action='Main' method='post'>");
- out.println("<div class='row'>");
- for (int i = 0; i < arrayList.size(); i++) {
- out.println("<div class='col-md-6'>");
- out.println("<fieldset class='form-group'>");
- out.println("<label for='problem" + i + "'>" + arrayList.get(i) + "</label>");
- out.println("<input type='text' class='form-control' id='problem" + i + "' name='problem" + i + "'>");
- out.println("</fieldset></div>");
- }
- out.println("<div class='col-md-12'><input class='btn btn-primary' type='submit' value='Check' name='button'></div>");
- out.println("</form>");
- }
- out.println("</div></div>");
- out.println("<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js\"></script>\n"
- + "<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js\" integrity=\"sha384-vZ2WRJMwsjRMW/8U7i6PWi6AlO1L79snBrmgiDpgIWJ82z8eA5lenwvxbMV1PAh7\" crossorigin=\"anonymous\"></script>");
- out.println("</body>");
- out.println("</html>");
- }
- }
- // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
- /**
- * Handles the HTTP <code>GET</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Handles the HTTP <code>POST</code> method.
- *
- * @param request servlet request
- * @param response servlet response
- * @throws ServletException if a servlet-specific error occurs
- * @throws IOException if an I/O error occurs
- */
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- /**
- * Returns a short description of the servlet.
- *
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }// </editor-fold>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement