Advertisement
Teammasik

laba5_OOP

Dec 12th, 2023
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.32 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Comparator;
  3. import java.util.List;
  4. import java.util.Objects;
  5. class Person {
  6. String lastName;
  7. String firstName;
  8. String middleName;
  9. String birthDate;
  10. String gender;
  11.  
  12. public Person(String lastName, String firstName, String middleName, String birthDate, String gender) {
  13. this.lastName = lastName;
  14. this.firstName = firstName;
  15. this.middleName = middleName;
  16. this.birthDate = birthDate;
  17. this.gender = gender;
  18. }
  19. }
  20. class Position {
  21. String title;
  22.  
  23. int valuability;
  24. public Position(String title, int valuability) {
  25. this.title = title;
  26. this.valuability=valuability;
  27. }
  28. }
  29. class StaffPosition {
  30. Position position;
  31. Department department;
  32.  
  33. public StaffPosition(Position position, Department department) {
  34. this.position = position;
  35. this.department = department;
  36. }
  37. }
  38.  
  39.  
  40. class Employee extends Person implements Comparable {
  41. Department department;
  42. Position position;
  43. double salary;
  44. boolean hired;
  45. public Employee(String lastName, String firstName, String middleName, String birthDate, String gender,
  46. Department department, Position position, double salary) {
  47. super(lastName, firstName, middleName, birthDate, gender);
  48. this.department = department;
  49. this.position = position;
  50. this.salary = salary;
  51. this.hired = true;
  52. }
  53. public void hire() {
  54. this.hired = true;
  55. }
  56. public void dismiss() {
  57. this.hired = false;
  58.  
  59. }
  60. @Override
  61. public boolean isComparable(Employee e) {
  62. if (!Objects.equals(department.name, e.department.name)) {
  63. return false;
  64. }
  65. return true;
  66. }
  67. }
  68. interface Comparable {
  69. boolean isComparable(Employee e);
  70. }
  71. class Statistics {
  72. public static int getEmployeeCount(Department department) {
  73. int count = 0;
  74. for (Employee emp:department.employees
  75. ) {
  76. count++;
  77. }
  78. return count;
  79. }
  80.  
  81. public static double getAverageSalary(Department department) {
  82. int count = 0;
  83. double salary = 0;
  84. for (Employee emp:department.employees
  85. ) {
  86. count++;
  87. salary += emp.salary;
  88. }
  89. return salary/count;
  90. }
  91. public static double getTotalSalary(Department department) {
  92. double salary = 0;
  93. for (Employee emp:department.employees
  94. ) {
  95. salary += emp.salary;
  96. }
  97. return salary;
  98. }
  99. }
  100. class Department {
  101. String name;
  102. Employee head;
  103. List<Employee> employees;
  104. List<Department> subDepartments;
  105. public Department(String name, Employee head, List<Employee> employees, List<Department> subDepartments) {
  106. this.name = name;
  107. this.head = head;
  108. this.employees = employees;
  109. this.subDepartments = subDepartments;
  110. }
  111. }
  112. class EmpComparator implements Comparator<Employee> {
  113. @Override
  114. public int compare(Employee e1, Employee e2) {
  115. if (!e1.isComparable(e2)) {
  116. return -1;
  117. }
  118. if (e1.position.valuability > e2.position.valuability) {
  119. return 1;
  120. } else if (e1.position.valuability < e2.position.valuability) {
  121. return 2;
  122. }
  123. return 0;
  124. }
  125. }
  126. public class Main {
  127. public static void main(String[] args) {
  128.  
  129. Position manager = new Position("Manager", 3);
  130. Position Topmanager = new Position("Manager", 5);
  131. Position programmer = new Position("Programmer", 7);
  132. Department progDepartment = new Department("PROG Department", null, new ArrayList<>(), new ArrayList<>());
  133. Department hrDepartment = new Department("HR Department", null, new ArrayList<>(), new ArrayList<>());
  134. Employee johnDoe = new Employee("Doe", "John", "", "01-01-1990", "Male", hrDepartment, manager, 50000.0);
  135. Employee carlJonson = new Employee("Johnson", "Carl", "", "01-10-1993", "Male", hrDepartment, manager, 70000.0);
  136. Employee jimJonson = new Employee("Johnson", "Jim", "", "15-10-1993", "Male", progDepartment, programmer, 140000.0);
  137. Employee whaneKris = new Employee("Kris", "Whane", "", "01-01-1990", "Male", hrDepartment, Topmanager, 100000.0);
  138.  
  139. ArrayList<Employee> listHR = new ArrayList<>();
  140. listHR.add(johnDoe);
  141. listHR.add(carlJonson);
  142. hrDepartment.employees = listHR;
  143.  
  144. ArrayList<Employee> listPR = new ArrayList<>();
  145. listPR.add(jimJonson);
  146. progDepartment.employees = listPR;
  147.  
  148. StaffPosition hrManager = new StaffPosition(manager, hrDepartment);
  149. System.out.println("Средняя зарплата по отделу: " + Statistics.getAverageSalary(hrDepartment));
  150. System.out.println("количество работников в отделе " + Statistics.getEmployeeCount(hrDepartment));
  151. System.out.println("полная зарплата по отделу " + Statistics.getTotalSalary(progDepartment));
  152.  
  153. EmpComparator comparator = new EmpComparator();
  154. int k = comparator.compare(johnDoe,whaneKris);
  155. if(k == 1){
  156. System.out.println((whaneKris.firstName + " подчиняется " + johnDoe.firstName));
  157. } else if (k == 2) {
  158. System.out.println((johnDoe.firstName + " подчиняется " + whaneKris.firstName));
  159. } else if (k == -1) {
  160. System.out.println((johnDoe.firstName + " и " + whaneKris.firstName +"невозможно сравнить"));
  161. } else {
  162. System.out.println((johnDoe.firstName + whaneKris.firstName + " равны"));
  163. }
  164.  
  165. k = comparator.compare(johnDoe,jimJonson);
  166. if(k == 1){
  167. System.out.println((jimJonson.firstName + " подчиняется "+ johnDoe.firstName));
  168. } else if (k == 2) {
  169. System.out.println((johnDoe.firstName + " подчиняется " + jimJonson.firstName));
  170. } else if (k == -1) {
  171. System.out.println((johnDoe.firstName + " и " + jimJonson.firstName + " невозможно сравнить"));
  172. } else {
  173. System.out.println((johnDoe.firstName + " " + jimJonson.firstName + " равны"));
  174. }
  175.  
  176. }
  177. }
  178.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement