这个问题在这里已经有答案了:
从对象的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;
}
}
}
}
2条答案
按热度按时间c0vxltue1#
您可以在人员数组中循环查找薪资最高的员工。
ruyhziif2#
您可以使用lambda函数来获取最大工资,并检查同一函数中的人员类型。