ЯП Лабораторная работа 6
.pdfif (Violations == null || Violations.isEmpty()) { System.out.println("Нарушений нет :)");
} else {
for (Violation violation : Violations) { System.out.println(violation.getViolationName() + " (Дата: " +
dateTimeFormat.format(violation.getDate()) + ")");
System.out.println("Штраф: " + violation.getPenaltyCost() + " руб.");
}
}
}
}
class MonitoringRealization extends MonitoringSystem { @Override
public void getEmployeesInfo(ArrayList<Employee> employees) { System.out.println("Информация о производительности сотрудников:"); if (employees.isEmpty()){
System.out.println("Список сотрудников пуст."); } else{
for (Employee employee : employees) {
System.out.println("Сотрудник: " + employee.getName() + " " + employee.getSurname());
System.out.println("Должность: " +
employee.getPosition().getPositionName() +
"(смена " + employee.getPosition().getWorkingHours() + "
часов, " +
employee.getPosition().getSalary() + " руб./мес.)"); System.out.println("Эффективность: " + employee.getEfficiency() +
"%");
if (employee.getViolations() == null ||
employee.getViolations().isEmpty()) {
System.out.println("Общее количество штрафов: 0\n"); } else {
System.out.println("Общее количество штрафов: " + employee.getViolations().size() + "\n");
}
}
}
}
@Override
public void getEmployeeInfo(ArrayList<Employee> employees) { try {
11
if (employees.isEmpty()) { System.out.println("Список сотрудников пуст."); return;
}
System.out.println("Выберите сотрудника:"); for (int i = 0; i < employees.size(); i++) {
System.out.println((i + 1) + " – " + employees.get(i).getName() + " " + employees.get(i).getSurname());
}
System.out.println("\nВведите номер сотрудника: "); int employeeID = Integer.parseInt(Main.br.readLine()); if (employeeID < 1 || employeeID > employees.size()) {
throw new IllegalArgumentException("Такой ID сотрудника не
существует.");
}
Employee employee = employees.get(employeeID - 1); System.out.println("\nИнформация о сотруднике:");
System.out.println("Сотрудник: " + employee.getName() + " " + employee.getSurname());
System.out.println("Должность: " +
employee.getPosition().getPositionName() +
"(смена " + employee.getPosition().getWorkingHours() + " часов, "
+
employee.getPosition().getSalary() + " руб./мес.)"); System.out.println("Эффективность: " + employee.getEfficiency() + "%"); System.out.println("Зарплата после штрафов: " + employee.calculateSalary()
+ " руб./мес."); System.out.print("Нарушения: "); employee.displayViolations();
} catch (NumberFormatException e) {
System.out.println("Ошибка: Введите корректные числовые значения.");
}catch (IllegalArgumentException e) { System.out.println("Ошибка: " + e.getMessage());
}catch (IOException e) {
System.out.println("Ошибка ввода-вывода: " + e.getMessage());
}
}
@Override
public void employeesEfficiency(ArrayList<Employee> employees) { int totalEfficiency = 0;
for (Employee employee : employees) { totalEfficiency += employee.getEfficiency();
12
}
if (employees.isEmpty()){ System.out.println("Список сотрудников пуст.");
}else{
totalEfficiency /= employees.size();
System.out.println("Общая эффективность сотрудников: " + totalEfficiency +
"%\n");
}
}
public void addNewEmployee(ArrayList<Employee> employees, ArrayList<Position> positions) throws IOException {
try {
System.out.println("Введите имя сотрудника:"); String name = Main.br.readLine(); System.out.println("Введите фамилию сотрудника:"); String surname = Main.br.readLine();
System.out.println("Выберите должность сотрудника:"); for (int i = 0; i < positions.size(); i++) {
System.out.println((i + 1) + " – " +
positions.get(i).getPositionName());
}
int positionID = Integer.parseInt(Main.br.readLine()); if (positionID < 1 || positionID > positions.size()) {
throw new IllegalArgumentException("Некорректный ID должности.");
}
employees.add(new Employee(name, surname, positions.get(positionID - 1),
100));
System.out.println("Сотрудник успешно добавлен!"); } catch (NumberFormatException e) {
System.out.println("Ошибка: Введите корректные числовые значения."); } catch (IllegalArgumentException e) {
System.out.println("Ошибка: " + e.getMessage());
}
}
public void deleteEmployee(ArrayList<Employee> employees) throws IOException { try {
if (employees.isEmpty()) { System.out.println("Список сотрудников пуст."); return;
}
System.out.println("Выберите сотрудника, которого хотите удалить:");
13
for (int i = 0; i < employees.size(); i++) {
System.out.println((i + 1) + " – " + employees.get(i).getName() + " " + employees.get(i).getSurname());
}
int employeeID = Integer.parseInt(Main.br.readLine()); if (employeeID < 1 || employeeID > employees.size()) {
throw new IllegalArgumentException("Такой ID сотрудника не
существует.");
}
employees.remove(employeeID - 1); System.out.println("Сотрудник успешно удален!");
} catch (NumberFormatException e) {
System.out.println("Ошибка: Введите корректные числовые значения."); } catch (IllegalArgumentException e) {
System.out.println("Ошибка: " + e.getMessage());
}
}
@Override
public void addEmployeeViolation(ArrayList<Employee> employees) { try {
if (employees.isEmpty()) { System.out.println("Список сотрудников пуст."); return;
}
System.out.println("Выберите сотрудника, которому хотите добавить
нарушение:");
for (int i = 0; i < employees.size(); i++) {
System.out.println((i + 1) + " – " + employees.get(i).getName() + " " + employees.get(i).getSurname());
}
int employeeID = Integer.parseInt(Main.br.readLine()); if (employeeID < 1 || employeeID > employees.size()) {
throw new IllegalArgumentException("Некорректный ID сотрудника.");
}
Employee employee = employees.get(employeeID - 1); System.out.println("Введите описание нарушения:"); String violationName = Main.br.readLine();
System.out.println("Введите дату нарушения (формат: dd.MM.yyyy):"); String dateInput = Main.br.readLine();
Date violationDate = new SimpleDateFormat("dd.MM.yyyy").parse(dateInput); System.out.println("Введите сумму штрафа:");
double penaltyCost = Double.parseDouble(Main.br.readLine());
14
Violation violation = new Violation(violationName, violationDate,
penaltyCost);
employee.addViolation(violation); System.out.println("Нарушение успешно добавлено!");
} catch (NumberFormatException e) {
System.out.println("Ошибка: Введите корректные числовые значения.");
}catch (IllegalArgumentException e) { System.out.println("Ошибка: " + e.getMessage());
}catch (Exception e) {
System.out.println("Ошибка: " + e.getMessage());
}
}
@Override
public void deleteEmployeeViolation(ArrayList<Employee> employees) { try {
if (employees.isEmpty()) { System.out.println("Список сотрудников пуст."); return;
}
System.out.println("Выберите сотрудника, у которого хотите удалить
нарушение:");
for (int i = 0; i < employees.size(); i++) {
System.out.println((i + 1) + " – " + employees.get(i).getName() + " " + employees.get(i).getSurname());
}
int employeeID = Integer.parseInt(Main.br.readLine()); if (employeeID < 1 || employeeID > employees.size()) {
throw new IllegalArgumentException("Некорректный ID сотрудника.");
}
Employee employee = employees.get(employeeID - 1); List<Violation> violations = employee.getViolations(); if (violations == null || violations.isEmpty()) {
System.out.println("У сотрудника нет нарушений."); return;
}
System.out.println("Выберите нарушение для удаления:"); for (int i = 0; i < violations.size(); i++) {
System.out.println((i + 1) + " – " + violations.get(i).getViolationName() + " (" + violations.get(i).getDate() + ")");
}
int violationID = Integer.parseInt(Main.br.readLine()); if (violationID < 1 || violationID > violations.size()) {
15
throw new IllegalArgumentException("Некорректный ID нарушения.");
}
violations.remove(violationID - 1); System.out.println("Нарушение успешно удалено!");
} catch (NumberFormatException e) {
System.out.println("Ошибка: Введите корректные числовые значения.");
}catch (IllegalArgumentException e) { System.out.println("Ошибка: " + e.getMessage());
}catch (Exception e){
System.out.println("Ошибка: " + e.getMessage());
}
}
public void addNewPosition(ArrayList<Position> positions) throws IOException { try {
System.out.println("Введите название новой должности:"); String name = Main.br.readLine(); System.out.println("Введите время работы:");
int workHours = Integer.parseInt(Main.br.readLine()); System.out.println("Введите базовую заработную плату:"); double salary = Double.parseDouble(Main.br.readLine()); positions.add(new Position(name, workHours, salary)); System.out.println("Новая должность успешно добавлена!");
} catch (NumberFormatException e) {
System.out.println("Ошибка: Введите корректные числовые значения.");
}
}
public void deletePosition(ArrayList<Position> positions) throws IOException { try {
if (positions.isEmpty()) { System.out.println("Список должностей пуст."); return;
}
System.out.println("Выберите должность, которую хотите удалить:"); for (int i = 0; i < positions.size(); i++) {
System.out.println((i + 1) + " – " +
positions.get(i).getPositionName());
}
int positionID = Integer.parseInt(Main.br.readLine()); if (positionID < 1 || positionID > positions.size()) {
throw new IllegalArgumentException("Такой ID должности не
существует.");
16
}
positions.remove(positionID - 1); System.out.println("Должность успешно удалена!");
} catch (NumberFormatException e) {
System.out.println("Ошибка: Введите корректные числовые значения."); } catch (IllegalArgumentException e) {
System.out.println("Ошибка: " + e.getMessage());
}
}
}
public class Main {
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static ArrayList<Employee> employees = new ArrayList<>(); public static ArrayList<Position> positions = new ArrayList<>();
public static void main(String[] args) throws IOException { MonitoringRealization ms = new MonitoringRealization(); boolean isRunning = true;
positions.add(new Position("Генеральный директор", 8, 120000)); positions.add(new Position("Менеджер", 8, 50000)); positions.add(new Position("Программист", 10, 65000));
employees.add(new Employee("Алиса", "Бахтина", positions.get(2), 100)); employees.add(new Employee("Дарья", "Исаева", positions.get(2), 100)); employees.add(new Employee("Игорь", "Куличихин", positions.get(0), 100)); employees.add(new Employee("Кирилл", "Новиков", positions.get(1), 100)); employees.add(new Employee("Руслан", "Абиджанов", positions.get(1), 100));
System.out.println("Добро пожаловать в систему для управления персоналом!");
while (isRunning) { Menu();
String command = br.readLine(); switch (command) {
case "1": ms.getEmployeesInfo(employees); break;
case "2": ms.employeesEfficiency(employees); break;
17
case "3": ms.getEmployeeInfo(employees); break;
case "4":
if (AdminMenu()) { ms.addNewEmployee(employees, positions);
}
break; case "5":
if (AdminMenu()) { ms.deleteEmployee(employees);
}
break; case "6":
ms.addEmployeeViolation(employees);
break; case "7":
ms.deleteEmployeeViolation(employees);
break; case "8":
if (AdminMenu()) { ms.addNewPosition(positions);
}
break; case "9":
if (AdminMenu()) { ms.deletePosition(positions);
}
break; case "0":
System.out.println("Выход из системы..."); isRunning = false;
break;
default:
System.out.println("Неизвестная команда, повторите попытку!"); break;
}
}
}
static void Menu() { System.out.println("\nВыберите действие:");
System.out.println("1 – Посмотреть информацию о всех сотрудниках");
18
System.out.println("2 – Общая эффективность сотрудников"); System.out.println("3 – Посмотреть информацию о сотруднике"); System.out.println("--------------------------------------"); System.out.println("4 – Добавить нового сотрудника в систему"); System.out.println("5 – Удалить сотрудника из системы"); System.out.println("6 – Добавить нарушение сотруднику"); System.out.println("7 – Удалить нарушение сотрудника"); System.out.println("8 – Добавить новую должность"); System.out.println("9 – Удалить должность"); System.out.println("--------------------------------------"); System.out.println("0 – Выход из системы");
}
static boolean AdminMenu() throws IOException { int attempts = 3;
while (attempts > 0) {
System.out.println("Внимание! Эти настройки доступны только с правами администратора.");
System.out.println("Введите пароль:"); String password = br.readLine();
if (!password.equals("admin")) {
System.out.println("Пароль неверный! Повторите попытку."); attempts--;
} else {
return true;
}
}
System.out.println("Превышено количество попыток входа."); return false;
}
}
19
