<%@ page contentType="text/html; charset=Shift_JIS" %>
<%@ page import="trail.sfsb.*,
                 javax.naming.*,
                 java.text.*,
                 java.util.ArrayList"%>

<%!
  private NumberFormat nf = null;

  public void jspInit () {
    nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(2);
  }
%>

<%
  Calculator cal =
      (Calculator) session.getAttribute("sfsb_cal");
  if (cal == null) {
    try {
      InitialContext ctx = new InitialContext();
      cal = (Calculator) ctx.lookup(
                  "EJB3Trail/StatefulCalculator/local");
      session.setAttribute ("sfsb_cal", cal);
    } catch (Exception e) {
      e.printStackTrace ();
    }
  }

  String result;

  int start = 25;
  int end = 65;
  double growthrate = 0.08;
  double saving = 300.0;

  try {
    start = Integer.parseInt(request.getParameter ("start"));
    end = Integer.parseInt(request.getParameter ("end"));
    growthrate = Double.parseDouble(request.getParameter ("growthrate"));
    saving = Double.parseDouble(request.getParameter ("saving"));

    double res = cal.calculate(start, end, growthrate, saving);
    result = nf.format(res);

  } catch (Exception e) {
    // e.printStackTrace ();
    result = "Not valid";
  }
%>

<html><body>

<p>セッション履歴機能付きの投資計算プログラム<br/>
<form action="calculator.jsp" method="POST">
  開始年齢 = <input type="text" name="start" value="<%=start%>"><br/>
  終了年齢 = <input type="text" name="end" value="<%=end%>"><br/>
  年成長率 = <input type="text" name="growthrate" value="<%=growthrate%>"><br/>
  月掛金額 = <input type="text" name="saving" value="<%=saving%>"><br/>
  <input type="submit" value="計算">
  <INPUT type="button" value="閉じる" onClick="window.close()">
</form>
</p>

<p>最新の計算結果：終了年齢での合計投資額は、
<b><%=result%></b></p>

<p><i>過去の計算結果</i><br/>
<%
  int entries = cal.getStarts().size();
%>
<table>
<tr>
<td>開始年齢</td>
<td>終了年齢</td>
<td>年成長率</td>
<td>月掛金額</td>
<td><b>合計投資額</b></td>
</tr>

<%
  for (int i = 0; i < entries; i++) {
%>

<tr>
<td><%=cal.getStarts().get(i)%></td>
<td><%=cal.getEnds().get(i)%></td>
<td><%=nf.format(cal.getGrowthrates().get(i))%></td>
<td><%=nf.format(cal.getSavings().get(i))%></td>
<td><%=nf.format(cal.getResults().get(i))%></td>
</tr>

<%
  }
%>
</table></p>

</body></html>