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

<%!
  private Calculator cal = null;
  public void jspInit () {
    try {
      InitialContext ctx = new InitialContext();
      cal = (Calculator) ctx.lookup(
                  "EJB3Trail/StatelessCalculator/local");
    } 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"));

    NumberFormat nf = NumberFormat.getInstance();
    nf.setMaximumFractionDigits(2);
    result = nf.format(cal.calculate(start, end, growthrate, saving));
  } 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>

</body>
</html>