Employee.java 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package exericse.exericse02;
  2. import java.util.Objects;
  3. /**
  4. * @author WanJl
  5. * @version 1.0
  6. * @title Employee
  7. * @description
  8. * @create 2026/8/3
  9. */
  10. public class Employee implements Comparable<Employee> {
  11. private String id; // 工号
  12. private String name; // 姓名
  13. private double salary; // 薪资
  14. private String dept; // 部门
  15. public Employee(String id, String name, double salary, String dept) {
  16. this.id = id;
  17. this.name = name;
  18. this.salary = salary;
  19. this.dept = dept;
  20. }
  21. // TODO: 补全 getter / setter / toString 方法
  22. public String getId() {
  23. return id;
  24. }
  25. public void setId(String id) {
  26. this.id = id;
  27. }
  28. public String getName() {
  29. return name;
  30. }
  31. public void setName(String name) {
  32. this.name = name;
  33. }
  34. public double getSalary() {
  35. return salary;
  36. }
  37. public void setSalary(double salary) {
  38. this.salary = salary;
  39. }
  40. public String getDept() {
  41. return dept;
  42. }
  43. public void setDept(String dept) {
  44. this.dept = dept;
  45. }
  46. // TODO: 重写 compareTo —— 按薪资降序,薪资相同再按工号
  47. @Override
  48. public int compareTo(Employee o) {
  49. // 提示:Double.compare(o.getSalary(), this.salary) // 降序
  50. // 若返回 0,再比较工号:this.id.compareTo(o.getId())
  51. int result= Double.compare(o.getSalary(), this.salary);
  52. return result==0?this.id.compareTo(o.getId()):result;
  53. }
  54. @Override
  55. public boolean equals(Object o) {
  56. if (o == null || getClass() != o.getClass()) return false;
  57. Employee employee = (Employee) o;
  58. return Double.compare(salary, employee.salary) == 0 && Objects.equals(id, employee.id) && Objects.equals(name, employee.name) && Objects.equals(dept, employee.dept);
  59. }
  60. @Override
  61. public int hashCode() {
  62. return Objects.hash(id, name, salary, dept);
  63. }
  64. @Override
  65. public String toString() {
  66. return "Employee{" +
  67. "id='" + id + '\'' +
  68. ", name='" + name + '\'' +
  69. ", salary=" + salary +
  70. ", dept='" + dept + '\'' +
  71. '}';
  72. }
  73. }