How can I get the average salary of all employees in the selected company? I first select the company and then pass the id and based on that id, i get all employees in there and display their info in a table. The goal is to get the average salary of everyone in this group.
<%@page import="data.Employee"%>
<%@page import="data.Company"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<jsp:useBean id="company" type="data.Company" scope="request"/>
<% List<Employee> employees = company.getEmployees();
double sum=0.0;
%>
<table border="1">
<tr>
<td>ID</td>
<td>Name</td>
<td>Salary</td>
</tr>
<%
for(int i=0; i <employees.size(); i++){
sum += employees.get(i).getSalary();
%>
<tr>
<td><%=employees.get(i).getId()%></td>
<td><%=employees.get(i).getNom()%></td>
<td><%=employees.get(i).getSalary()%></td>
</tr>
<% } %>
</table>
//get the average salary of all employees ::: This is working based on JChris's answer
<p>Average salary of all employees in this company:<%=sum/(double)employees.size()%> </p>
//this is returning zero.
<p> New average method: <%=company.getAverageSalary()%></p>