Java 从对象中查找最大值
我有一个扩展到2个辅助类(员工和学生)的超级类Person,在输入员工信息(姓名、SSN、工资、性别)后,我想找到工资最高的员工并键入他/她的信息,但我不知道如何使用对象!,如果你能给我一个提示,我会很感激的Java 从对象中查找最大值,java,Java,我有一个扩展到2个辅助类(员工和学生)的超级类Person,在输入员工信息(姓名、SSN、工资、性别)后,我想找到工资最高的员工并键入他/她的信息,但我不知道如何使用对象!,如果你能给我一个提示,我会很感激的 public abstract class Person { private String name=" "; private String gender=" "; private long SSN; public
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;
}
}
}
}
公共抽象类人物{
私有字符串名称=”;
私人字符串性别=”;
私人长SSN;
公众人士(){
}
公众人物(字符串名称、字符串性别、长SSN){
this.name=名称;
这个。性别=性别;
这个.SSN=SSN;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getGender(){
返回性别;
}
公共无效设置性别(字符串性别){
这个。性别=性别;
}
公共长getSSN(){
返回SSN;
}
公共无效设置序列号(长序列号){
这个.SSN=SSN;
}
@凌驾
公共字符串toString(){
返回名称+“”+性别+“”+SSN+“”;
}
@凌驾
公共布尔等于(对象obj){
if(this==obj){
返回true;
}
if(obj==null){
返回false;
}
如果(getClass()!=obj.getClass()){
返回false;
}
最终人员其他=(人员)obj;
如果(this.SSN!=其他.SSN){
返回false;
}
如果(!Objects.equals(this.gender,other.gender)){
返回false;
}
返回true;
}
}
公共类雇员扩展个人{
私有字符串类型=”;
私人双薪;
公职人员(){
}
公共雇员(双薪、字符串名称、字符串性别、长SSN){
超级(姓名、性别、SSN);
这个。薪水=薪水;
}
公共字符串getType(){
返回类型;
}
公共void集合类型(字符串类型){
this.type=type;
}
公共双薪制{
返回工资;
}
公共收入(双倍工资){
这个。薪水=薪水;
}
@凌驾
公共字符串toString(){
返回super.toString()+“”+类型+“”+薪资;
}
@凌驾
公共布尔等于(对象obj){
if(this==obj){
返回true;
}
if(obj==null){
返回false;
}
如果(getClass()!=obj.getClass()){
返回false;
}
最终员工其他=(员工)obj;
if(Double.doubleToLongBits(this.salary)!=Double.doubleToLongBits(other.salary)){
返回false;
}
如果(!Objects.equals(this.type,other.type)){
返回false;
}
返回true;
}
}
公开课考试{
静态无效打印(人员[]全部,整数计数){
System.out.println(“allPersons数组包含:”);
对于(int i=0;i,您可以在人员数组中循环查找工资最高的员工
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);
Employee highest=null;
for(int i=0;i最高.getSalary()){
最高=(雇员)所有人[i];
}
}
系统输出打印项次(最高);
您可以使用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);
}
这是否回答了您的问题?将Person子类化的问题是,您可能有一名员工也是一名学生。