7-5 jmu-Java-03物件導向-06-繼承覆蓋綜合練習-Person、Student、Employee、Company JAVA

hidewindzz發表於2020-11-13

定義Person抽象類,Student類、Company類,Employee類。**

Person類的屬性: String name, int age, boolean gender
Person類的方法:
public Person(String name, int age, boolean gender);
public String toString(); //返回"name-age-gender"格式的字串
public boolean equals(Object obj);//比較name、age、gender,都相同返回true,否則返回false

Student類繼承自Person,屬性:String stuNo, String clazz
Student類的方法:
//建議使用super複用Person類的相關有參建構函式
public Student(String name, int age, boolean gender, String stuNo, String clazz);
public String toString(); //返回 “Student:person的toString-stuNo-clazz”格式的字串
public boolean equals(Object obj);//首先呼叫父類的equals方法,如果返回true,則繼續比較stuNo與clazz。

Company類屬性:String name
Company類方法:
public Company(String name);
public String toString(); //直接返回name
public boolean equals(Object obj);//name相同返回true

Employee類繼承自Person,屬性:Company company, double salary
Employee類方法:
//建議使用super複用Person類的相關有參建構函式
public Employee(String name, int age, boolean gender, double salary, Company company);
public String toString(); //返回"Employee:person的toString-company-salary"格式的字串
public boolean equals(Object obj);//首先呼叫父類的equals方法,如果返回true。再比較company與salary。
//比較salary屬性時,使用DecimalFormat df = new DecimalFormat("#.#");保留1位小數

編寫equals方法重要說明:

對Employee的company屬性的比較。要考慮傳入為null的情況。如果company不為null且傳入為null,返回false
對所有String字元型別比較時,也要考慮null情況。
提示:排序可使用Collections.sort;equals方法要考慮周全

main方法說明

建立若干Student物件、Employee物件。

輸入s,然後依次輸入name age gender stuNo clazz建立Student物件。
輸入e,然後依次輸入name age gender salary company建立Employee物件。
然後將建立好的物件放入List personList。輸入其他字元,則結束建立。

建立說明: 對於String型別,如果為null則不建立物件,而賦值為null。對於company屬性,如果為null則賦值為null,否則建立相應的Company物件。

對personList中的元素實現先按照姓名升序排序,姓名相同再按照年齡升序排序。提示:可使用Comparable或Comparator

接受輸入,如果輸入為exit則return退出程式,否則繼續下面步驟。

將personList中的元素按照型別分別放到stuList與empList。注意:不要將兩個內容相同的物件放入列表(是否相同是根據equals返回結果進行判定)。

輸出字串stuList,然後輸出stuList中的每個物件。
輸出字串empList,然後輸出empList中的每個物件。

輸入樣例:

s zhang 23 false 001 net15
e wang 18 true 3000.51 IBM
s zhang 23 false 001 net15
e bo 25 true 5000.51 IBM
e bo 25 true 5000.52 IBM
e bo 18 true 5000.54 IBM
e tan 25 true 5000.56 IBM
e tan 25 true 5000.51 IBM
s wang 17 false 002 null
s wang 17 false 002 null
e hua 16 false 1000 null
s wang 17 false 002 net16
e hua 16 false 1000 null
e hua 18 false 1234 MicroSoft
!
continue

輸出樣例:

Employee:bo-18-true-IBM-5000.54
Employee:bo-25-true-IBM-5000.51
Employee:bo-25-true-IBM-5000.52
Employee:hua-16-false-null-1000.0
Employee:hua-16-false-null-1000.0
Employee:hua-18-false-MicroSoft-1234.0
Employee:tan-25-true-IBM-5000.56
Employee:tan-25-true-IBM-5000.51
Student:wang-17-false-002-null
Student:wang-17-false-002-null
Student:wang-17-false-002-net16
Employee:wang-18-true-IBM-3000.51
Student:zhang-23-false-001-net15
Student:zhang-23-false-001-net15
stuList
Student:wang-17-false-002-null
Student:wang-17-false-002-net16
Student:zhang-23-false-001-net15
empList
Employee:bo-18-true-IBM-5000.54
Employee:bo-25-true-IBM-5000.51
Employee:hua-16-false-null-1000.0
Employee:hua-18-false-MicroSoft-1234.0
Employee:tan-25-true-IBM-5000.56
Employee:tan-25-true-IBM-5000.51
Employee:wang-18-true-IBM-3000.51

