<%@ 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);
  }
%>

<%
    if ("AddFund".equals(request.getParameter("action"))) {
        cal.addFund (request.getParameter("fundname"),
            Double.parseDouble(request.getParameter("fundrate")));
    }
%>

<html><body>

<p>投資会社を追加する:<br/>
<form action="addfund.jsp" method="POST">
  投資会社名 : <input type="text" name="fundname" value="">
  年成長率 : <input type="text" name="fundrate" value="0.05">
  <input type="hidden" name="action" value="AddFund"><br/>
  <input type="submit" value="投資会社を追加する">
  <INPUT type="button" value="閉じる" onClick="window.close()">
</form><br/>

<%
  // Collection <Fund> funds = cal.getFunds();
  Collection funds = cal.getFunds();
%>

<%=funds.size()%></b>社の投資会社がデータベースに登録されています。<br/>

<table>
<tr>
<td>投資会社名</td>
<td>年成長率</td>
</tr>

<%
    for (Iterator iter = funds.iterator(); iter.hasNext();) {
    Fund fund = (Fund) iter.next();
%>

<tr>
<td><%=fund.getName()%></td>
<td><%=nf.format(fund.getGrowthrate())%></td>
</tr>

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

</body></html>