Person defined abstract class, Student category, Company category, Employee class.
Person class attributes : String name, int age, boolean gender
Person class methods :
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 class inherits from Person
the property: String stuNo, String clazz
methods of the Student class:
//建议使用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 class attributes: String name
Company class method:
public Company(String name); public String toString(); //直接返回name public boolean equals(Object obj);//name相同返回true
Employee class inherits from Person
the property: Company company, double salary
Employee class methods:
//建议使用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位小数
Write equals method IMPORTANT:
- Comparison of the Employee of company property. To be considered for the incoming
null
cases. If the company is not null and passed in is null, returns false - String comparison for all types of characters, but also consider
null
the situation.
prompt
- Sort using Collections.sort
- equals method to be thoughtful
main Method Description
-
Student create several objects, Employee object.
Enters
, then enter in turnname age gender stuNo clazz
create a Student object .
Entere
, then enter in turnname age gender salary company
create an Employee object .
Then the created object intoList<Person> personList
. Enter the other characters, the end of creation.
Creating Note: For String type, ifnull
the object is not created, and assignednull
. For company property, or null if assignednull
, otherwise the Company to create the appropriate object. -
The elements of personList achieved first by name in ascending order, the same name and then sorted in ascending order of age . Tip: You can use
Comparable<Person>
orComparator<Person>
-
Accept input, if the input is
exit
thenreturn
exit the program. Otherwise, continue with the following steps. -
The personList the elements are placed according to the type and stuList empList. Note: Do not use the same object content into two lists (whether the same is based on the results of determination equals returns).
-
Output string
stuList
, and then outputs stuList each object. -
Output string
empList
, and then outputs empList each object.
1-3
As a test point 4-6
to a test point
Sample input:
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
Sample output:
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.ArrayList; import java.util.Comparator; import java.util.List; import java.util.Scanner; class Person{ private String name = null; private int age = 0; private boolean gender = false; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public boolean isGender() { return gender; } public void setGender(boolean gender) { this.gender = gender; } public Person(String n, int a, boolean g) { this.name = n; this.age = a; this.gender = g; } @Override public String toString() { return name+"-"+age+"-"+gender ; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Person other = (Person) obj; if (age != other.age) return false; if (gender != other.gender) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } } class Student extends Person{ String stuNo; String clazz; public Student(String n, int a, boolean g,String stuNo, String clazz) { super(n, a, g); this.stuNo=stuNo; this.clazz=clazz; // TODO Auto-generated constructor stub } @Override public String toString() { return super.toString()+"-"+stuNo+"-"+clazz ; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!super.equals(obj)) return false; if (getClass() != obj.getClass()) return false; Student other = (Student) obj; if (clazz == null) { if (other.clazz != null) return false; } else if (!clazz.equals(other.clazz)) return false; if (stuNo == null) { if (other.stuNo != null) return false; } else if (!stuNo.equals(other.stuNo)) return false; return true; } } class Company{ 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) return false; if (getClass() != obj.getClass()) return false; Company other = (Company) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } } class Employee extends Person{ int age; boolean gender; double salary; Company company; public Employee(String n, int age, boolean gender, double salary, Company company) { super(n, age, gender); this.age = age; this.gender = gender; this.salary = salary; this.company = company; } @Override public String toString() { return super.toString()+"-"+company+"-"+salary; } DecimalFormat df = new DecimalFormat("#.#"); @Override public boolean equals(Object obj) { if(super.equals(obj)==true) { Employee other = (Employee)obj; if(this.company.toString()==null||other.company.toString()==null) { return false; } String df1 = new DecimalFormat("#.#").format(this.salary); String df2 = new DecimalFormat("#.#").format(other.salary); if(this.company.toString().compareTo(other.company.toString())==0&&df1.compareTo(df2)==0) { return true; } } return false; } } public class Main{ public static void main(String[] args) { String name; int age; boolean gender; String stuNo; String clazz; double salary; String company; Scanner sc=new Scanner(System.in); List<Person> personList=new ArrayList <>(); ArrayList<Student> students = new ArrayList<Student>(); ArrayList<Employee> employees = new ArrayList<Employee>(); while(true) { String c =sc.next(); if(c.compareTo("s")==0) { name=sc.next(); age=sc.nextInt(); gender=sc.nextBoolean(); stuNo=sc.next(); clazz=sc.next(); Person stu=new Student(name ,age ,gender ,stuNo ,clazz); personList.add(stu); }else if(c.compareTo("e")==0) { name=sc.next(); age=sc.nextInt(); gender=sc.nextBoolean(); salary = sc.nextDouble(); company = sc.next(); Company company2 = new Company(company); Person emp=new Employee(name, age, gender, salary, company2); personList.add(emp); }else { personList.sort(Comparator.comparing(Person::getName).thenComparing(Person::getAge)); for (int i=0;i<personList.size();i++) { if (personList.get(i) instanceof Student) { System.out.println("Student:"+personList.get(i).toString()); int flag = 0; for(int j=0;j<students.size();j++) { if(students.get(j).equals(personList.get(i))) { flag=1; break; } } if(flag == 0) { students.add((Student) personList.get(i)); } }else { System.out.println("Employee:"+personList.get(i).toString()); int flag = 0; for(int j=0;j<employees.size();j++) { if(employees.get(j).equals(personList.get(i))) { flag = 1; break; } } if(flag == 0) { employees.add((Employee)personList.get(i)); } } } String tempString = sc.next(); if(tempString.compareTo("exit")==0||tempString.compareTo("return")==0) { return; } System.out.println("stuList"); for(int i=0;i<students.size();i++) { System.out.println("Student:"+students.get(i).toString()); } System.out.println("empList"); for(int i=0;i<employees.size();i++) { System.out.println("Employee:"+employees.get(i).toString()); } } } } }