diff --git a/src/ru/skypro/Employee.java b/src/ru/skypro/Employee.java new file mode 100644 index 0000000..1f2ec01 --- /dev/null +++ b/src/ru/skypro/Employee.java @@ -0,0 +1,82 @@ +package ru.skypro; + +// Ентити +//@Getter +public class Employee { + private static int counter; + private final Integer id; + private final String firstName; + private final String lastName; + private final String secondName; + //@Setter + private String department;//1-5 + //@Setter + private Integer salary; + + public Employee(final String firstName, final String lastName, final String secondName, final String department, final Integer salary) { + this.id = counter++; + this.firstName = firstName; + this.lastName = lastName; + this.secondName = secondName; + this.department = department; + this.salary = salary; + } + + // Alt+Insert generated && modified to JSON output + @Override + public String toString() { + return "{" + + "\"ИД\":" + id + + ", \"Фамилия\":\"" + lastName + '\"' + + ", \"Имя\":\"" + firstName + '\"' + + ", \"Отчество\":\"" + secondName + '\"' + + ", \"Отдел\":\"" + department + '\"' + + ", \"Зарплата\":" + salary + + "}"; + } + + public String toStringOnlyFIO() { + return "{" + + "\"Фамилия\":\"" + lastName + '\"' + + ", \"Имя\":\"" + firstName + '\"' + + ", \"Отчество\":\"" + secondName + "\"" + + "}"; + } + + public String toStringWithoutDepartment() { + return "{" + + "\"Фамилия\":\"" + lastName + '\"' + + ", \"Имя\":\"" + firstName + '\"' + + ", \"Отчество\":\"" + secondName + "\"" + + ", \"Зарплата\":" + salary + + "}"; + } + + public String getFirstName() { + return firstName; + } + + public String getLastName() { + return lastName; + } + + public String getSecondName() { + return secondName; + } + + public String getDepartment() { + return department; + } + + public void setDepartment(String department) { + this.department = department; + } + + public Integer getSalary() { + return salary; + } + + public void setSalary(Integer salary) { + this.salary = salary; + } +} diff --git a/src/ru/skypro/EmployeeBook.java b/src/ru/skypro/EmployeeBook.java new file mode 100644 index 0000000..72419ec --- /dev/null +++ b/src/ru/skypro/EmployeeBook.java @@ -0,0 +1,158 @@ +package ru.skypro; + +import java.util.Arrays; +import java.util.Objects; +import java.util.stream.Collectors; + +// Аналог репозитория похоже +public class EmployeeBook { + + private final Employee[] eployies = new Employee[10]; + + public EmployeeBook() { + + } + + public void init() { + for (int i = 0; i < 10; i++) { + eployies[i] = new Employee( + "И." + i, "Ф." + i, "О." + i, + String.valueOf(1 + i / 2), + 100000 + (int) (Math.random() * 100000)); + } + } + + public String getAllEmployies() { + return "[" + Arrays.stream(eployies) + .map(Employee::toString) + .collect(Collectors.joining(",\n")) + "]"; + } + + public Integer getSumSalaries() { + return Arrays.stream(eployies) + .mapToInt(Employee::getSalary) + .sum(); + } + + public Employee getEmployeeWithMinSalary() { + return Arrays.stream(eployies) + .reduce((e1, e2) -> e1.getSalary() < e2.getSalary() ? e1 : e2).orElseThrow(); + } + + public Employee getEmployeeWithMaxSalary() { + return Arrays.stream(eployies) + .reduce((e1, e2) -> e1.getSalary() > e2.getSalary() ? e1 : e2).orElseThrow(); + } + + public Double getAvgSalaries() { + return (double) getSumSalaries() / eployies.length; + } + + public String listNamesOfAllEmployies() { + return "[" + Arrays.stream(eployies) + .map(Employee::toStringOnlyFIO) + .collect(Collectors.joining(",\n")) + "]"; + } + + public void salaryIndex(Integer percent) { + Arrays.stream(eployies) + .forEach((e) -> e.setSalary(e.getSalary() + e.getSalary() * percent / 100)); + } + + public Employee getEmployeeWithMinSalaryInDepartment(String department) { + return Arrays.stream(eployies) + .filter(e -> Objects.equals(e.getDepartment(), department)) + .reduce((e1, e2) -> e1.getSalary() < e2.getSalary() ? e1 : e2).orElseThrow(); + } + + public Employee getEmployeeWithMaxSalaryInDepartment(String department) { + return Arrays.stream(eployies) + .filter(e -> Objects.equals(e.getDepartment(), department)) + .reduce((e1, e2) -> e1.getSalary() > e2.getSalary() ? e1 : e2).orElseThrow(); + } + + public Integer getSumSalariesInDepartment(String department) { + return Arrays.stream(eployies) + .filter(e -> Objects.equals(e.getDepartment(), department)) + .mapToInt(Employee::getSalary).sum(); + } + + public Double getAvgSalariesInDepartment(String department) { + return (double) getSumSalariesInDepartment(department) / + Arrays.stream(eployies) + .filter(e -> Objects.equals(e.getDepartment(), department)) + .count(); + } + + public void salaryIndexInDepartment(String department, Integer percent) { + Arrays.stream(eployies) + .filter(e -> Objects.equals(e.getDepartment(), department)) + .forEach((e) -> e.setSalary(e.getSalary() + e.getSalary() * percent / 100)); + } + + public String listNamesOfEmployiesInDepartment(String department) { + return "[" + Arrays.stream(eployies) + .filter(e -> Objects.equals(e.getDepartment(), department)) + .map(Employee::toStringWithoutDepartment) + .collect(Collectors.joining(",\n")) + "]"; + } + + public Employee[] listEmployiesWithSalaryMoreThanOrEqual(Integer value) { + var list = Arrays.stream(eployies) + .filter(e -> e.getSalary() >= value).toList(); + return list.toArray(new Employee[0]); + } + + public Employee[] listEmployiesWithSalaryLessThan(Integer value) { + var list = Arrays.stream(eployies) + .filter(e -> e.getSalary() < value).toList(); + return list.toArray(new Employee[0]); + } + + public String specialOutputForListEmployiesMethods(Employee[] eployies) { + return "[" + Arrays.stream(eployies) + .map(Employee::toStringWithoutDepartment) + .collect(Collectors.joining(",\n")) + "]"; + } + + public void addNewEmplyeeInMem(String firstName, String lastName, String secondName, String department, Integer salary) { + for (var i = 0; i < 10; i++) { + if (eployies[i] == null) { + eployies[i] = new Employee(firstName, lastName, secondName, department, salary); + return; + } + } + throw new RuntimeException("Все ячейки массива заняты"); + } + + public void delEmployeeById(Integer id) { + eployies[id] = null; + } + + public void delEmployeeByFIO(String firstName, String lastName, String secondName) { + for (var i = 0; i < 10; i++) { + if (eployies[i] != null && + Objects.equals(eployies[i].getFirstName(), firstName) && + Objects.equals(eployies[i].getLastName(), lastName) && + Objects.equals(eployies[i].getSecondName(), secondName) + ) + eployies[i] = null; + } + } + + public void setEmployeeSalaryAndDepartment(String firstName, String lastName, String secondName, String department, Integer salary) { + for (var i = 0; i < 10; i++) { + if (eployies[i] != null && + Objects.equals(eployies[i].getFirstName(), firstName) && + Objects.equals(eployies[i].getLastName(), lastName) && + Objects.equals(eployies[i].getSecondName(), secondName) + ) { + if (salary != null) + eployies[i].setSalary(salary); + if (department != null) + eployies[i].setDepartment(department); + } + } + } + +} diff --git a/src/ru/skypro/Main.java b/src/ru/skypro/Main.java index 625884e..483d5f2 100644 --- a/src/ru/skypro/Main.java +++ b/src/ru/skypro/Main.java @@ -1,7 +1,42 @@ package ru.skypro; public class Main { - public static void main(String[] args){ + private static final EmployeeBook book = new EmployeeBook(); + + public static void main(String[] args) { + book.init(); + //Basic difficult + System.out.println("Список всех сотрудников со всеми имеющимися по ним данными: " + book.getAllEmployies()); + System.out.println("Сумма затрат на зарплаты в месяц: " + book.getSumSalaries()); + System.out.println("Сотрудник с минимальной зарплатой: " + book.getEmployeeWithMinSalary()); + System.out.println("Сотрудник с максимальной зарплатой: " + book.getEmployeeWithMaxSalary()); + System.out.println("Среднее значение зарплат: " + book.getAvgSalaries()); + System.out.println("Ф. И. О. всех сотрудников: " + book.listNamesOfAllEmployies()); + //Advanced difficult + book.salaryIndex(10); + System.out.println("TestCase: " + book.getAllEmployies()); + String randomDepartment = String.valueOf(1 + (int) (Math.random() * 5)); + System.out.println("Сотрудник с минимальной зарплатой в отделе " + randomDepartment + ": " + book.getEmployeeWithMinSalaryInDepartment(randomDepartment)); + System.out.println("Сотрудник с максимальной зарплатой в отделе " + randomDepartment + ": " + book.getEmployeeWithMaxSalaryInDepartment(randomDepartment)); + System.out.println("Сумму затрат на зарплату по отделу " + randomDepartment + ": " + book.getSumSalariesInDepartment(randomDepartment)); + System.out.println("Средняя зарплата по отделу " + randomDepartment + ": " + book.getAvgSalariesInDepartment(randomDepartment)); + book.salaryIndexInDepartment(randomDepartment, 10); + System.out.println("Ф. И. О. сотрудников отдела " + randomDepartment + ": " + book.listNamesOfEmployiesInDepartment(randomDepartment)); + Integer someValue = 150000; + System.out.println("Сотрудники с зарплатой меньше числа " + someValue + ":"); + System.out.println(book.specialOutputForListEmployiesMethods(book.listEmployiesWithSalaryLessThan(someValue))); + System.out.println("Сотрудники с зарплатой больше числа " + someValue + ":"); + System.out.println(book.specialOutputForListEmployiesMethods(book.listEmployiesWithSalaryMoreThanOrEqual(someValue))); + //Very difficult + book.delEmployeeById(1); + book.delEmployeeByFIO("И.4", "Ф.4", "О.4"); + book.addNewEmplyeeInMem("asdf", "fdsa", "sasd", "3", 120000); + book.addNewEmplyeeInMem("QQasdf", "QWfdsa", "WWasd", "1", 60000); + book.setEmployeeSalaryAndDepartment("И.6", "Ф.6", "О.6", "1", 300000); + book.setEmployeeSalaryAndDepartment("И.7", "Ф.7", "О.7", null, 250000); + System.out.println("TestCase: "); + System.out.println(book.getAllEmployies()); } + }