I am doing an exercise for a Java course and I am stuck with an exercise of making a payroll for employees, said data that is requested is name, surname, id, salary, these are loaded into an arraylist as an object in the secondary class of Employees.
main class code
package Nomina;
import java.util.ArrayList;
import java.util.Scanner;
public class Nominas {
private static Empleados eghp;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("*****TABACOS EL GRAN HUMO*****");
for (int i = 0; i < 2; i++){
System.out.print("Ingrese nombre: ");
String nombre = sc.next().toUpperCase();
System.out.print("Ingrese apellido: ");
String apellido = sc.next().toUpperCase();
System.out.print("Ingrese cedula: ");
int cedula = sc.nextInt();
System.out.print("Ingrese Salario: ");
double salario = sc.nextDouble();
eghp = new Empleados(nombre, apellido, cedula, salario);
//listaEmpleados.add(eghp);
eghp.addEmpleado(nombre, apellido, cedula, salario);
//eghp.imprimirEmpleados();
/*System.out.println();
System.out.print("-------------------------------------------");
System.out.println();
System.out.println("Nombre: " + eghp.getNombres() + " " + eghp.getApellidos());
System.out.println("Cédula de ciudadanía: " + eghp.getCedulas());
System.out.println("INGRESOS");
System.out.println("Salario: " + eghp.AnalizadorSalario());
eghp.opeNomina();*/
eghp.imprimirEmpleados();
}
eghp.imprimirEmpleados();
}
}
The exercise must meet a series of conditions which have already been carried out, the program at the end asks to show an arraylist with the objects that are added, that is (names, surnames, ID, salary) of each registered worker.
Attached the code of the second class
package Nomina;
import java.util.ArrayList;
public class Empleados {
private String nombres;
private String apellidos;
private int cedulas;
private double salarios;
private ArrayList <Empleados> listaEmpleados;
public Empleados(String nombres, String apellidos, int cedulas, double salarios) {
this.nombres = nombres;
this.apellidos = apellidos;
this.cedulas = cedulas;
this.salarios = salarios;
this.listaEmpleados = new ArrayList<>();
}
public void setSalario(double salarios){
this.salarios = salarios;
}
public String getNombres() {
return nombres;
}
public String getApellidos() {
return apellidos;
}
public int getCedulas() {
return cedulas;
}
public double getSalarios() {
return salarios;
}
public double AnalizadorSalario(){
double MIN = 908526;
double sal = this.salarios;
if (sal < MIN){
sal = MIN;
}
return sal;
}
public void opeNomina(){
double MIN = 908526;
double sal = this.AnalizadorSalario();
double aux = 0;
double desSalyPen = 0;
double desSolPen = 0;
double total = 0;
if (sal >= MIN && sal <= MIN*2){
aux =+ 106454;
desSalyPen = sal * 0.04;
total = sal + aux - desSalyPen - desSalyPen;
System.out.println("Auxilio de transporte: " + aux);
System.out.println("DESCUENTOS");
System.out.println("Aportes a salud: " + desSalyPen);
System.out.println("Aportes a pensión: " + desSalyPen);
System.out.println("TOTAL A PAGAR");
System.out.println(total);
}
else if (sal > MIN*2 && sal < MIN*4){
desSalyPen = sal * 0.04;
total = sal - desSalyPen - desSalyPen;
System.out.println("DESCUENTOS");
System.out.println("Aportes a salud: " + desSalyPen);
System.out.println("Aportes a pensión: " + desSalyPen);
System.out.println("TOTAL A PAGAR");
System.out.println(total);
}
else{
desSalyPen = sal * 0.04;
desSolPen = sal * 0.01;
total = sal - desSalyPen - desSalyPen - desSolPen;
System.out.println("DESCUENTOS");
System.out.println("Aportes a salud: " + desSalyPen);
System.out.println("Aportes a pensión: " + desSalyPen);
System.out.println("Fondo de solidaridad pensional: " + desSolPen);
System.out.println("TOTAL A PAGAR");
System.out.println(total);
}
}
public void addEmpleado(String nombres, String apellidos, int cedulas, double salarios){
Empleados c = new Empleados(nombres, apellidos, cedulas, salarios);
this.listaEmpleados.add(c);
}
public void imprimirEmpleados(){
for(Empleados empleado : this.listaEmpleados){
System.out.println();
System.out.print("-------------------------------------------");
System.out.println();
System.out.println("Nombre: " + getNombres() + " " + getApellidos());
System.out.println("Cédula de ciudadanía: " + getCedulas());
System.out.println("INGRESOS");
System.out.println("Salario: " + AnalizadorSalario());
opeNomina();
}
}
}
However, at the time of making the final sample, the program only gives me the data of the last person to register, I appreciate if you can help me