import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Scanner;

abstract class Person implements Comparable { String name; int age; boolean gender;

public Person(String name, int age, boolean gender) {
    this.name = name;
    this.age = age;
    this.gender = gender;
}

public abstract String toString();

public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj instanceof Person) {
        Person person = (Person) obj;
        return this.name.equals(person.name) && this.age == person.age && this.gender == person.gender;
    }
    return false;
}

public int compareTo(Person person) {
    int result = this.name.compareTo(person.name);
    if (result == 0) {
        result = this.age - person.age;
    }
    return result;
}

}

class Student extends Person { String stuNo; String clazz;

public Student(String name, int age, boolean gender, String stuNo, String clazz) {
    super(name, age, gender);
    this.stuNo = stuNo;
    this.clazz = clazz;
}

public String toString() {
    return 'Student:' + super.toString() + '-' + stuNo + '-' + clazz;
}

public boolean equals(Object obj) {
    if (super.equals(obj)) {
        if (obj instanceof Student) {
            Student student = (Student) obj;
            return this.stuNo.equals(student.stuNo) && this.clazz.equals(student.clazz);
        }
    }
    return false;
}

}

class Company { String name;

public Company(String name) {
    this.name = name;
}

public String toString() {
    return name;
}

public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }
    if (obj instanceof Company) {
        Company company = (Company) obj;
        return this.name.equals(company.name);
    }
    return false;
}

}

class Employee extends Person { Company company; double salary;

public Employee(String name, int age, boolean gender, double salary, Company company) {
    super(name, age, gender);
    this.salary = salary;
    this.company = company;
}

public String toString() {
    DecimalFormat df = new DecimalFormat('#.#');
    return 'Employee:' + super.toString() + '-' + company + '-' + df.format(salary);
}

public boolean equals(Object obj) {
    if (super.equals(obj)) {
        if (obj instanceof Employee) {
            Employee employee = (Employee) obj;
            if (this.company == null && employee.company == null) {
                return true;
            }
            if (this.company != null && employee.company != null) {
                return this.company.equals(employee.company) && this.salary == employee.salary;
            }
        }
    }
    return false;
}

}

public class Main { public static void main(String[] args) { List personList = new ArrayList(); Scanner scanner = new Scanner(System.in);

    while (true) {
        String input = scanner.nextLine();
        if (input.equals('exit')) {
            return;
        } else if (input.equals('s')) {
            String name = scanner.next();
            int age = scanner.nextInt();
            boolean gender = scanner.nextBoolean();
            String stuNo = scanner.next();
            String clazz = scanner.next();
            scanner.nextLine();
            Student student = new Student(name, age, gender, stuNo, clazz);
            personList.add(student);
        } else if (input.equals('e')) {
            String name = scanner.next();
            int age = scanner.nextInt();
            boolean gender = scanner.nextBoolean();
            double salary = scanner.nextDouble();
            String companyName = scanner.next();
            scanner.nextLine();
            Company company = companyName.equals('null') ? null : new Company(companyName);
            Employee employee = new Employee(name, age, gender, salary, company);
            personList.add(employee);
        } else {
            break;
        }
    }

    Collections.sort(personList);

    List<Student> stuList = new ArrayList<Student>();
    List<Employee> empList = new ArrayList<Employee>();

    for (Person person : personList) {
        if (person instanceof Student) {
            if (!stuList.contains((Student) person)) {
                stuList.add((Student) person);
            }
        } else if (person instanceof Employee) {
            if (!empList.contains((Employee) person)) {
                empList.add((Employee) person);
            }
        }
    }

    System.out.println('stuList');
    for (Student student : stuList) {
        System.out.println(student.toString());
    }

    System.out.println('empList');
    for (Employee employee : empList) {
        System.out.println(employee.toString());
    }
}

}


原文地址: https://www.cveoy.top/t/topic/phGh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录