calculator.jsp
001 <%page contentType="text/html; charset=Shift_JIS" %>
002 <%page import="javax.naming.*,
003                  java.text.*,
004                  java.util.*,
005                  trail.entity.beans.*,
006                  trail.entity.basic.*"%>
007 
008 <%!
009   private Calculator cal = null;
010   private NumberFormat nf = null;
011 
012   public void jspInit () {
013     try {
014       InitialContext ctx = new InitialContext();
015       cal = (Calculatorctx.lookup(
016                   "EJB3Trail/EntityCalculator/local");
017     catch (Exception e) {
018       e.printStackTrace ();
019     }
020 
021     nf = NumberFormat.getInstance();
022     nf.setMaximumFractionDigits(2);
023   }
024 %>
025 
026 <%
027     String result = "Not Valid";
028     if ("Calculate".equals(request.getParameter("action"))) {
029         double res = -1;
030         res = cal.calculate (Integer.parseInt(request.getParameter("fund")),
031                              Integer.parseInt(request.getParameter("investor")),
032                              Double.parseDouble(request.getParameter("saving")));
033         if (res != -1) {
034             result = nf.format(res);
035         }
036     }
037 %>
038 
039 <html><body>
040 
041 <p>投資計算プログラム<br/>
042 <form action="calculator.jsp" method="POST">
043   投資会社:
044   <select name="fund">
045 <%
046   Collection funds = cal.getFunds ();
047   for (Iterator iter = funds.iterator(); iter.hasNext();) {
048     Fund fund = (Funditer.next();
049 %>
050     <option value="<%=fund.getId()%>"><%=fund.getName()%></option>
051 <%
052   }
053 %>
054   </select>
055   と個人投資家:
056   <select name="investor">
057  を選択する
058 <%
059   Collection investors = cal.getInvestors ();
060   for (Iterator iter = investors.iterator(); iter.hasNext();) {
061     Investor investor = (Investoriter.next();
062 %>
063     <option value="<%=investor.getId()%>"><%=investor.getName()%></option>
064 <%
065   }
066 %>
067   </select><br/>
068   月掛金額 = <input type="text" name="saving" value="100">
069   <input type="hidden" name="action" value="Calculate">
070   <input type="submit" value="計算">
071   <INPUT type="button" value="閉じる" onClick="window.close()">
072 </form><br/>
073 合計投資額は<%=result%>です<br/>
074 <br/>
075 データベース内の過去の計算記録<br/>
076 <table>
077 <tr>
078 <td>タイムスタンプ</td>
079 <td>投資会社</td>
080 <td>個人投資家</td>
081 <td>月掛金額</td>
082 <td><b>合計投資額</b></td>
083 </tr>
084 
085 <%
086     Collection records = cal.getRecords ();
087     for (Iterator iter = records.iterator(); iter.hasNext();) {
088         TimedRecord record = (TimedRecorditer.next();
089 %>
090 
091 <tr>
092 <td><%=record.getTs()%></td>
093 <td><%=record.getFund().getName()%></td>
094 <td><%=record.getInvestor().getName()%></td>
095 <td><%=nf.format(record.getSaving())%></td>
096 <td><%=nf.format(record.getResult())%></td>
097 </tr>
098 
099 <%
100     }
101 %>
102 </table>
103 
104 </p>
105 
106 </body></html>