Advertisement
Guest User

Untitled

a guest
Jun 30th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.91 KB | None | 0 0
  1. package matematika;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.ArrayList;
  11. import java.util.Objects;
  12. import java.util.Random;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15. import javax.servlet.ServletException;
  16. import javax.servlet.annotation.WebServlet;
  17. import javax.servlet.http.HttpServlet;
  18. import javax.servlet.http.HttpServletRequest;
  19. import javax.servlet.http.HttpServletResponse;
  20.  
  21. /**
  22.  *
  23.  * @author qasko
  24.  */
  25. @WebServlet(name = "Main", urlPatterns = {"/Main"})
  26. public class Main extends HttpServlet {
  27.  
  28.     private ArrayList<Integer> correctResults;
  29.     private ArrayList<Integer> userResults;
  30.     private ArrayList<String> arrayList;
  31.  
  32.     private final String driver = "com.mysql.jdbc.Driver";
  33.     private Connection con = null;
  34.     private Statement stmt = null;
  35.     private ResultSet rs = null;
  36.     private final String username = "root";
  37.     private final String password = "";
  38.     private final String URL = "jdbc:mysql://localhost/math";
  39.  
  40.     private int MIN_NUMBER;
  41.     private int MAX_NUMBER;
  42.  
  43.     private int a;
  44.     private int b;
  45.     private int c;
  46.  
  47.     private int count;
  48.  
  49.     private String o;
  50.  
  51.     @Override
  52.     public void init() throws ServletException {
  53.  
  54.         super.init();
  55.  
  56.         try {
  57.             Class.forName(driver);
  58.             con = DriverManager.getConnection(URL, username, password);
  59.         } catch (ClassNotFoundException | SQLException ex) {
  60.             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  61.         }
  62.  
  63.         try {
  64.             stmt = con.createStatement();
  65.             rs = stmt.executeQuery("select count from problems_count");
  66.             rs.next();
  67.             count = rs.getInt(1);
  68.         } catch (SQLException ex) {
  69.             Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
  70.         }
  71.  
  72.         correctResults = new ArrayList<>();
  73.         userResults = new ArrayList<>();
  74.         arrayList = new ArrayList<>();
  75.  
  76.         MIN_NUMBER = 0;
  77.         MAX_NUMBER = 100;
  78.  
  79.         a = 0;
  80.         b = 0;
  81.         c = 0;
  82.  
  83.         o = null;
  84.     }
  85.  
  86.     public int randInt(int min, int max) {
  87.         Random rand = new Random();
  88.  
  89.         int randomNum = rand.nextInt((max - min) + 1) + min;
  90.  
  91.         return randomNum;
  92.     }
  93.  
  94.     public int mathProblem() {
  95.  
  96.         int plusMinus = randInt(0, 1);
  97.  
  98.         if (plusMinus == 0) {
  99.             a = randInt(MIN_NUMBER, MAX_NUMBER);
  100.             b = randInt(MIN_NUMBER, MAX_NUMBER);
  101.  
  102.             while ((a + b) > 100) {
  103.                 a = randInt(MIN_NUMBER, MAX_NUMBER);
  104.                 b = randInt(MIN_NUMBER, MAX_NUMBER);
  105.             }
  106.  
  107.             c = a + b;
  108.             o = "+";
  109.  
  110.             //kontrola
  111.             System.out.println(a + o + b + "=" + c);
  112.  
  113.             //listy
  114.             correctResults.add(c);
  115.             arrayList.add(a + " " + o + " " + b + " = ");
  116.  
  117.             return c;
  118.         } else {
  119.             a = randInt(MIN_NUMBER, MAX_NUMBER);
  120.             b = randInt(MIN_NUMBER, a);
  121.             c = a - b;
  122.             o = "-";
  123.             System.out.println(a + o + b + "=" + c);
  124.  
  125.             //listy
  126.             correctResults.add(c);
  127.             arrayList.add(a + " " + o + " " + b + " = ");
  128.  
  129.             return c;
  130.         }
  131.     }
  132.  
  133.     /**
  134.      * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  135.      * methods.
  136.      *
  137.      * @param request servlet request
  138.      * @param response servlet response
  139.      * @throws ServletException if a servlet-specific error occurs
  140.      * @throws IOException if an I/O error occurs
  141.      */
  142.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  143.             throws ServletException, IOException {
  144.         response.setContentType("text/html;charset=UTF-8");
  145.         try (PrintWriter out = response.getWriter()) {
  146.  
  147.             /* TODO output your page here. You may use following sample code. */
  148.             out.println("<!DOCTYPE html>");
  149.             out.println("<html>");
  150.             out.println("<head>");
  151.             out.println("<title>Servlet Main</title>");
  152.             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\">");
  153.             out.println("</head>");
  154.             out.println("<body>");
  155.             out.println("<div style='margin-top: 5em' class='container'>");
  156.  
  157.             if (request.getContentType() != null) {
  158.  
  159.                 String button = request.getParameter("button");
  160.  
  161.                 double correct = 0;
  162.  
  163.                 if (button.equals("Check")) {
  164.  
  165.                     out.println("<div class='row'>");
  166.  
  167.                     for (int i = 0; i < count; i++) {
  168.  
  169.                         out.println("<div class='col-md-6'>");
  170.  
  171.                         try {
  172.                             userResults.add(Integer.parseInt(request.getParameter("problem" + i)));
  173.                         } catch (NumberFormatException ex) {
  174.                             userResults.add(0);
  175.                         }
  176.  
  177.                         if (Objects.equals(userResults.get(i), correctResults.get(i))) {
  178.                             out.println("<p><h3 style='color: green'>" + arrayList.get(i) + userResults.get(i) + "</h3></p>"); //spravne
  179.                             correct++;
  180.                         } else {
  181.                             out.println("<p><h3 style='color: red'>" + arrayList.get(i) + correctResults.get(i) + " (" + userResults.get(i) + ")</h3></p>"); //nespravne
  182.                         }
  183.  
  184.                         out.println("</div>");
  185.                     }
  186.  
  187.                     out.println("<h2><div class='col-md-12'><hr>Success: " + Math.round((correct / count) * 100) + "%</div></h2>");
  188.  
  189.                     out.println("</div>");
  190.                 }
  191.  
  192.             }
  193.  
  194.             if (request.getContentType() == null) {
  195.  
  196.                 arrayList.clear();
  197.                 correctResults.clear();
  198.                 userResults.clear();
  199.  
  200.                 for (int i = 0; i < count; i++) {
  201.                     mathProblem();
  202.                 }
  203.  
  204.                 out.println("<form action='Main' method='post'>");
  205.                 out.println("<div class='row'>");
  206.  
  207.                 for (int i = 0; i < arrayList.size(); i++) {
  208.                     out.println("<div class='col-md-6'>");
  209.                     out.println("<fieldset class='form-group'>");
  210.                     out.println("<label for='problem" + i + "'>" + arrayList.get(i) + "</label>");
  211.                     out.println("<input type='text' class='form-control' id='problem" + i + "' name='problem" + i + "'>");
  212.                     out.println("</fieldset></div>");
  213.                 }
  214.  
  215.                 out.println("<div class='col-md-12'><input class='btn btn-primary' type='submit' value='Check' name='button'></div>");
  216.                 out.println("</form>");
  217.             }
  218.  
  219.             out.println("</div></div>");
  220.  
  221.             out.println("<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js\"></script>\n"
  222.                     + "<script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.2/js/bootstrap.min.js\" integrity=\"sha384-vZ2WRJMwsjRMW/8U7i6PWi6AlO1L79snBrmgiDpgIWJ82z8eA5lenwvxbMV1PAh7\" crossorigin=\"anonymous\"></script>");
  223.             out.println("</body>");
  224.             out.println("</html>");
  225.         }
  226.  
  227.     }
  228.  
  229.     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  230.     /**
  231.      * Handles the HTTP <code>GET</code> method.
  232.      *
  233.      * @param request servlet request
  234.      * @param response servlet response
  235.      * @throws ServletException if a servlet-specific error occurs
  236.      * @throws IOException if an I/O error occurs
  237.      */
  238.     @Override
  239.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  240.             throws ServletException, IOException {
  241.         processRequest(request, response);
  242.     }
  243.  
  244.     /**
  245.      * Handles the HTTP <code>POST</code> method.
  246.      *
  247.      * @param request servlet request
  248.      * @param response servlet response
  249.      * @throws ServletException if a servlet-specific error occurs
  250.      * @throws IOException if an I/O error occurs
  251.      */
  252.     @Override
  253.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  254.             throws ServletException, IOException {
  255.         processRequest(request, response);
  256.     }
  257.  
  258.     /**
  259.      * Returns a short description of the servlet.
  260.      *
  261.      * @return a String containing servlet description
  262.      */
  263.     @Override
  264.     public String getServletInfo() {
  265.         return "Short description";
  266.     }// </editor-fold>
  267.  
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement