package aufgabe1; import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; import java.util.Optional; public record BookDatabase(List books) { public static BookDatabase readFromFile(File file) { if (!file.exists() || !file.canRead() || file.isDirectory()) throw new IllegalArgumentException("Cannot read from file"); try { var source = Files.readString(Paths.get(file.getPath()), StandardCharsets.UTF_8); var books = source.lines().skip(1).map(Book::parseFromCommaSeparatedString).toList(); return new BookDatabase(books); } catch (ArrayIndexOutOfBoundsException | IOException e) { throw new IllegalStateException("Cannot read from file: " + e); } } public Optional getBook(String title) { return books.stream().filter(book -> book.title().equals(title)).findFirst(); } public List getTitles() { return books.stream().map(Book::title).toList(); } }