chore: organize homework into first-part directory

This commit is contained in:
user
2026-09-12 17:46:39 +04:00
parent cd4bc632ed
commit 58459cc92b
80 changed files with 0 additions and 0 deletions
@@ -0,0 +1,71 @@
package com.example;
import java.io.IOException;
import java.nio.file.Path;
import java.time.Year;
import java.util.Comparator;
import java.util.List;
public class App {
public static void main(String[] args) {
try {
Path studentsJsonFile = Path.of("students.json");
List<Student> students = StudentReader.read(studentsJsonFile);
// Условие использования одного stream сохраняется
students.stream()
// Вывести в консоль каждого студента (переопределите toString)
.peek(System.out::println)
// Получить для каждого студента список книг
.map(s -> s.getBooks()) // Stream<List<Book>>
// Получить книги
.flatMap(List::stream) // Stream<Book>;
// Oтсортировать книги по количеству страниц (Не забывайте про условия для сравнения
// объектов)
.sorted(Comparator.comparingInt(Book::getPageCount))
// Оставить только уникальные книги
.distinct()
// Отфильтровать книги, оставив только те, которые были выпущены после 2000 года
.filter(b -> b.getPublishYear().isAfter(Year.of(2000)))
// Ограничить стрим на 3 элементах
.limit(3)
// Получить из книг годы выпуска
.map(Book::getPublishYear)
// При помощи методов короткого замыкания (почитайте самостоятельно что это такое) вернуть
// Optional от года
.findFirst()
// При помощи методов получения значения из Optional вывести в консоль год выпуска
// найденной книги, либо запись о том, что такая книга отсутствует
.ifPresentOrElse(
year -> System.out.println("Год выпуска: " + year),
() -> System.out.println("Подходящая книга отсутствует"));
} catch (IOException e) {
System.err.println("Сударь, вы передали неправильный файл!");
e.printStackTrace();
System.exit(1);
} catch (Exception e) {
System.err.println("Ну тут мои полномочии всё!");
e.printStackTrace();
System.exit(1);
}
System.out.println("Выполнение программы завершено");
}
}
@@ -0,0 +1,72 @@
package com.example;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.Year;
import java.util.Objects;
public class Book {
@JsonCreator
public Book(
@JsonProperty("author") String author,
@JsonProperty("title") String title,
@JsonProperty("publishYear") Year publishYear,
@JsonProperty("pageCount") int pageCount) {
this.author = author;
this.title = title;
this.publishYear = publishYear;
this.pageCount = pageCount;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof Book book)) {
return false;
}
return pageCount == book.pageCount
&& author.equals(book.author)
&& title.equals(book.title)
&& publishYear.equals(book.publishYear);
}
@Override
public int hashCode() {
return Objects.hash(author, title, publishYear, pageCount);
}
@Override
public String toString() {
return "Book{"
+ "author='"
+ author
+ '\''
+ ", title='"
+ title
+ '\''
+ ", publishYear="
+ publishYear
+ ", pageCount="
+ pageCount
+ '}';
}
public int getPageCount() {
return pageCount;
}
public Year getPublishYear() {
return publishYear;
}
private final String author;
private final String title;
private final Year publishYear;
private final int pageCount;
}
@@ -0,0 +1,47 @@
package com.example;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import java.util.List;
public class Student {
@JsonCreator
public Student(
@JsonProperty("name") String name,
@JsonProperty("surname") String surname,
@JsonProperty("books") List<Book> books) {
this.name = name;
this.surname = surname;
this.books = List.copyOf(books);
}
@Override
public String toString() {
StringBuilder booksString = new StringBuilder();
for (Book book : books) {
booksString.append("\n ").append(book);
}
return "Student{\n"
+ " name='"
+ name
+ "'\n"
+ " surname='"
+ surname
+ "'\n"
+ " books="
+ booksString
+ "\n}";
}
public List<Book> getBooks() {
return books;
}
private final String name;
private final String surname;
private final List<Book> books;
}
@@ -0,0 +1,20 @@
package com.example;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import java.io.IOException;
import java.nio.file.Path;
import java.util.List;
public final class StudentReader {
public static List<Student> read(Path path) throws IOException {
return MAPPER.readValue(path.toFile(), new TypeReference<List<Student>>() {});
}
private StudentReader() {}
private static final ObjectMapper MAPPER =
new ObjectMapper().registerModule(new JavaTimeModule());
}