7-6 jmu-Java-03面向对象-06-继承覆盖综合练习
写之前还是要先明确好程序编写的要求,以免过了样例但代码运行逻辑和要求不符合。
记录一个比较浮点型数据的方法
String df1 = new DecimalFormat("#.#").format(this.salary);
String df2 = new DecimalFormat("#.#").format(e.salary);
return this.company.toString().compareTo(e.company.toString()) == 0 && df1.compareTo(df2)==0;
import java.text.DecimalFormat;
import java.util.*;
abstract class Person implements Comparable<Person> {
String name;
int age;
boolean gender;
Person() {
}
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;
} //返回"name-age-gender"格式的字符串
public boolean equals(Person o1) {
if (this.name.equals(o1.name) && this.gender == o1.gender && this.age == o1.age) {
return true;
} else {
return false;
}
}//比较name、age、gender,都相同返回true,否则返回false
@Override
public int compareTo(Person o) {
if (this.name.compareTo(o.name) > 0) {
return 1;
} else if (this.name.compareTo(o.name) < 0) {
return -1;
} else {
return this.age - o.age;
}
}
}
class Student extends Person {
String stuNo;
String clazz;
Student() {
super();
}
Student(String name, int age, boolean gender, String stuNo, String clazz) {
super(name, age, gender);
this.stuNo = stuNo;
this.clazz = clazz;
}
@Override
public String toString() {
return this.name + "-" + this.age + "-" + this.gender + "-" + this.stuNo + "-" + this.clazz;
}
public boolean equals(Student o2) {
if (super.equals(o2) && this.stuNo.equals(o2.stuNo) && this.clazz.equals(o2.clazz)) {
return true;
} else {
return false;
}
}
}
class Company {
String name;
Company(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
} //直接返回name
public boolean equals(Person o1) {
if (o1.name == this.name) {
return true;
} else {
return false;
}
}
}
class Employee extends Person {
Company company;
double salary;
//建议使用super复用Person类的相关有参构造函数
Employee(String name, int age, boolean gender, double salary, Company company) {
super(name, age, gender);
this.company = company;
this.salary = salary;
}
@Override
public String toString() {
return this.name + "-" + this.age + "-" + this.gender + "-" + this.company + "-" + this.salary;
}
public boolean equals(Employee e) {
if (super.equals(e) && this.company.name.equals(e.company.name)) {
String df1 = new DecimalFormat("#.#").format(this.salary);
String df2 = new DecimalFormat("#.#").format(e.salary);
return this.company.toString().compareTo(e.company.toString()) == 0 && df1.compareTo(df2) == 0;
} else {
return false;
}
}
}
class Main {
public static void main(String[] args) {
List<Person> personList = new ArrayList<>();
List<Student> stuList = new ArrayList<>();
List<Employee> empList = new ArrayList<>();
Scanner sc = new Scanner(System.in);
String op;
while (true) {
op = sc.next();
//System.out.println(op);
if (op.equals("s")) {
String name;
int age;
boolean gender;
String stuNo;
String clazz;
name = sc.next();
age = sc.nextInt();
gender = sc.nextBoolean();
stuNo = sc.next();
clazz = sc.next();
if (name.equals("null") || stuNo.equals("null")) {
continue;
} else {
Student st = new Student(name, age, gender, stuNo, clazz);
personList.add(st);
}
} else if (op.equals("e")) {
String name;
int age;
boolean gender;
String company;
double salary;
name = sc.next();
age = sc.nextInt();
gender = sc.nextBoolean();
salary = sc.nextDouble();
company = sc.next();
if (name.equals("null")) {
continue;
} else {
Company cm = new Company(company);
Employee em = new Employee(name, age, gender, salary, cm);
personList.add(em);
}
} else {
Collections.sort(personList);
for (Person person : personList) {
if (person instanceof Student) {
Student student = (Student) person;
int f = 1;
for (Student stu : stuList) {
if (stu.equals(student)) {
f = 0;
break;
}
}
if (f == 1) {
stuList.add(student);
}
System.out.println("Student:" + student);
} else if (person instanceof Employee) {
Employee employee = (Employee) person;
int f = 1;
for (Employee emp : empList) {
if (emp.equals(employee)) {
f = 0;
break;
}
}
if (f == 1) {
empList.add(employee);
}
System.out.println("Employee:" + employee);
}
}
op = sc.next();
if (op.equals("exit")) {
break;
} else if (op.equals("return")) {
break;
}
System.out.println("stuList");
for (Student student : stuList) {
System.out.println("Student:" + student);
}
System.out.println("empList");
for (Employee employee : empList) {
System.out.println("Employee:" + employee);
}
}
}
}
}
版权声明:本文为Bookerbobo原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。