<%@ page contentType="text/html; charset=Shift_JIS" %>
<%@ page import="trail.interceptor.*,
                 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("interceptor_cal");
  if (cal == null) {
    try {
      InitialContext ctx = new InitialContext();
      cal = (Calculator) ctx.lookup(
                  "EJB3Trail/InterceptorCalculator/local");
      session.setAttribute ("interceptor_cal", cal);
    } catch (Exception e) {
      e.printStackTrace ();
    }
  }

  String result;

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

  // clear the call trace
  cal.setTrace ("");

  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>

<%
  ArrayList starts = cal.getStarts();
  ArrayList ends = cal.getEnds ();
  ArrayList growthrates = cal.getGrowthrates ();
  ArrayList savings = cal.getSavings ();
  ArrayList results = cal.getResults ();
  for (int i = 0; i < entries; i++) {
%>

<tr>
<td><%=starts.get(i)%></td>
<td><%=ends.get(i)%></td>
<td><%=nf.format(growthrates.get(i))%></td>
<td><%=nf.format(savings.get(i))%></td>
<td><%=nf.format(results.get(i))%></td>
</tr>

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

<p><i>直前の呼び出しトレース</i><br/>
<%=cal.getTrace()%>
</p>

</body></html>
