Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArtemBut classwork #114

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<Task> allDistinctTasks(List<Task> tasks) {
List<Task> listOfUniqueTasks = tasks.stream()
.distinct()
.collect(Collectors.toList());
return listOfUniqueTasks;
}

// Найти 5 первых (по дате создания) задач по чтению, вернуть имя задачи
public static List<String> top5ReadingTaskNames(List<Task> tasks) {
List<String> 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<Task> tasks, TaskType type) {
long counter = tasks.stream()
.map(Task::getType)
.filter(i->i.equals(type))
.count();
return counter;
}

// Вернуть все уникальные теги задач
public static List<String> getAllDistinctTags(List<Task> tasks) {
List<String> listOfUniqueTags = tasks.stream()
.map(Task::getTags)
.flatMap(tags -> tags.stream())
.distinct()
.collect(Collectors.toList());


return listOfUniqueTags;
}

// Проверить у всех ли задач по чтению есть тег "Books"
public static Boolean isAllReadingTasksWithTagBooks(List<Task> tasks) {
boolean tagExist = tasks.stream()
.filter(i -> i.getType() == TaskType.READING)
.allMatch(i ->i.getTags ().contains ("Books"));
return tagExist;
}

//Сконкатенировать все названия задач в одну надпись через "***"
public static String joinAllTaskTitles(List<Task> 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<String> searchForThreeLetterWordsStartingWithA(List<String> list) {
List<String> listOfThreeLetterWordsStartingWithA = list.stream()
.filter(i -> i.length() == 3)
.filter(j -> j.substring(0,1).equals("a"))
.collect(Collectors.toList());
return listOfThreeLetterWordsStartingWithA;
}


}
Original file line number Diff line number Diff line change
@@ -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<String> tags;

public Task(String name, TaskType type, LocalDate creationDate, List<String> 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<String> 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 +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.hillel.elementary.javageeks.artem.tasks;

enum TaskType {
READING,
WRITING,
SPEAKING
}
Original file line number Diff line number Diff line change
@@ -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<String> emptyTagsList = Collections.emptyList();
private LocalDate dummyDate = LocalDate.of(2018, 12, 1);

@Test
void shouldReturnDistinctListOfTasks() {
List<Task> 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<Task> 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<Task> 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<String> 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<Task> 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<Task> 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<String> result = FindDistinctTask.getAllDistinctTags(tasks);
assertThat(result).isEqualTo(Arrays.asList(
"English",
"Grammar",
"Books",
"Spanish",
"History",
"Rome"
));
}

@Test
void shouldReturnTrueIfAllReadingTasksHaveBooksTag() {
List<Task> 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<Task> 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<Task> 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<String> expected = Lists.list("abc", "aaa");

List<String> result = FindDistinctTask.searchForThreeLetterWordsStartingWithA(Lists.list("abc", "bbb", "ab", "abcd", "", "aaa"));

assertThat(result).isEqualTo(expected);
}

}