Reputation: 25
I have to make some methods to sort and filter the objects from an ArrayList
, and I'm turning crazy with that. I explain the methods I have to do in the ListaEmpleados
class.
public class Empleado implements Comparable<Empleado> {
private String nif;
private String nombre;
private Double sueldo;
public Empleado(String nif, String nombre, Double sueldo) {
this.nif = nif;
this.nombre = nombre;
this.sueldo = sueldo;
}
public String getNif() {
return nif;
}
public void setNif(String nif) {
this.nif = nif;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public Double getSueldo() {
return sueldo;
}
public void setSueldo(Double sueldo) {
this.sueldo = sueldo;
}
@Override
public String toString() {
return "Empleado{" + "nif=" + nif + ", nombre=" + nombre + ", sueldo=" +
sueldo + '}';
}
public int compareTo(Empleado t) {
return this.nif.compareTo(t.nif);
}
}
Here is where I have to construct the methods, some works but other I can't do it or don't know how to contruct them.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import javax.accessibility.AccessibleRole;
public class ListaEmpleados implements Comparator<Empleado> {
static List<Empleado> empleados = new ArrayList<>();
// Method to order the list by name. SEEMS it WORKS!
public static void ordenaPorNombre(List<Empleado> lista) {
Collections.sort(lista, (Empleado e1, Empleado e2) ->
e1.getNombre().compareTo(e2.getNombre()));
}
// Method to order by ascending salary (sueldo in spanish). SEEMS it WORKS!
public static void ordenaPorSueldo(List<Empleado> lista) {
Collections.sort(lista, (Empleado e1, Empleado e2) ->
e1.getSueldo().compareTo(e2.getSueldo()));
}
// In this one i have to return the employee whith hig salary. DONT WORKS
//return incorrect employee
// returns {nif=4433345Z, nombre=juan, sueldo=2000.0} and must be
//{nif=0403993X, nombre=zen, sueldo=4000.0}
public static Empleado retornaEmpleadoMayorSueldo(List<Empleado> lista) {
Empleado emp = Collections.max(lista);
return emp;
}
// This one return all employees with salary minus or equals to
// sueldoMaximo, I have to do it with stream.
// Dont works. ERRORS--- ") expected" --- "and boolean cannot be
// derefenced"
public List<Empleado> empleadosConSueldo(double sueldoMAximo, List<Empleado>
lista) {
Empleado employee = (Empleado) lista.stream().filter(e ->
e.getSueldo().equals(sueldoMAximo).get();
return (List<Empleado>) employee;
}
// this method return the average salary from the employees that name begins
//with the firstword passed by argument.
public double sueldoMedioEmpiezanPor(char firstword) {
//no idea how i can make it.
}
@Override
public int compare(Empleado e, Empleado e1) {
return e.getSueldo().compareTo(e.getSueldo());
}
}
And the main where I initialize the array and the employees (empleado in spanish)
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Unidad4P1 {
public static void main(String[] args) {
List<Empleado> empleados = new ArrayList();
Empleado emp1 = new Empleado("1459383D", "patxi", 1000.0);
Empleado emp2 = new Empleado("3403993X", "ana", 1500.0);
Empleado emp4 = new Empleado("0403993X", "zen", 4000.0);
Empleado emp3 = new Empleado("4433345Z", "juan", 2000.0);
empleados.add(emp1);
empleados.add(emp2);
empleados.add(emp4);
empleados.add(emp3);
Collections.sort(empleados);
System.out.println("Order by NIF:" + empleados);
System.out.println("-------------------------------------------");
ListaEmpleados.ordenaPorNombre(empleados);
System.out.println("order by NAME:" + empleados);
System.out.println("-------------------------------------------");
ListaEmpleados.ordenaPorSueldo(empleados);
System.out.println("Order by salary:" + empleados);
System.out.println("-------------------------------------------");
System.out.println("Employee Hig Salary is:" +
ListaEmpleados.retornaEmpleadoMayorSueldo(empleados));
System.out.println("-------------------------------------------");
}
}
Upvotes: 0
Views: 1339
Reputation: 25
Finally I solved the problem this way:
public static List<Empleado> empleadosConSueldo(double sueldoMaximo, List<Empleado> lista) {
List<Empleado> listaResultado;
listaResultado = lista.stream().filter(e -> e.getSueldo() >= (sueldoMaximo))
.collect(Collectors.toList());
return listaResultado;
}
public static double sueldoMedioEmpiezanPor(char letra, List<Empleado> lista) {
Double sueldoMedio = 0.0;
sueldoMedio = lista.stream().filter(e -> e.getNombre().charAt(0) == letra)
.mapToDouble(e -> e.getSueldo()).average().getAsDouble();
return sueldoMedio;
}
Upvotes: 1
Reputation: 498
The method "retornaEmpleadoMayorSueldo" don't work because Collections.max use "Comparable" to determine who is the max employee. Your class Empleado is returning "this.nif.compareTo(t.nif)" he are returning "Juan" because Juan has the max nif(4433345Z). You must use a "Comparator" like your method "ordenaPorSueldo". Like this:
public static Empleado retornaEmpleadoMayorSueldo(List<Empleado> lista) {
return Collections.max(lista, new Comparator<Empleado >() {
@Override
public int compare(Empleado e1, Empleado e2) {
return e1.getSueldo().compareTo(e2.getSueldo()));
}
});
}
The average salary is:
public double sueldoMedioEmpiezanPor(List<Empleado> employees, char firstword) {
double salarySum = 0.0;
int employeeCount = 0;
for (Empleado employee : employees ) {
if(firstword == employee.getNombre().charAt(0)){
salarySum += employee.getSueldo();
employeeCount++;
}
}
return salarySum / employeeCount;
}
The "empleadosConSueldo" is like above, you just compare "sueldo" instead of "nombre" and put all employee that has "Sueldo" minus or equals "sueldoMAximo" in another list to return. Other way is remove all employee that has salary greater than "sueldoMAximo" from the employees list. Try it yourself and come back if you have doubt.
Upvotes: 0