diff --git a/src/ru/skypro/Main.java b/src/ru/skypro/Main.java index 625884e..3a43882 100644 --- a/src/ru/skypro/Main.java +++ b/src/ru/skypro/Main.java @@ -1,7 +1,138 @@ package ru.skypro; +import ru.skypro.exceptions.DataInvalidException; +import ru.skypro.task.Task; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Scanner; +import java.util.stream.Collectors; + public class Main { + static Map taskMap = new HashMap<>(); public static void main(String[] args){ + testInitTasks(); + try (Scanner scanner = new Scanner(System.in)) { + label: + while (true) { + printMenu(); + System.out.print("Выберите пункт меню: "); + if (scanner.hasNextInt()) { + int menu = scanner.nextInt(); + switch (menu) { + case 1: + inputTask(scanner); + break; + case 2: + removeTask(scanner); + break; + case 3: + getTaskOnDate(scanner); + break; + case 0: + break label; + } + } else { + scanner.next(); + System.out.println("Выберите пункт меню из списка!"); + } + } + } + } + + private static void testInitTasks() { + try { + Task newTask = new Task( + "Планерка по понедельникам", + "Планерка по понедельникам", + 2, + 3, + "17-10-2022", + "10:00" + ); + taskMap.put(newTask.getId(), newTask); + newTask = new Task( + "Обеденный перерыв", + "Обеденный перерыв", + 1, + 2, + "17-10-2022", + "13:00" + ); + taskMap.put(newTask.getId(), newTask); + } catch (DataInvalidException e){ + System.err.println(); + } + } + + private static void inputTask(Scanner scanner) { + System.out.print("Введите название задачи: \n"); + String taskName = scanner.next(); + System.out.print("Введите описание задачи: \n"); + String description = scanner.next(); + System.out.print("Выберите тип задачи: \n 1: Личная \n 2: Рабочая\n"); + int type = scanner.nextInt(); + System.out.print("Выберите периодичность задачи: \n"); + int taskPeriod = scanner.nextInt(); + System.out.print("Выберите дату задачи: \n Формат Даты: 'dd-mm-yyyy'\n"); + String date = scanner.next(); + System.out.print("Выберите время задачи: \n Формат Времени: 'hh:mm'\n"); + String time = scanner.next(); + try { + Task newTask = new Task( + taskName, + description, + type, + taskPeriod, + date, + time + ); + taskMap.put(newTask.getId(), newTask); + } catch (DataInvalidException e){ + System.err.println(); + } + } + + private static void printMenu() { + System.out.println( + """ + 1. Добавить задачу + 2. Удалить задачу + 3. Получить задачу на указанный день + 0. Выход + """ + ); + } + + private static void removeTask(Scanner scanner) { + System.out.print("Выберите id задачи: \n"); + for (var task:taskMap.entrySet()) { + System.out.print(task.getKey()+" "+task.getValue().toString()+" \n"); + } + System.out.print("0 - отменить \n"); + int idTask = scanner.nextInt(); + if (idTask == 0){ + return; + } + if (taskMap.containsKey(idTask)){ + taskMap.get(idTask).deleteTask(); + System.out.print("Удалено\n"); + } else { + System.out.print("Данного id не найдено \n"); + } + } + private static void getTaskOnDate(Scanner scanner) { + System.out.print("Выберите дату задачи: \n Формат Даты: 'dd-mm-yyyy'\n"); + String date = scanner.next(); + List response = taskMap.values().stream().filter( + (t) -> t.whenNext(date) + ).toList(); + response.forEach(task -> { + System.out.print(task.toString() + " \n"); + }); } } diff --git a/src/ru/skypro/exceptions/DataInvalidException.java b/src/ru/skypro/exceptions/DataInvalidException.java new file mode 100644 index 0000000..76e3cb6 --- /dev/null +++ b/src/ru/skypro/exceptions/DataInvalidException.java @@ -0,0 +1,11 @@ +package ru.skypro.exceptions; + +public class DataInvalidException extends Exception{ + public DataInvalidException() { + super("error"); + } + + public DataInvalidException(String message) { + super(message); + } +} diff --git a/src/ru/skypro/exceptions/NotImplementedException.java b/src/ru/skypro/exceptions/NotImplementedException.java new file mode 100644 index 0000000..ff95aab --- /dev/null +++ b/src/ru/skypro/exceptions/NotImplementedException.java @@ -0,0 +1,11 @@ +package ru.skypro.exceptions; + +public class NotImplementedException extends Exception { + public NotImplementedException() { + this("SomeClass"); + } + + public NotImplementedException(String message) { + super(message + " not implemented"); + } +} diff --git a/src/ru/skypro/task/PeriodUtils.java b/src/ru/skypro/task/PeriodUtils.java new file mode 100644 index 0000000..10a1256 --- /dev/null +++ b/src/ru/skypro/task/PeriodUtils.java @@ -0,0 +1,11 @@ +package ru.skypro.task; + +import ru.skypro.exceptions.NotImplementedException; + +import java.time.LocalDate; + +public interface PeriodUtils { + + + Boolean whenNext(String date); +} diff --git a/src/ru/skypro/task/Task.java b/src/ru/skypro/task/Task.java new file mode 100644 index 0000000..18be9c6 --- /dev/null +++ b/src/ru/skypro/task/Task.java @@ -0,0 +1,124 @@ +package ru.skypro.task; + +import ru.skypro.exceptions.DataInvalidException; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.Period; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeParseException; + +public class Task implements PeriodUtils { + private static int counter; + private final Integer id; + private String header; + private String description; + private TaskType type; + private LocalDateTime dateTime; + private TaskPeriod taskPeriod; + + private Boolean flagDelete; + + public Task(String header, String description, int type, int taskPeriod, String date, String time) throws DataInvalidException { + this.flagDelete = false; + try { + this.id = counter++; + this.header = header; + this.description = description; + this.type = TaskType.values()[type-1]; + this.taskPeriod = TaskPeriod.values()[taskPeriod-1]; + this.dateTime = LocalDateTime.parse(date+" "+time, DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm")); + } catch (DateTimeParseException | ArrayIndexOutOfBoundsException e){ + throw new DataInvalidException(e.getMessage()); + } + } + + public Integer getId() { + return id; + } + + public String getHeader() { + return header; + } + + public String getDescription() { + return description; + } + + public TaskType getType() { + return type; + } + + public LocalDateTime getDateTime() { + return dateTime; + } + + public TaskPeriod getTaskPeriod() { + return taskPeriod; + } + + public void setHeader(String header) { + this.header = header; + } + + public void setDescription(String description) { + this.description = description; + } + + public void setType(TaskType type) { + this.type = type; + } + + public void setDateTime(LocalDateTime dateTime) { + this.dateTime = dateTime; + } + + public void setTaskPeriod(TaskPeriod taskPeriod) { + this.taskPeriod = taskPeriod; + } + + @Override + public Boolean whenNext(String date) { + if (this.flagDelete) { + return false; + } + var dateIs = LocalDate.parse(date, DateTimeFormatter.ofPattern("dd-MM-yyyy")); + switch (taskPeriod) { + case ONE_OFF -> { + return dateIs.equals(this.dateTime.toLocalDate()); + } + case DAILY -> { + return dateIs.isAfter(this.dateTime.toLocalDate()); + } + case WEEKLY -> { + return dateIs.getDayOfWeek().equals(this.dateTime.toLocalDate().getDayOfWeek()); + } + case MONTHLY -> { + return dateIs.getDayOfMonth() == this.dateTime.toLocalDate().getDayOfMonth(); +// return this.dateTime.toLocalDate().datesUntil(dateIs.plusDays(1), Period.ofMonths(1)).anyMatch(t->t.equals(dateIs)); + } + case YEARLY -> { + return dateIs.getDayOfYear() == this.dateTime.toLocalDate().getDayOfYear(); +// return this.dateTime.toLocalDate().datesUntil(dateIs.plusDays(1), Period.ofYears(1)).anyMatch(t->t.equals(dateIs)); + } + default -> { + return false; + } + } + } + + public void deleteTask() { + this.flagDelete = true; + } + + @Override + public String toString() { + return "Task{" + + "header='" + header + '\'' + + ", description='" + description + '\'' + + ", type=" + type + + ", dateTime=" + dateTime + + ", taskPeriod=" + taskPeriod + + '}'; + } +} diff --git a/src/ru/skypro/task/TaskPeriod.java b/src/ru/skypro/task/TaskPeriod.java new file mode 100644 index 0000000..ea7296e --- /dev/null +++ b/src/ru/skypro/task/TaskPeriod.java @@ -0,0 +1,9 @@ +package ru.skypro.task; + +public enum TaskPeriod { + ONE_OFF, + DAILY, + WEEKLY, + MONTHLY, + YEARLY +} diff --git a/src/ru/skypro/task/TaskType.java b/src/ru/skypro/task/TaskType.java new file mode 100644 index 0000000..f3aa896 --- /dev/null +++ b/src/ru/skypro/task/TaskType.java @@ -0,0 +1,6 @@ +package ru.skypro.task; + +public enum TaskType { + PERSONAL, + WORK +}