Sample JAVA Code: Formula Calculations

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"

      pageEncoding="ISO-8859-1"%>

<%

      final String PATH = "C:/Sun/AppServer/domains/domain1/applications/j2ee-modules/PSC42Samples/";

 

      String summary = new String("");

      String output1 = new String("");

      String output2 = new String("");

 

      String a1 = request.getParameter("DdlCurrent_Age");

      String a2 = request.getParameter("DdlDesired_Retirement_Age");

      String a3 = request.getParameter("TxtMonthly_savings");

      String a4 = request.getParameter("TxtAnnual_Investment_growth_rate");

      String a5 = request.getParameter("TxtAnnual_Salary_Increase_Rate");

     

      if(a1 != null)

      {

            try

            {

                  //Create an instance of the application

                  Application app = new Application();

 

                  //Open retirement workbook

                  app.getWorkbooks().open(PATH + "Spreadsheets/Retirement/Retirement.xls");

 

                  //Load workbook

                  Workbook wb = app.getWorkbooks().getItem(PATH + "Spreadsheets/Retirement/Retirement.xls");

 

                  //Set input values

                  wb.getNames().getItem("Current_Age").getRange().setValue(a1);

                  wb.getNames().getItem("Desired_Retirement_Age").getRange().setValue(a2);

                  wb.getNames().getItem("Monthly_savings").getRange().setValue(a3);

                  wb.getNames().getItem("Annual_Investment_growth_rate").getRange().setValue(a4);

                  wb.getNames().getItem("Annual_Salary_Increase_Rate").getRange().setValue(a5);

                 

                  //Display output

                  output1 = wb.getNames().getItem("Retirement_Nest_Egg").getRange().getValue().toString();

                  output2 = wb.getNames().getItem("duration").getRange().getValue().toString();

                 

                  wb.close();

 

            }

            catch(Exception e)

            {

                  summary = e.getMessage();

            }

      }

%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%@page import="com.pagos.Application"%>

<%@page import="com.pagos.Workbook"%>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<TITLE>RETIREMENT SAMPLE</TITLE>

<style type="text/css">

    TD, INPUT, SELECT

    {

            font-family: Arial;

            font-size: 10pt;

      }

     

      TABLE#HTInput, #HTOutput

      {

            border-top:solid 1px #C0C0C0;

            border-left:solid 1px #C0C0C0;

      }

     

      TABLE#HTInput TD, TABLE#HTOutput TD

      {

            border-bottom:solid 1px #C0C0C0;

            border-right:solid 1px #C0C0C0;

      }

     

      TABLE#HTOutput TD

      {

            background-color:#CCCCFF;

      }

    </style>

</head>

<body>

<form id="form1" action="Retirement.jsp" method="post">

<DIV>

<table border="0" cellpadding="0" cellspacing="0" width="600">

      <tr>

            <td style="width: 328px">Current Age</td>

            <td>

                  <select name="DdlCurrent_Age"  onChange="javascript:submit(form1);">

                        <%

                             for (int i = 1; i < 66; i++)

                             {

                                   out.write("<option ");

                                   if(i == (a1 == null ? 36 : Integer.parseInt(a1)))

                                         out.write(" selected ");

                                   out.write("value=\"");

                                   out.write(Integer.toString(i));

                                  

                                   out.write("\">");

                                   out.write(Integer.toString(i));

                                   out.write("</option>");

                             }

                        %>

                  </select>

            </td>

      </tr>

      <tr>

            <td style="width: 328px">Desired Retirement Age</td>

            <td>

                  <select name="DdlDesired_Retirement_Age" onChange="javascript:submit(form1);">

                        <%

                             for (int i = 45; i < 86; i++)

                             {

                                   out.write("<option ");                              

                                   if(i == (a2 == null ? 80 : Integer.parseInt(a2)))

                                         out.write(" selected ");

                                   out.write("value=\"");

                                   out.write(Integer.toString(i));

                                   out.write("\">");

                                   out.write(Integer.toString(i));

                                   out.write("</option>");

                             }

                        %>

                  </select>

            </td>

      </tr>

      <tr>

            <td style="width: 328px">Monthly savings</td>

            <td>

                  <INPUT type="text" Name="TxtMonthly_savings" value="<%out.write(a3 == null ? "1" : a3);%>"/>

            </td>

      </tr>

      <tr>

            <td style="width: 328px">Annual Investment growth rate (between

            0-1)</td>

            <td>

                  <INPUT type="text" Name="TxtAnnual_Investment_growth_rate" value="<%out.write(a4 == null ? "5" : a4);%>"/>           

            </td>

      </tr>

      <tr>

            <td style="width: 328px">Annual Salary Increase Rate (between

            0-1)</td>

            <td>

                  <INPUT type="text" Name="TxtAnnual_Salary_Increase_Rate" value="<%out.write(a5 == null ? "15" : a5);%>"/>          

            </td>      

      </tr>

      <tr>

            <td style="width: 328px">&nbsp;</td>

            <td>&nbsp;</td>

      </tr>

      <tr>

            <td style="width: 328px">&nbsp;

                  <%out.write(summary);%>

            <td>

            <INPUT type="submit" name="BtnCalculate" value="Calculate"/>

            </td>

      </tr>

</table>

<br />

<table border="0" cellpadding="0" cellspacing="0" width="600"

      id="HTOutput" runat="server" visible="false">

      <tr>

            <td style="width: 328px">Retirement Nest Egg in $</td>

            <td><%out.write(output1);%></td>

      </tr>

      <tr>

            <td style="width: 328px; height: 19px;">Duration (max 100 years)

            </td>

            <td style="height: 19px"><%out.write(output2);%></td>

      </tr>

</table>

 

</DIV>

</form>

</body>

</html>