Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.List;
- import java.util.Objects;
- class Person {
- String lastName;
- String firstName;
- String middleName;
- String birthDate;
- String gender;
- public Person(String lastName, String firstName, String middleName, String birthDate, String gender) {
- this.lastName = lastName;
- this.firstName = firstName;
- this.middleName = middleName;
- this.birthDate = birthDate;
- this.gender = gender;
- }
- }
- class Position {
- String title;
- int valuability;
- public Position(String title, int valuability) {
- this.title = title;
- this.valuability=valuability;
- }
- }
- class StaffPosition {
- Position position;
- Department department;
- public StaffPosition(Position position, Department department) {
- this.position = position;
- this.department = department;
- }
- }
- class Employee extends Person implements Comparable {
- Department department;
- Position position;
- double salary;
- boolean hired;
- public Employee(String lastName, String firstName, String middleName, String birthDate, String gender,
- Department department, Position position, double salary) {
- super(lastName, firstName, middleName, birthDate, gender);
- this.department = department;
- this.position = position;
- this.salary = salary;
- this.hired = true;
- }
- public void hire() {
- this.hired = true;
- }
- public void dismiss() {
- this.hired = false;
- }
- @Override
- public boolean isComparable(Employee e) {
- if (!Objects.equals(department.name, e.department.name)) {
- return false;
- }
- return true;
- }
- }
- interface Comparable {
- boolean isComparable(Employee e);
- }
- class Statistics {
- public static int getEmployeeCount(Department department) {
- int count = 0;
- for (Employee emp:department.employees
- ) {
- count++;
- }
- return count;
- }
- public static double getAverageSalary(Department department) {
- int count = 0;
- double salary = 0;
- for (Employee emp:department.employees
- ) {
- count++;
- salary += emp.salary;
- }
- return salary/count;
- }
- public static double getTotalSalary(Department department) {
- double salary = 0;
- for (Employee emp:department.employees
- ) {
- salary += emp.salary;
- }
- return salary;
- }
- }
- class Department {
- String name;
- Employee head;
- List<Employee> employees;
- List<Department> subDepartments;
- public Department(String name, Employee head, List<Employee> employees, List<Department> subDepartments) {
- this.name = name;
- this.head = head;
- this.employees = employees;
- this.subDepartments = subDepartments;
- }
- }
- class EmpComparator implements Comparator<Employee> {
- @Override
- public int compare(Employee e1, Employee e2) {
- if (!e1.isComparable(e2)) {
- return -1;
- }
- if (e1.position.valuability > e2.position.valuability) {
- return 1;
- } else if (e1.position.valuability < e2.position.valuability) {
- return 2;
- }
- return 0;
- }
- }
- public class Main {
- public static void main(String[] args) {
- Position manager = new Position("Manager", 3);
- Position Topmanager = new Position("Manager", 5);
- Position programmer = new Position("Programmer", 7);
- Department progDepartment = new Department("PROG Department", null, new ArrayList<>(), new ArrayList<>());
- Department hrDepartment = new Department("HR Department", null, new ArrayList<>(), new ArrayList<>());
- Employee johnDoe = new Employee("Doe", "John", "", "01-01-1990", "Male", hrDepartment, manager, 50000.0);
- Employee carlJonson = new Employee("Johnson", "Carl", "", "01-10-1993", "Male", hrDepartment, manager, 70000.0);
- Employee jimJonson = new Employee("Johnson", "Jim", "", "15-10-1993", "Male", progDepartment, programmer, 140000.0);
- Employee whaneKris = new Employee("Kris", "Whane", "", "01-01-1990", "Male", hrDepartment, Topmanager, 100000.0);
- ArrayList<Employee> listHR = new ArrayList<>();
- listHR.add(johnDoe);
- listHR.add(carlJonson);
- hrDepartment.employees = listHR;
- ArrayList<Employee> listPR = new ArrayList<>();
- listPR.add(jimJonson);
- progDepartment.employees = listPR;
- StaffPosition hrManager = new StaffPosition(manager, hrDepartment);
- System.out.println("Средняя зарплата по отделу: " + Statistics.getAverageSalary(hrDepartment));
- System.out.println("количество работников в отделе " + Statistics.getEmployeeCount(hrDepartment));
- System.out.println("полная зарплата по отделу " + Statistics.getTotalSalary(progDepartment));
- EmpComparator comparator = new EmpComparator();
- int k = comparator.compare(johnDoe,whaneKris);
- if(k == 1){
- System.out.println((whaneKris.firstName + " подчиняется " + johnDoe.firstName));
- } else if (k == 2) {
- System.out.println((johnDoe.firstName + " подчиняется " + whaneKris.firstName));
- } else if (k == -1) {
- System.out.println((johnDoe.firstName + " и " + whaneKris.firstName +"невозможно сравнить"));
- } else {
- System.out.println((johnDoe.firstName + whaneKris.firstName + " равны"));
- }
- k = comparator.compare(johnDoe,jimJonson);
- if(k == 1){
- System.out.println((jimJonson.firstName + " подчиняется "+ johnDoe.firstName));
- } else if (k == 2) {
- System.out.println((johnDoe.firstName + " подчиняется " + jimJonson.firstName));
- } else if (k == -1) {
- System.out.println((johnDoe.firstName + " и " + jimJonson.firstName + " невозможно сравнить"));
- } else {
- System.out.println((johnDoe.firstName + " " + jimJonson.firstName + " равны"));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement