以下のような暗算アプリを作成しよう。
正答数と誤答数はアプリケーションスコープで管理すること。
問題は50~99までの二つの値の掛け算とすること。

〇問題が出題される

〇答えを入力して、送信ボタンを押すと結果が表示され。正答数などが増える。

〇retryを押すと違う問題が出題される

〇間違えた場合、誤答数が増える

解答例

Model

●model.Stat.java

package model;

import java.io.Serializable;

public class Stat implements Serializable{
	private int correct;
	private int wrong;
	public Stat() {
		correct=0;
		wrong=0;
	}
	public int getCorrect() {
		return correct;
	}
	public void setCorrect(int correct) {
		this.correct = correct;
	}
	public int getWrong() {
		return wrong;
	}
	public void setWrong(int wrong) {
		this.wrong = wrong;
	}

}

●model.StatLogic.java

package model;

public class StatLogic {
	public void correctPlus(Stat stat) {
		stat.setCorrect(stat.getCorrect()+1);
	}
	public void wrongPlus(Stat stat) {
		stat.setWrong(stat.getWrong()+1);
	}
}

●model.Qs.java

package model;

import java.io.Serializable;

public class Qs implements Serializable{

	private int correctAns;
	private String userAns;
	private boolean isCorrect;
	private String qString;

	public String getqString() {
		return qString;
	}
	public void setqString(String qString) {
		this.qString = qString;
	}
	public int getCorrectAns() {
		return correctAns;
	}
	public void setCorrectAns(int correctAns) {
		this.correctAns = correctAns;
	}
	public String getUserAns() {
		return userAns;
	}
	public void setUserAns(String userAns) {
		this.userAns = userAns;
	}
	public boolean isCorrect() {
		return isCorrect;
	}
	public void setCorrect(boolean isCorrect) {
		this.isCorrect = isCorrect;
	}

}

●model.QsLogic.java

package model;

public class QsLogic {
	public void create(Qs qs,int numsCount,int min,int max) {
		int[] nums=new int[numsCount];
		for(int i=0;i<numsCount;i++) {
			nums[i]=(int)(Math.random()*(max-min+1))+min;
		}

		int result=nums[0];
		for(int i=1;i<nums.length;i++) {
			result*=nums[i];
		}
		qs.setCorrectAns(result);
		String qString="";
		for(int i=0;i<nums.length;i++) {
			qString+=nums[i]+(i==nums.length-1? "=":"×");
		}
		qs.setqString(qString);
	}
	public void result(Qs qs,String userAns) {
		qs.setUserAns(userAns);
		int ans=Integer.parseInt(userAns);
		qs.setCorrect(qs.getCorrectAns()==ans);
	}
}

Controller

●controller.Calc.java

package controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Qs;
import model.QsLogic;
import model.Stat;
import model.StatLogic;

/**
 * Servlet implementation class Calc
 */
@WebServlet("/calc")
public class Calc extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		final int NUMCOUNT=2,MIN=50,MAX=99;
		ServletContext application=this.getServletContext();
		Stat stat=(Stat)application.getAttribute("stat");
		if(stat==null) {
			stat=new Stat();
		}
		application.setAttribute("stat",stat);

		Qs qs=new Qs();
		QsLogic logic=new QsLogic();
		logic.create(qs,NUMCOUNT,MIN,MAX);
		HttpSession session=request.getSession();
		session.setAttribute("qs",qs);
		RequestDispatcher rd=request.getRequestDispatcher("/WEB-INF/view/index.jsp");
		rd.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String ans=(String)request.getParameter("ans");
		HttpSession session=request.getSession();
		Qs qs=(Qs)session.getAttribute("qs");
		QsLogic logic=new QsLogic();
		logic.result(qs,ans);
		ServletContext application=this.getServletContext();
		Stat stat=(Stat)application.getAttribute("stat");
		StatLogic statLogic=new StatLogic();
		if(qs.isCorrect()) {
			statLogic.correctPlus(stat);
		}else {
			statLogic.wrongPlus(stat);
		}

		RequestDispatcher rd=request.getRequestDispatcher("/WEB-INF/view/index.jsp");
		rd.forward(request, response);
	}

}

View

●/WEB-INF/view/index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="model.*"%>
  <%
  Stat stat=(Stat)application.getAttribute("stat");
  Qs qs=(Qs)session.getAttribute("qs");
  String userAns=qs ==null? "":qs.getUserAns();
  %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>暗算君</title>
</head>
<body>
<h1>暗算にチャレンジ</h1>
<p>正答数:<%=stat.getCorrect() %> 誤答数:<%=stat.getWrong() %></p>
<form action="/calcapp/calc" method="post">
<p><%=qs.getqString() %><input type="number" name="ans" value="<%=userAns%>"><input type="submit" value="送信"></p>
</form>
<%if(qs.getUserAns() !=null){ %>
<p><%=qs.getqString()+qs.getCorrectAns()  %></p>
<p><%=qs.isCorrect()?"正解":"不正解" %>
<p><a href="/calcapp/calc">Retry</a></p>
<%} %>
</body>
</html>