calculator.jsp
001 <%@ page import="javax.naming.*,
002                  java.text.*,
003                  java.util.*,
004                  trail.entity.beans.*,
005                  trail.entity.basic.*"%>
006 
007 <%!
008   private Calculator cal = null;
009   private NumberFormat nf = null;
010 
011   public void jspInit () {
012     try {
013       InitialContext ctx = new InitialContext();
014       cal = (Calculatorctx.lookup(
015                   "EJB3Trail/EntityCalculator/local");
016     catch (Exception e) {
017       e.printStackTrace ();
018     }
019 
020     nf = NumberFormat.getInstance();
021     nf.setMaximumFractionDigits(2);
022   }
023 %>
024 
025 <%
026     String result = "Not Valid";
027     if ("Calculate".equals(request.getParameter("action"))) {
028         double res = -1;
029         res = cal.calculate (Integer.parseInt(request.getParameter("fund")),
030                              Integer.parseInt(request.getParameter("investor")),
031                              Double.parseDouble(request.getParameter("saving")));
032         if (res != -1) {
033             result = nf.format(res);
034         }
035     }
036 %>
037 
038 <html><body>
039 
040 <p>The Investment calculator<br/>
041 <form action="calculator.jsp" method="POST">
042   Choose a fund :
043   <select name="fund">
044 <%
045   Collection funds = cal.getFunds ();
046   for (Iterator iter = funds.iterator(); iter.hasNext();) {
047     Fund fund = (Funditer.next();
048 %>
049     <option value="<%=fund.getId()%>"><%=fund.getName()%></option>
050 <%
051   }
052 %>
053   </select>
054   and an investor:
055   <select name="investor">
056 <%
057   Collection investors = cal.getInvestors ();
058   for (Iterator iter = investors.iterator(); iter.hasNext();) {
059     Investor investor = (Investoriter.next();
060 %>
061     <option value="<%=investor.getId()%>"><%=investor.getName()%></option>
062 <%
063   }
064 %>
065   </select><br/>
066   Monthly saving = <input type="text" name="saving" value="100">
067   <input type="hidden" name="action" value="Calculate">
068   <input type="submit" value="Calculate">
069   <INPUT type="button" value="Close Window" onClick="window.close()">
070 </form><br/>
071 The total investment is <%=result%><br/>
072 <br/>
073 All records from past calculations in the database<br/>
074 <table>
075 <tr>
076 <td>Time stamp</td>
077 <td>Fund</td>
078 <td>Investor</td>
079 <td>Monthly savings</td>
080 <td><b>Total investment</b></td>
081 </tr>
082 
083 <%
084     Collection records = cal.getRecords ();
085     for (Iterator iter = records.iterator(); iter.hasNext();) {
086         TimedRecord record = (TimedRecorditer.next();
087 %>
088 
089 <tr>
090 <td><%=record.getTs()%></td>
091 <td><%=record.getFund().getName()%></td>
092 <td><%=record.getInvestor().getName()%></td>
093 <td><%=nf.format(record.getSaving())%></td>
094 <td><%=nf.format(record.getResult())%></td>
095 </tr>
096 
097 <%
098     }
099 %>
100 </table>
101 
102 </p>
103 
104 </body></html>