Hasta donde tengo entendido sobre lo que deseas hacer, lo haría así:
Claro que está demás decir que tienes que manejar las excepciones que pueden ocurrir (NumberFormatException, InputMismatchException).
Código (java) [Seleccionar]
package com.company.app.model.entities;
public class Employee {
private Short id;
private String names;
private String surnames;
private Double salary;
public Employee() {}
public Employee(Short id, String names, String surnames, Double salary) {
super();
this.id = id;
this.names = names;
this.surnames = surnames;
this.salary = salary;
}
public Short getId() {
return id;
}
public void setId(Short id) {
this.id = id;
}
public String getNames() {
return names;
}
public void setNames(String names) {
this.names = names;
}
public String getSurnames() {
return surnames;
}
public void setSurnames(String surnames) {
this.surnames = surnames;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
}
Código (java) [Seleccionar]
package com.company.app.model.entities;
import java.util.ArrayList;
public class EmployeeList extends ArrayList<Employee> {
private static final long serialVersionUID = 2924861284507271931L;
public EmployeeList() {
super();
}
public boolean hasRepeatProperties(Employee employee) {
boolean hasRepeat = false;
for(Employee e : this) {
if(e.equals(employee))
continue;
if(compareProperties(e.getId(), employee.getId())) {
hasRepeat = true;
break;
}
else if(compareProperties(e.getNames(), employee.getNames())) {
hasRepeat = true;
break;
}
else if(compareProperties(e.getSurnames(), employee.getSurnames())) {
hasRepeat = true;
break;
}
else if(compareProperties(e.getSalary(), employee.getSalary())) {
hasRepeat = true;
break;
}
}
return hasRepeat;
}
private boolean compareProperties(Object first, Object second) {
boolean hasRepeat = false;
if(first instanceof String) {
hasRepeat = first.equals(second);
}
else if (first instanceof Short){
hasRepeat = ((Short) first).shortValue() == ((Short) second).shortValue();
}
else if (first instanceof Double) {
hasRepeat = ((Double) first).doubleValue() == ((Double) second).doubleValue();
}
return hasRepeat;
}
}
Código (java) [Seleccionar]
package com.company.app;
import java.util.Scanner;
import com.company.app.model.entities.Employee;
import com.company.app.model.entities.EmployeeList;
public class Main {
public static void main(String[] args) {
EmployeeList employeeList = new EmployeeList();
Scanner reader = new Scanner(System.in);
String option = "S";
do {
Employee employee = new Employee();
System.out.println("Ingrese el ID del nuevo empleado");
employee.setId(Short.valueOf(reader.nextLine()));
System.out.println("\nIngrese sus nombres:");
employee.setNames(reader.nextLine());
System.out.println("\nIngrese sus apellidos");
employee.setSurnames(reader.nextLine());
System.out.println("Ingrese su salario");
employee.setSalary(Double.valueOf(reader.nextLine()));
employeeList.add(employee);
System.out.println("\n¿Desea seguir agregando empleados? S/N");
option = reader.nextLine();
}
while(option.equalsIgnoreCase("s"));
reader.close();
for(Employee employee : employeeList) {
if(employeeList.hasRepeatProperties(employee))
continue;
System.out.println("ID del empleado: "+employee.getId());
System.out.println("Nombres: "+employee.getNames());
System.out.println("Apellidos: "+employee.getSurnames());
System.out.println("Salario: "+employee.getSalary());
}
}
}
Claro que está demás decir que tienes que manejar las excepciones que pueden ocurrir (NumberFormatException, InputMismatchException).