feat(07-books): Добавлено задание

This commit is contained in:
Mark Zheleznyakov
2024-11-08 00:44:40 +03:00
parent 12a30b0e80
commit 892df53f8e
21 changed files with 788 additions and 0 deletions

View File

@ -0,0 +1,46 @@
/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.10.2/userguide/building_java_projects.html in the Gradle documentation.
*/
plugins {
// Apply the application plugin to add support for building a CLI application in Java.
application
}
repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}
dependencies {
// This dependency is used by the application.
implementation(libs.guava)
implementation("jakarta.xml.bind:jakarta.xml.bind-api:4.0.2")
implementation("org.glassfish.jaxb:jaxb-runtime:4.0.2")
}
// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(23)
}
}
application {
// Define the main class for the application.
mainClass = "ru.mrqiz.books.App"
}
tasks.named<Test>("test") {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
}
tasks.named<JavaExec>("run") {
standardInput = System.`in`
}

View File

@ -0,0 +1,23 @@
package ru.mrqiz.books;
public class App {
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("hmm noo filename go touch grass");
return;
}
String filename = args[0];
BooksParser parser = new BooksParser();
Books books = parser.parse(filename);
if (books == null || books.getBookList().isEmpty()) {
System.out.println("hmm nothing in the books.. go read some");
return;
}
CLI cli = new CLI(books);
cli.start();
}
}

View File

@ -0,0 +1,64 @@
package ru.mrqiz.books;
import jakarta.xml.bind.annotation.XmlAttribute;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "book")
public class Book {
private String author;
private String name;
private int year;
private int id;
public Book() {}
public Book(String author, String name, int year, int id) {
this.author = author;
this.name = name;
this.year = year;
this.id = id;
}
@XmlAttribute
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
@Override
public String toString() {
return "ID: " + id + ", Author: " + author + ", Name: " + name + ", Year: " + year;
}
}

View File

@ -0,0 +1,20 @@
package ru.mrqiz.books;
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
import java.util.List;
@XmlRootElement(name = "books")
public class Books {
private List<Book> bookList;
@XmlElement(name = "book")
public List<Book> getBookList() {
return bookList;
}
public void setBookList(List<Book> bookList) {
this.bookList = bookList;
}
}

View File

@ -0,0 +1,29 @@
package ru.mrqiz.books;
import jakarta.xml.bind.JAXBContext;
import jakarta.xml.bind.JAXBException;
import jakarta.xml.bind.Unmarshaller;
import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
public class BooksParser {
private static final Logger logger = Logger.getLogger(BooksParser.class.getName());
public static Books parse(String filename) {
try {
File file = new File(filename);
JAXBContext jaxbContext = JAXBContext.newInstance(Books.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
return (Books) jaxbUnmarshaller.unmarshal(file);
} catch (JAXBException e) {
logger.log(Level.SEVERE, "JAXBException occurred while parsing the file: " + filename, e);
} catch (Exception e) {
logger.log(Level.SEVERE, "An error occurred while parsing the file: " + filename, e);
}
return null;
}
}

View File

@ -0,0 +1,49 @@
package ru.mrqiz.books;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class CLI {
private final Books books;
private final Scanner scanner;
private final Map<String, CLICommand> commands;
public CLI(Books books) {
this.books = books;
this.scanner = new Scanner(System.in);
this.commands = new HashMap<>();
initializeCommands();
}
private void initializeCommands() {
commands.put("display", new DisplayAllBooks());
commands.put("almost_fulltext_search", new SearchBooks(scanner));
commands.put("display_pairs", new DisplaySortedPairs(scanner));
commands.put("date_search", new SearchByDateRange(scanner));
commands.put("ggwp", null);
}
public void start() {
while (true) {
System.out.println("\npick one:");
commands.keySet().forEach(key -> System.out.println(key));
System.out.print("> ");
String choice = scanner.nextLine().trim().toLowerCase();
if (choice == "ggwp") {
System.out.println("kthxbye..");
return;
}
CLICommand command = commands.get(choice);
if (command != null) {
command.execute(books);
} else {
System.out.println("what the command..");
}
}
}
}

View File

@ -0,0 +1,6 @@
package ru.mrqiz.books;
public interface CLICommand {
void execute(Books books);
}

View File

@ -0,0 +1,11 @@
package ru.mrqiz.books;
import java.util.stream.Stream;
public class DisplayAllBooks implements CLICommand {
@Override
public void execute(Books books) {
books.getBookList().stream().forEach(System.out::println);
}
}

View File

@ -0,0 +1,38 @@
package ru.mrqiz.books;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class DisplaySortedPairs implements CLICommand {
private final Scanner scanner;
public DisplaySortedPairs(Scanner scanner) {
this.scanner = scanner;
}
@Override
public void execute(Books books) {
System.out.print("u wanna sort by author or name\n> ");
String choice = scanner.nextLine().trim().toLowerCase();
List<String> sortedPairs;
if (choice.equals("author")) {
sortedPairs = books.getBookList().stream()
.sorted((b1, b2) -> b1.getAuthor().compareTo(b2.getAuthor()))
.map(book -> book.getAuthor() + " + " + book.getName())
.collect(Collectors.toList());
} else if (choice.equals("name")) {
sortedPairs = books.getBookList().stream()
.sorted((b1, b2) -> b1.getName().compareTo(b2.getName()))
.map(book -> book.getAuthor() + " + " + book.getName())
.collect(Collectors.toList());
} else {
System.out.println("what the choice..");
return;
}
sortedPairs.forEach(System.out::println);
}
}

View File

@ -0,0 +1,29 @@
package ru.mrqiz.books;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class SearchBooks implements CLICommand {
private final Scanner scanner;
public SearchBooks(Scanner scanner) {
this.scanner = scanner;
}
@Override
public void execute(Books books) {
System.out.println("gimme author name or year:");
String input = scanner.nextLine();
List<Book> foundBooks = books.getBookList().stream()
.filter(book -> book.getAuthor().equalsIgnoreCase(input) || String.valueOf(book.getYear()).equals(input))
.collect(Collectors.toList());
if (foundBooks.isEmpty()) {
System.out.println("no books found");
} else {
foundBooks.forEach(System.out::println);
}
}
}

View File

@ -0,0 +1,32 @@
package ru.mrqiz.books;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
public class SearchByDateRange implements CLICommand {
private final Scanner scanner;
public SearchByDateRange(Scanner scanner) {
this.scanner = scanner;
}
@Override
public void execute(Books books) {
System.out.println("gimme start year:");
int startYear = scanner.nextInt();
System.out.println("gimme end year:");
int endYear = scanner.nextInt();
List<Book> foundBooks = books.getBookList().stream()
.filter(book -> book.getYear() >= startYear && book.getYear() <= endYear)
.collect(Collectors.toList());
if (foundBooks.isEmpty()) {
System.out.println("no books found");
} else {
foundBooks.forEach(System.out::println);
}
}
}