-2

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>
passion
  • 1,000
  • 6
  • 20
  • 47
  • With the anwer to your previous question, this question should by answered as well, or am I missing something? I mean, you have an instance of `Company` and could simply call `company.getAverageSalary()`. No need for navigating from an employee to its company. http://stackoverflow.com/a/42058263/7492402 – Philipp Merkle Feb 07 '17 at 12:59
  • @P.Merkle i have `company.getAverageSalary()` in my JSP, and it is returning 0.0 , however, the answer by JChrist is also on the same page, it is returning the correct answer. What is wrong? how should this be corrected? – passion Feb 07 '17 at 21:12
  • That is hard to say without knowing where your `company` object comes from. Did you load it via `CompanyDTO.getAll()`? – Philipp Merkle Feb 07 '17 at 21:22
  • @P.Merkle I updated my code, and you see what I import and what bean i use, and the JSP code. the code now includes the answer provided by JChrist, which works for what he suggested, but the second way i put in the last line doesn't return the same average, it returns zero. – passion Feb 07 '17 at 22:59

1 Answers1

1

You could sum the salaries up and then divide by the number of employees:

<% 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
<p>Average salary of all employees in this company:<%=sum/(double)employees.size()%> </p>

Having said that, I urge you to reconsider before going down that road. Having code inside your JSP will severely hinder your chances of maintaining it if it gets any bigger than 1-2 pages. All your business logic should happen in your code (e.g. inside your servlets) and in JSPs only display the information.

JChrist
  • 1,734
  • 17
  • 23
  • Agreed, a bean would be best. – Adrian M. Feb 05 '17 at 21:07
  • how can I do this in my Company.java and import it as a bean? could you please help? – passion Feb 05 '17 at 21:08
  • 1
    For JSP, you can take a look at Oracle's Java EE 5 JavaBeans Components: http://docs.oracle.com/javaee/5/tutorial/doc/bnair.html Although I would suggest moving over to Java Server Faces (JSF) https://javaserverfaces.java.net/ – JChrist Feb 05 '17 at 21:11
  • i am taking a look at this link, i am still a newbie and i would appreciate if you could give an answer with this method regarding my problem. thanks – passion Feb 05 '17 at 21:14