程式碼實現:


import java.text.DecimalFormat;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        List<Person> personList = new ArrayList<>();
        List<Student> stuList = new ArrayList<>();
        List<Employee> empList = new ArrayList<>();
        boolean flag = true;
        while (true){
            String str = scan.nextLine();
            String[] strings = cut(str," ");
                if (!strings[0].equals("exit")){
                    if (flag){
                    switch (strings[0]){
                        case "s":
                            personList.add(new Student(strings[1],Integer.parseInt(strings[2]),Boolean.parseBoolean(strings[3]),strings[4],strings[5]));
                            break;
                        case "e":
                            personList.add(new Employee(strings[1],Integer.parseInt(strings[2]),Boolean.parseBoolean(strings[3]),
                            Double.parseDouble(strings[4]),strings[5] == "null"? null: new Company(strings[5])));
                            break;
                        default:
                            flag = false;
                            //排序
                            personList.sort(new Comparator<Person>() {
                                @Override
                                public int compare(Person o1, Person o2) {
                                    int sort = o1.name.compareTo(o2.name);
                                    if(sort == 0) return o1.age - o2.age;
                                    return sort;
                                }
                            });
                            //分裝
                            for (int i=0;i<personList.size();i++){
                                Person person = personList.get(i);
                                if (person instanceof Student) {
                                    if (stuList.indexOf(person) == -1) stuList.add((Student) person);
                                } else {
                                    if (empList.indexOf(person) == -1)  empList.add((Employee) person);
                                }
                            }
                            //輸出
                            for(int i=0;i<personList.size();i++){
                                System.out.println(personList.get(i));
                            }

                    }
                } else {
                        System.out.println("stuList");
                        for(Person p:stuList) System.out.println(p);
                        System.out.println("empList");
                        for(Person p:empList) System.out.println(p);
                    }
        }else return;
    }
}
    private static String[] cut(String str, String s) {
        String[]  strings = str.split(s);
        for (int i=0;i<strings.length;i++){
            if (strings[i].equals("null")) strings[i] = null;
        }
        return strings;
        }
    }

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

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

    @Override
    public String toString() {
        return this.name + "-" + this.age + "-" + this.gender;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        Person that = (Person) obj;
        return (this.name == that.name|| this.name != null && this.name.equals(that.name))
                && this.age == that.age && this.gender == that.gender;
    }
}

class Student extends Person{
    private String stuNo;
    private String clazz;

    public String getStuNo() {
        return stuNo;
    }

    public void setStuNo(String stuNo) {
        this.stuNo = stuNo;
    }

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

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

    @Override
    public String toString() {
        return "Student:" + super.toString() + "-" + this.getStuNo() + "-" + this.getClazz();
    }
    @Override
    public boolean equals(Object obj){
        if (super.equals(obj)){
            Student that = (Student) obj;
            if((that.stuNo == this.stuNo || that.stuNo != null && that.stuNo.equals(this.stuNo))
                    && (that.clazz == this.clazz || that.clazz != null && that.clazz.equals(this.clazz)))
                return true;
        }
        return false;
    }
}

class Company{
    public String name;

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

    @Override
    public String toString() {
        return name;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (obj == null || getClass() != obj.getClass()) return false;
        Company company = (Company) obj;
        if(company.name == this.name || company.name != null && company.name.equals(this.name))
            return true;

        return false;
    }
}

class Employee extends Person{
    private Company company;
    private double salary;

    public Company getCompany() {
        return company;
    }

    public void setCompany(Company company) {
        this.company = company;
    }

    public double getSalary() {
        return salary;
    }

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

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

    @Override
    public String toString() {
        return "Employee:" + super.toString() + "-" + this.getCompany() +"-" + this.getSalary();
    }

    @Override
    public boolean equals(Object obj) {
        if (super.equals(obj)){
            Employee that = (Employee)obj;
            DecimalFormat df = new DecimalFormat("#.#");
            return (this.company == that.company||this.company != null && this.company.equals(that.company))
                    && df.format(this.salary).equals(df.format(that.salary));
        }
        return false;
    }
}

相關文章