import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Exam extends HttpServlet {
    public void doGet(HttpServletRequest request,
              HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        String message = (String)session.getAttribute("message");
        String one = (String)session.getAttribute("one"),
               two = (String)session.getAttribute("two"),
               balance = (String)session.getAttribute("balance"),
               answer = "", bet = "";
        if (message == null) {
          one = (int)(Math.random() * 6 + 1) + ""; session.setAttribute("one", one);
          two = (int)(Math.random() * 6 + 1) + ""; session.setAttribute("two", two);
          balance = "5000"; session.setAttribute("balance", balance);
          message = "Welcome to the game."; session.setAttribute("message", message);
        } else {
          answer = request.getParameter("guess");
          bet = request.getParameter("bet");
          int sum = Integer.parseInt(one) + Integer.parseInt(two);
          if (sum % 2 == 0 && answer.equals("eve") ||
              sum % 2 == 1 && answer.equals("odd")) {
            balance =  Integer.parseInt(balance) + Integer.parseInt(bet) + "";
          } else {
            balance =  Integer.parseInt(balance) - Integer.parseInt(bet) + "";
          }
          one = (int)(Math.random() * 6 + 1) + ""; session.setAttribute("one", one);
          two = (int)(Math.random() * 6 + 1) + ""; session.setAttribute("two", two);
          session.setAttribute("balance", balance);
          message = "You are a new user no longer.";
          session.setAttribute("message", message);

        }
    out.println("<form method=GET>" + message +
          "Your balance is: " + balance + "<p>" +
          "How is " + one + " + " + two + ", odd or even? <p>" +
          "Please choose <select name=guess> " +
          "   <option value=non> Click me! " +
          "   <option value=eve> Even " +
          "   <option value=odd> Odd " +
          "  </select> <p> ... and place your bet here: " +
          "  <input type=text name=bet size=8> <p> Then press " +
          " <input type=submit value=Proceed> when you're ready." +
          "</form>"
        );
    }
}