From 47774694f6e032ed55535628fce4a230d5af3a74 Mon Sep 17 00:00:00 2001 From: project Date: Mon, 4 Feb 2019 00:58:24 +0200 Subject: [PATCH] add my classwork --- .../artem/tasks/FindDistinctTask.java | 95 ++++++++++ .../javageeks/artem/tasks/Task.java | 60 ++++++ .../javageeks/artem/tasks/TaskType.java | 7 + .../artem/tasks/FindDistinctTaskTest.java | 179 ++++++++++++++++++ 4 files changed, 341 insertions(+) create mode 100644 modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/tasks/FindDistinctTask.java create mode 100644 modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/tasks/Task.java create mode 100644 modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/tasks/TaskType.java create mode 100644 modules/artem/src/test/java/com/hillel/elementary/javageeks/artem/tasks/FindDistinctTaskTest.java diff --git a/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/tasks/FindDistinctTask.java b/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/tasks/FindDistinctTask.java new file mode 100644 index 0000000..26519a4 --- /dev/null +++ b/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/tasks/FindDistinctTask.java @@ -0,0 +1,95 @@ +package com.hillel.elementary.javageeks.artem.tasks; + +import java.lang.reflect.Array; +import java.util.Arrays; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collector; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class FindDistinctTask { + + // Вернуть список с уникальными задачами (доп. сложность - с уникальными именами) + public static List allDistinctTasks(List tasks) { + List listOfUniqueTasks = tasks.stream() + .distinct() + .collect(Collectors.toList()); + return listOfUniqueTasks; + } + + // Найти 5 первых (по дате создания) задач по чтению, вернуть имя задачи + public static List top5ReadingTaskNames(List tasks) { + List list = tasks.stream() + .filter(i -> i.getType() == TaskType.READING) + .sorted((i,j) -> i.getCreationDate().compareTo(j.getCreationDate())) + .map(i->i.getName()) + .limit(5) + .collect(Collectors.toList()); + return list; + } + + // Посчитать количество задач указанного типа + public static Long countAllTasksByType(List tasks, TaskType type) { + long counter = tasks.stream() + .map(Task::getType) + .filter(i->i.equals(type)) + .count(); + return counter; + } + + // Вернуть все уникальные теги задач + public static List getAllDistinctTags(List tasks) { + List listOfUniqueTags = tasks.stream() + .map(Task::getTags) + .flatMap(tags -> tags.stream()) + .distinct() + .collect(Collectors.toList()); + + + return listOfUniqueTags; + } + + // Проверить у всех ли задач по чтению есть тег "Books" + public static Boolean isAllReadingTasksWithTagBooks(List tasks) { + boolean tagExist = tasks.stream() + .filter(i -> i.getType() == TaskType.READING) + .allMatch(i ->i.getTags ().contains ("Books")); + return tagExist; + } + + //Сконкатенировать все названия задач в одну надпись через "***" + public static String joinAllTaskTitles(List tasks) { + String finalString = tasks.stream() + .map(Task::getName) + .reduce((i,j)->i+"***"+j).get(); + return finalString; + } + + // Вернуть строку с числами, перечисленными через запятую на основе заданного списка целых чисел. + // Каждому элементу должна предшествовать буква «e», если число четное, и буква «o», если число нечетное. + // Например, если список ввода (3,44), вывод должен быть 'o3, e44'. + public static String evensAndOdds(String numbers) { + String str = Stream.of(numbers) + .flatMap (i -> Arrays.asList (i.split (",")) + .stream ()) + .map(String::trim) + .filter (p->!p.isEmpty ()) + .map (Integer::parseInt) + .map(p->(p % 2 == 0 ? "e" + p : "o" + p)) + .reduce((i,j) -> (i + "," + j)) + .orElse (""); + return str; + } + + //Вернуть список всех строк, которые начинаются с буквы «а» и имеют ровно 3 буквы + public static List searchForThreeLetterWordsStartingWithA(List list) { + List listOfThreeLetterWordsStartingWithA = list.stream() + .filter(i -> i.length() == 3) + .filter(j -> j.substring(0,1).equals("a")) + .collect(Collectors.toList()); + return listOfThreeLetterWordsStartingWithA; + } + + +} \ No newline at end of file diff --git a/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/tasks/Task.java b/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/tasks/Task.java new file mode 100644 index 0000000..cbfde7f --- /dev/null +++ b/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/tasks/Task.java @@ -0,0 +1,60 @@ +package com.hillel.elementary.javageeks.artem.tasks; + +import java.time.LocalDate; +import java.util.List; +import java.util.Objects; + +public class Task { + private final String name; + private final TaskType type; + private final LocalDate creationDate; + private final List tags; + + public Task(String name, TaskType type, LocalDate creationDate, List tags) { + this.name = name; + this.type = type; + this.creationDate = creationDate; + this.tags = tags; + } + + public String getName() { + return name; + } + + public TaskType getType() { + return type; + } + + public LocalDate getCreationDate() { + return creationDate; + } + + public List getTags() { + return tags; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Task task = (Task) o; + return Objects.equals(name, task.name) && + type == task.type && + Objects.equals(creationDate, task.creationDate); + } + + @Override + public int hashCode() { + return Objects.hash(name, type, creationDate); + } + + @Override + public String toString() { + return "Task{" + + "name='" + name + '\'' + + ", type=" + type + + ", creationDate=" + creationDate + + ", tags=" + tags + + '}'; + } +} diff --git a/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/tasks/TaskType.java b/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/tasks/TaskType.java new file mode 100644 index 0000000..ea03e34 --- /dev/null +++ b/modules/artem/src/main/java/com/hillel/elementary/javageeks/artem/tasks/TaskType.java @@ -0,0 +1,7 @@ +package com.hillel.elementary.javageeks.artem.tasks; + +enum TaskType { + READING, + WRITING, + SPEAKING +} diff --git a/modules/artem/src/test/java/com/hillel/elementary/javageeks/artem/tasks/FindDistinctTaskTest.java b/modules/artem/src/test/java/com/hillel/elementary/javageeks/artem/tasks/FindDistinctTaskTest.java new file mode 100644 index 0000000..7824c8e --- /dev/null +++ b/modules/artem/src/test/java/com/hillel/elementary/javageeks/artem/tasks/FindDistinctTaskTest.java @@ -0,0 +1,179 @@ +package com.hillel.elementary.javageeks.artem.tasks; + +import org.assertj.core.util.Lists; +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +class FindDistinctTaskTest { + private List emptyTagsList = Collections.emptyList(); + private LocalDate dummyDate = LocalDate.of(2018, 12, 1); + + @Test + void shouldReturnDistinctListOfTasks() { + List tasks = Arrays.asList( + new Task("Lesson 1", TaskType.READING, dummyDate, emptyTagsList), + new Task("Lesson 2", TaskType.WRITING, dummyDate, emptyTagsList), + new Task("Lesson 1", TaskType.READING, dummyDate, emptyTagsList), + new Task("Lesson 1", TaskType.READING, dummyDate, emptyTagsList), + new Task("Lesson 7", TaskType.WRITING, dummyDate, emptyTagsList), + new Task("Lesson 8", TaskType.SPEAKING, dummyDate, emptyTagsList), + new Task("Lesson 7", TaskType.WRITING, dummyDate, emptyTagsList), + new Task("Lesson 8", TaskType.SPEAKING, dummyDate, emptyTagsList), + new Task("Lesson 9", TaskType.READING, dummyDate, emptyTagsList) + ); + + List result = FindDistinctTask.allDistinctTasks(tasks); + + assertThat(result).isEqualTo(Arrays.asList( + new Task("Lesson 1", TaskType.READING, dummyDate, emptyTagsList), + new Task("Lesson 2", TaskType.WRITING, dummyDate, emptyTagsList), + new Task("Lesson 7", TaskType.WRITING, dummyDate, emptyTagsList), + new Task("Lesson 8", TaskType.SPEAKING, dummyDate, emptyTagsList), + new Task("Lesson 9", TaskType.READING, dummyDate, emptyTagsList) + )); + } + + @Test + void shouldReturnListOfReadingTaskNames() { + List tasks = Arrays.asList( + new Task("Lesson 1", TaskType.READING, LocalDate.of(2018, 1, 1), emptyTagsList), + new Task("Lesson 2", TaskType.WRITING, dummyDate, emptyTagsList), + new Task("Lesson 3", TaskType.READING, LocalDate.of(2018, 10, 1), emptyTagsList), + new Task("Lesson 4", TaskType.SPEAKING, dummyDate, emptyTagsList), + new Task("Lesson 5", TaskType.READING, LocalDate.of(2017, 7, 1), emptyTagsList), + new Task("Lesson 6", TaskType.WRITING, dummyDate, emptyTagsList), + new Task("Lesson 7", TaskType.WRITING, dummyDate, emptyTagsList), + new Task("Lesson 8", TaskType.SPEAKING, dummyDate, emptyTagsList), + new Task("Lesson 9", TaskType.READING, LocalDate.of(2017, 1, 1), emptyTagsList), + new Task("Lesson 10", TaskType.READING, LocalDate.of(2018, 10, 1), emptyTagsList), + new Task("Lesson 11", TaskType.READING, LocalDate.of(2018, 11, 1), emptyTagsList), + new Task("Lesson 12", TaskType.READING, LocalDate.of(2018, 12, 1), emptyTagsList) + ); + + List result = FindDistinctTask.top5ReadingTaskNames(tasks); + + assertThat(result).isEqualTo(Arrays.asList( + "Lesson 9", + "Lesson 5", + "Lesson 1", + "Lesson 3", + "Lesson 10" + )); + } + + @Test + void shouldReturnCorrectNumberOfTasksByType() { + LocalDate creationDate = LocalDate.of(2018, 1, 1); + List tasks = Arrays.asList( + new Task("Lesson 1", TaskType.READING, LocalDate.of(2018, 1, 1), emptyTagsList), + new Task("Lesson 2", TaskType.WRITING, creationDate, emptyTagsList), + new Task("Lesson 3", TaskType.READING, LocalDate.of(2018, 10, 1), emptyTagsList), + new Task("Lesson 4", TaskType.SPEAKING, creationDate, emptyTagsList), + new Task("Lesson 5", TaskType.READING, LocalDate.of(2017, 7, 1), emptyTagsList), + new Task("Lesson 6", TaskType.WRITING, creationDate, emptyTagsList), + new Task("Lesson 7", TaskType.WRITING, creationDate, emptyTagsList), + new Task("Lesson 8", TaskType.SPEAKING, creationDate, emptyTagsList), + new Task("Lesson 9", TaskType.READING, LocalDate.of(2017, 1, 1), emptyTagsList), + new Task("Lesson 10", TaskType.READING, LocalDate.of(2018, 10, 1), emptyTagsList), + new Task("Lesson 11", TaskType.READING, LocalDate.of(2018, 11, 1), emptyTagsList), + new Task("Lesson 12", TaskType.READING, LocalDate.of(2018, 12, 1), emptyTagsList) + ); + + Long result = FindDistinctTask.countAllTasksByType(tasks, TaskType.READING); + assertThat(result).isEqualTo(7); + } + + @Test + void shouldReturnDistinctListOfTaskTags() { + List tasks = Arrays.asList( + new Task("Lesson 1", TaskType.READING, dummyDate, Arrays.asList("English", "Grammar", "Books")), + new Task("Lesson 2", TaskType.WRITING, dummyDate, Arrays.asList("Spanish", "Grammar")), + new Task("Lesson 3", TaskType.READING, dummyDate, Arrays.asList("History", "Rome")), + new Task("Lesson 4", TaskType.SPEAKING, dummyDate, Arrays.asList("English", "Grammar")) + ); + + List result = FindDistinctTask.getAllDistinctTags(tasks); + assertThat(result).isEqualTo(Arrays.asList( + "English", + "Grammar", + "Books", + "Spanish", + "History", + "Rome" + )); + } + + @Test + void shouldReturnTrueIfAllReadingTasksHaveBooksTag() { + List tasks = Arrays.asList( + new Task("Lesson 1", TaskType.READING, dummyDate, Arrays.asList("English", "Grammar", "Books")), + new Task("Lesson 2", TaskType.WRITING, dummyDate, Arrays.asList("Spanish", "Grammar")), + new Task("Lesson 3", TaskType.READING, dummyDate, Arrays.asList("History", "Rome", "Books")), + new Task("Lesson 4", TaskType.SPEAKING, dummyDate, Arrays.asList("English", "Grammar")) + ); + + Boolean result = FindDistinctTask.isAllReadingTasksWithTagBooks(tasks); + assertThat(result).isTrue(); + } + + @Test + void shouldReturnFalseIfNotAllReadingTasksHaveBooksTag() { + List tasks = Arrays.asList( + new Task("Lesson 1", TaskType.READING, dummyDate, Arrays.asList("English", "Grammar", "Books")), + new Task("Lesson 2", TaskType.WRITING, dummyDate, Arrays.asList("Spanish", "Grammar")), + new Task("Lesson 3", TaskType.READING, dummyDate, Arrays.asList("History", "Rome")), + new Task("Lesson 4", TaskType.SPEAKING, dummyDate, Arrays.asList("English", "Grammar")) + ); + + Boolean result = FindDistinctTask.isAllReadingTasksWithTagBooks(tasks); + assertThat(result).isFalse(); + } + + @Test + void shouldReturnASummaryOfAllTaskTitles() { + List tasks = Arrays.asList( + new Task("Lesson 1", TaskType.READING, dummyDate, emptyTagsList), + new Task("Lesson 2", TaskType.WRITING, dummyDate, emptyTagsList), + new Task("Lesson 3", TaskType.READING, dummyDate, emptyTagsList), + new Task("Lesson 4", TaskType.SPEAKING, dummyDate, emptyTagsList) + ); + + String result = FindDistinctTask.joinAllTaskTitles(tasks); + assertThat(result).isEqualTo("Lesson 1***Lesson 2***Lesson 3***Lesson 4"); + } + + + @Test + void shouldReturnListWithPrefixes() { + String expected = "o1,o5,o7,e8,e100,o1001"; + + String result = FindDistinctTask.evensAndOdds("1, 5, 7,8,100,1001"); + + assertThat(result).isEqualTo(expected); + } + + @Test + void shouldReturnEmptyStringIfNothingIsPassed() { + String expected = ""; + + String result = FindDistinctTask.evensAndOdds(""); + + assertThat(result).isEqualTo(expected); + } + + @Test + void hOnly3LetterWordsStartshouldReturnListWitingWithA() { + List expected = Lists.list("abc", "aaa"); + + List result = FindDistinctTask.searchForThreeLetterWordsStartingWithA(Lists.list("abc", "bbb", "ab", "abcd", "", "aaa")); + + assertThat(result).isEqualTo(expected); + } + +} \ No newline at end of file