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

<%!
  private Calculator cal = null;
  private NumberFormat nf = null;

  public void jspInit () {
    try {
      InitialContext ctx = new InitialContext();
      cal = (Calculator) ctx.lookup(
                  "EJB3Trail/EntityCalculator/local");
    } catch (Exception e) {
      e.printStackTrace ();
    }

    nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(2);
  }
%>

<%
    String result = "Not Valid";
    if ("Calculate".equals(request.getParameter("action"))) {
        double res = -1;
        res = cal.calculate (Integer.parseInt(request.getParameter("fund")),
                             Integer.parseInt(request.getParameter("investor")),
                             Double.parseDouble(request.getParameter("saving")));
        if (res != -1) {
            result = nf.format(res);
        }
    }
%>

<html><body>

<p>投資計算プログラム<br/>
<form action="calculator.jsp" method="POST">
  投資会社:
  <select name="fund">
<%
  Collection funds = cal.getFunds ();
  for (Iterator iter = funds.iterator(); iter.hasNext();) {
    Fund fund = (Fund) iter.next();
%>
    <option value="<%=fund.getId()%>"><%=fund.getName()%></option>
<%
  }
%>
  </select>
  と個人投資家:
  <select name="investor">
　を選択する
<%
  Collection investors = cal.getInvestors ();
  for (Iterator iter = investors.iterator(); iter.hasNext();) {
    Investor investor = (Investor) iter.next();
%>
    <option value="<%=investor.getId()%>"><%=investor.getName()%></option>
<%
  }
%>
  </select><br/>
  月掛金額 = <input type="text" name="saving" value="100">
  <input type="hidden" name="action" value="Calculate">
  <input type="submit" value="計算">
  <INPUT type="button" value="閉じる" onClick="window.close()">
</form><br/>
合計投資額は<%=result%>です<br/>
<br/>
データベース内の過去の計算記録<br/>
<table>
<tr>
<td>タイムスタンプ</td>
<td>投資会社</td>
<td>個人投資家</td>
<td>月掛金額</td>
<td><b>合計投資額</b></td>
</tr>

<%
    Collection records = cal.getRecords ();
    for (Iterator iter = records.iterator(); iter.hasNext();) {
        TimedRecord record = (TimedRecord) iter.next();
%>

<tr>
<td><%=record.getTs()%></td>
<td><%=record.getFund().getName()%></td>
<td><%=record.getInvestor().getName()%></td>
<td><%=nf.format(record.getSaving())%></td>
<td><%=nf.format(record.getResult())%></td>
</tr>

<%
    }
%>
</table>

</p>

</body></html>
