feat(08-btrbooks): Добавлено задание
This commit is contained in:
48
labs/08-btrbooks/app/build.gradle.kts
Normal file
48
labs/08-btrbooks/app/build.gradle.kts
Normal file
@ -0,0 +1,48 @@
|
||||
/*
|
||||
* 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 {
|
||||
// Use JUnit Jupiter for testing.
|
||||
testImplementation(libs.junit.jupiter)
|
||||
|
||||
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||
|
||||
// This dependency is used by the application.
|
||||
implementation(libs.guava)
|
||||
}
|
||||
|
||||
// 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.btrbooks.App"
|
||||
}
|
||||
|
||||
tasks.named<Test>("test") {
|
||||
// Use JUnit Platform for unit tests.
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
tasks.named<JavaExec>("run") {
|
||||
standardInput = System.`in`
|
||||
}
|
||||
|
||||
@ -0,0 +1,25 @@
|
||||
package ru.mrqiz.btrbooks;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
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.read(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();
|
||||
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,65 @@
|
||||
package ru.mrqiz.btrbooks;
|
||||
|
||||
public class Book {
|
||||
private String author;
|
||||
private String name;
|
||||
private int year;
|
||||
private int id;
|
||||
private int price;
|
||||
|
||||
public Book() {}
|
||||
|
||||
public Book(String author, String name, int year, int id) {
|
||||
this.author = author;
|
||||
this.name = name;
|
||||
this.year = year;
|
||||
this.id = id;
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getYear() {
|
||||
return year;
|
||||
}
|
||||
|
||||
public void setYear(int year) {
|
||||
this.year = year;
|
||||
}
|
||||
|
||||
public int getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(int price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ID: " + id + ", Author: " + author + ", Name: " + name + ", Year: " + year;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,69 @@
|
||||
package ru.mrqiz.btrbooks;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class Books {
|
||||
private List<Book> bookList;
|
||||
|
||||
public Books() {
|
||||
this.bookList = new ArrayList<>();
|
||||
}
|
||||
|
||||
public List<Book> getBookList() {
|
||||
return bookList;
|
||||
}
|
||||
|
||||
public void setBookList(List<Book> bookList) {
|
||||
this.bookList = bookList;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Books{" +
|
||||
"bookList=" + bookList +
|
||||
'}';
|
||||
}
|
||||
|
||||
public String toJson() {
|
||||
StringBuilder jsonBuilder = new StringBuilder();
|
||||
jsonBuilder.append("[");
|
||||
|
||||
for (int i = 0; i < bookList.size(); i++) {
|
||||
Book book = bookList.get(i);
|
||||
jsonBuilder.append("{");
|
||||
jsonBuilder.append("\"id\": ").append(book.getId()).append(", ");
|
||||
jsonBuilder.append("\"author\": \"").append(book.getAuthor()).append("\", ");
|
||||
jsonBuilder.append("\"name\": \"").append(book.getName()).append("\", ");
|
||||
jsonBuilder.append("\"year\": ").append(book.getYear()).append(", ");
|
||||
jsonBuilder.append("\"price\": ").append(book.getPrice());
|
||||
jsonBuilder.append("}");
|
||||
|
||||
if (i < bookList.size() - 1) {
|
||||
jsonBuilder.append(", ");
|
||||
}
|
||||
}
|
||||
|
||||
jsonBuilder.append("]");
|
||||
jsonBuilder.append("");
|
||||
|
||||
return jsonBuilder.toString();
|
||||
}
|
||||
|
||||
public String toCsv() {
|
||||
StringBuilder csvBuilder = new StringBuilder();
|
||||
csvBuilder.append("ID,Author,Name,Year,Price\n");
|
||||
|
||||
for (Book book : bookList) {
|
||||
csvBuilder.append(book.getId()).append(",")
|
||||
.append(book.getAuthor()).append(",")
|
||||
.append(book.getName()).append(",")
|
||||
.append(book.getYear()).append(",")
|
||||
.append(book.getPrice()).append("\n");
|
||||
}
|
||||
|
||||
return csvBuilder.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -0,0 +1,73 @@
|
||||
package ru.mrqiz.btrbooks;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.io.*;
|
||||
|
||||
public class BooksParser {
|
||||
|
||||
public Books read(String filename) {
|
||||
StringBuilder content = new StringBuilder();
|
||||
|
||||
try (BufferedReader br = new BufferedReader(new FileReader(filename))) {
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
content.append(line).append(System.lineSeparator());
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
String fileContent = content.toString();
|
||||
return parseBooks(fileContent);
|
||||
}
|
||||
|
||||
private Books parseBooks(String json) {
|
||||
Books books = new Books();
|
||||
String[] bookEntries = json.split("\\},\\{");
|
||||
|
||||
for (String entry : bookEntries) {
|
||||
entry = entry.replaceAll("\\{\\}|\\$\\$\\$|\"", "").trim();
|
||||
String[] fields = entry.split(",");
|
||||
|
||||
String author = null;
|
||||
String name = null;
|
||||
int year = 0;
|
||||
int id = 0;
|
||||
int price = 0;
|
||||
|
||||
for (String field : fields) {
|
||||
String[] keyValue = field.replaceAll("\\[\\{", "").replaceAll("\\}\\]", "").split(":");
|
||||
if (keyValue.length == 2) {
|
||||
String key = keyValue[0].trim();
|
||||
String value = keyValue[1].trim();
|
||||
|
||||
switch (key) {
|
||||
case "author":
|
||||
author = value;
|
||||
break;
|
||||
case "name":
|
||||
name = value;
|
||||
break;
|
||||
case "year":
|
||||
year = Integer.parseInt(value.replaceAll("\\D+", ""));
|
||||
break;
|
||||
case "id":
|
||||
id = Integer.parseInt(value.replaceAll("\\D+", ""));
|
||||
break;
|
||||
case "price":
|
||||
price = Integer.parseInt(value.replaceAll("\\D+", ""));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Book book = new Book(author, name, year, id);
|
||||
book.setPrice(price);
|
||||
books.getBookList().add(book);
|
||||
}
|
||||
|
||||
return books;
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,31 @@
|
||||
package ru.mrqiz.btrbooks;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.io.IOException;
|
||||
|
||||
public class BooksSaver {
|
||||
public static void saveToFile(String filename, Books books, String fmt) {
|
||||
String content = convert(books, fmt);
|
||||
Path path = Paths.get(filename);
|
||||
try {
|
||||
Files.write(path, content.getBytes());
|
||||
System.out.println("ok.");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private static String convert(Books books, String fmt) {
|
||||
switch (fmt.toLowerCase()) {
|
||||
case "json":
|
||||
return books.toJson();
|
||||
case "csv":
|
||||
return books.toCsv();
|
||||
default:
|
||||
throw new IllegalArgumentException("idk.." + fmt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,49 @@
|
||||
package ru.mrqiz.btrbooks;
|
||||
|
||||
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("ls", new DisplayAllBooks());
|
||||
commands.put("rm", new DeleteBook(scanner));
|
||||
commands.put("save", new SaveBooks(scanner));
|
||||
commands.put("edit", new EditBook(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..");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,6 @@
|
||||
package ru.mrqiz.btrbooks;
|
||||
|
||||
public interface CLICommand {
|
||||
void execute(Books books);
|
||||
}
|
||||
|
||||
@ -0,0 +1,32 @@
|
||||
package ru.mrqiz.btrbooks;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class DeleteBook implements CLICommand {
|
||||
private final Scanner scanner;
|
||||
|
||||
public DeleteBook(Scanner scanner) {
|
||||
this.scanner = scanner;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(Books books) {
|
||||
System.out.println("gimme id:");
|
||||
String input = scanner.nextLine();
|
||||
|
||||
List<Book> bookList = books.getBookList();
|
||||
Book bookToDelete = bookList.stream()
|
||||
.filter(book -> String.valueOf(book.getId()).equals(input))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
|
||||
if (bookToDelete != null) {
|
||||
bookList.remove(bookToDelete);
|
||||
System.out.println("cancelled " + input + " in xwitter");
|
||||
} else {
|
||||
System.out.println("no book found - id=" + input);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,11 @@
|
||||
package ru.mrqiz.btrbooks;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
public class DisplayAllBooks implements CLICommand {
|
||||
@Override
|
||||
public void execute(Books books) {
|
||||
books.getBookList().stream().forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,76 @@
|
||||
package ru.mrqiz.btrbooks;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class EditBook implements CLICommand {
|
||||
private final Scanner scanner;
|
||||
|
||||
public EditBook(Scanner scanner) {
|
||||
this.scanner = scanner;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(Books books) {
|
||||
System.out.println("id?");
|
||||
String input = scanner.nextLine();
|
||||
|
||||
List<Book> bookList = books.getBookList();
|
||||
Book bookToEdit = bookList.stream()
|
||||
.filter(book -> String.valueOf(book.getId()).equals(input))
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
|
||||
if (bookToEdit != null) {
|
||||
System.out.println("k got the " + bookToEdit);
|
||||
System.out.println("author? (leave blank to keep current):");
|
||||
String newAuthor = scanner.nextLine();
|
||||
if (!newAuthor.isEmpty()) {
|
||||
bookToEdit.setAuthor(newAuthor);
|
||||
}
|
||||
|
||||
System.out.println("name? (leave blank to keep current):");
|
||||
String newName = scanner.nextLine();
|
||||
if (!newName.isEmpty()) {
|
||||
bookToEdit.setName(newName);
|
||||
}
|
||||
|
||||
int newYear = bookToEdit.getYear();
|
||||
System.out.println("year? (leave blank to keep current):");
|
||||
String yearInput = scanner.nextLine();
|
||||
if (!yearInput.isEmpty()) {
|
||||
while (!isValidYear(yearInput)) {
|
||||
System.out.println("thats not a year.. gimme year:");
|
||||
yearInput = scanner.nextLine();
|
||||
}
|
||||
newYear = Integer.parseInt(yearInput);
|
||||
}
|
||||
bookToEdit.setYear(newYear);
|
||||
|
||||
int newPrice = bookToEdit.getPrice();
|
||||
System.out.println("price? (leave blank to keep current):");
|
||||
String priceInput = scanner.nextLine();
|
||||
if (!priceInput.isEmpty()) {
|
||||
while (!isValidPrice(priceInput)) {
|
||||
System.out.println("thats not a price.. gimme price:");
|
||||
priceInput = scanner.nextLine();
|
||||
}
|
||||
newPrice = Integer.parseInt(priceInput);
|
||||
}
|
||||
bookToEdit.setPrice(newPrice);
|
||||
|
||||
System.out.println("k saved!");
|
||||
} else {
|
||||
System.out.println("umm got no book id=" + input);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isValidYear(String input) {
|
||||
return input.matches("^(19|20)\\d{2}$");
|
||||
}
|
||||
|
||||
private boolean isValidPrice(String input) {
|
||||
return input.matches("^\\d+(\\.\\d{1,2})?$");
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,30 @@
|
||||
package ru.mrqiz.btrbooks;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class SaveBooks implements CLICommand {
|
||||
private final Scanner scanner;
|
||||
|
||||
public SaveBooks(Scanner scanner) {
|
||||
this.scanner = scanner;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(Books books) {
|
||||
System.out.println("gimme format:");
|
||||
String fmt = scanner.nextLine();
|
||||
System.out.println("gimme path:");
|
||||
String path = scanner.nextLine();
|
||||
|
||||
List<Book> bookList = books.getBookList();
|
||||
|
||||
try {
|
||||
(new BooksSaver()).saveToFile(path, books, fmt);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user