java—从对象中查找最大值

iyzzxitl  于 2021-06-27  发布在  Java
关注(0)|答案(2)|浏览(397)

这个问题在这里已经有答案了

从对象的arraylist获取最大值(5个答案)
六天前关门了。
我有一个扩展到2个辅助类(雇员和学生)的super class person,在输入雇员信息(姓名、ssn、薪水、性别)后,我想找到薪水最高的雇员并键入他/她的信息,但我不知道如何使用对象!,如果你能给我一个提示,我会很感激的。

public abstract class Person {
    private String name=" ";
    private String gender=" ";
    private long SSN;

    public Person() {
    }

    public Person(String name, String gender, long SSN) {
        this.name = name;
        this.gender = gender;
        this.SSN = SSN;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public long getSSN() {
        return SSN;
    }

    public void setSSN(long SSN) {
        this.SSN = SSN;
    }

    @Override
    public String toString() {
        return  name + "  " + gender + " " + SSN+ " ";
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Person other = (Person) obj;
        if (this.SSN != other.SSN) {
            return false;
        }
        if (!Objects.equals(this.gender, other.gender)) {
            return false;
        }
        return true;
    }

}

public class Employee extends Person {
    private String type = " ";
    private double salary;

    public Employee() {

    }

    public Employee(double salary, String name, String gender, long SSN) {
        super(name, gender, SSN);
        this.salary = salary;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    @Override
    public String toString() {
        return super.toString()+ " " + type + " " + salary ;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Employee other = (Employee) obj;
        if (Double.doubleToLongBits(this.salary) != Double.doubleToLongBits(other.salary)) {
            return false;
        }
        if (!Objects.equals(this.type, other.type)) {
            return false;
        }
        return true;
    }

}

public class Test {
    static void print(Person[] all, int count){
        System.out.println("The allPersons array contains: ");
        for (int i = 0; i <count; i++) {
            System.out.println(all[i]);
        }

    }
    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    Person[] allPersons = new Person[5];
    String name = " ";
    long ssn=0;
    String gender = " ";
    int count = 0;
    boolean s = true;
   while(s){
       System.out.println("Choose 1 to insert a new student");
       System.out.println("Choose 2 to insert new employee");
       System.out.println("Choose 3 to retrieve the maximum salary");
       System.out.println("Choose 4 to retrieve all software engineering students");
       System.out.println("Choose 0 to exit");
       System.out.println("Please enter your choice: ");
       int n = input.nextInt();
       switch(n){
           case 1:{ if(count == 5){System.out.println("Sorry, you reach the maximum length."); break;}
               Student student = new Student();
               System.out.println("Please enter Name: ");
               name=input.next();
               input.nextLine();
               student.setName(name);
               System.out.println("Please enter SSN: ");
               ssn = input.nextLong();
               student.setSSN(ssn);
               System.out.println("Plase enter Gender: " );
               gender = input.next();
               student.setGender(gender);
               System.out.println("Please enter major: ");
               String major = input.next();
               input.nextLine();
               student.setMajor(major);
               System.out.println("Plase inter Year of Regestration: ");
               int year = input.nextInt();
               student.setYearOfReg(year);
               System.out.println("Please enter Studend ID: ");
               long ID = input.nextLong();
               student.setID(ID);
               allPersons[count]=student;
               count++;
               print(allPersons,count);}
           case 2 :{if (count==5) {System.out.println("Sorry, you reach the maximum length"); break;}
            Employee emp = new Employee();
               System.out.println("Please enter Name:");
               name=input.next();
               input.nextLine();
               emp.setName(name);
               System.out.println("Plese enter SSN:");
               ssn=input.nextLong();
               emp.setSSN(ssn);
               System.out.println("Please enter Gender:");
               gender=input.next();
               emp.setGender(gender);
               System.out.println("Plese enter type: ");
               String type = input.next();
               input.nextLine();
               emp.setType(type);
               System.out.println("Please enter Salary:");
               double salary = input.nextDouble();
               emp.setSalary(salary);
               allPersons[count]=emp;
               count++;
               print(allPersons,count);

           }
           case 3: //Employee with max salary
           case 0: System.out.println("Exit");s=false;break; 

       }         
   }
    }

}
c0vxltue

c0vxltue1#

您可以在人员数组中循环查找薪资最高的员工。

Employee highest = null;
for(int i = 0; i < count; i++){
   if(allPersons[i] instanceof Employee && (highest == null || ((Employee) allPersons[i]).getSalary() > highest.getSalary())){
       highest = (Employee) allPersons[i];
   }
}
System.out.println(highest);
ruyhziif

ruyhziif2#

您可以使用lambda函数来获取最大工资,并检查同一函数中的人员类型。

public Employee getMaxSalary(Person[] allPersons) {
    return Arrays.asList(allPersons)
            .stream()
            .filter(Employee.class::isInstance) //Filter only the person who are Employee
            .map(Employee.class::cast)
            .max(Comparator.comparing(Employee::getSalary)) //Obtain only the max salary
            .orElseThrow(NoSuchElementException::new);
}

相关问题