当前位置: 首页 > 知识库问答 >
问题:

从对象中查找最大值[重复]

仲孙鸿畴
2023-03-14

我有super class Person扩展到2个sup class(Employee和Student),在我输入员工信息(姓名、SSN、工资、Gendr)后,我想找到最大工资的员工并键入他的信息,但我不知道如何用对象来做!,如果你可以给我一个提示,我将非常感谢。

 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; 
               
       }         
   }
    }
    
}

共有1个答案

上官高翰
2023-03-14

您可以遍历人员数组并查找薪资最高的员工。

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);
 类似资料:
  • 我使用查找列表中的最大值,但是下面的代码返回,尽管最大值是。

  • 我试图找到矩阵中每列的最小值和最大值,但我当前的代码运行不正确。我试图把最小值放在一个新矩阵的第一行,最大值放在下一行,并对每一列这样做。任何帮助都将不胜感激,谢谢!

  • 问题内容: 我正在寻找一种非常快速,干净且有效的方法来获取以下JSON切片中的最大“ y”值: for-loop是解决此问题的唯一方法吗?我热衷于使用。 问题答案: 要在中找到对象的最大值:

  • 所以我有了这个产品对象的ArrayList。我所做的是在点击+按钮,我正在制作一个产品的对象,并从UI设置所有的属性;包括'Quantity'并将此对象添加到ArrayList中。现在,当我在单击+按钮时添加一个对象时,我得到了具有所有相同属性的重复产品对象,当然,除了“数量”计数。如果我添加一个数量为4的产品,我会在arraylist中得到4个不同数量为1、2、3和4的产品对象。我只想有最大数量

  • 我必须用随机值填充三个数组(完成),从每个数组中找到最大值(完成),然后在指定的三个数组中找到最大值((我不知道怎么做,有人能帮我吗?))。 但是,主要的问题是代码重复,我试了几种方法来做,但是失败了,有人能帮我吗?