Library Book Management with Classes
Kotlin
Medium
classes
Task «Library Book Management with Classes»
Create a class `Book` with fields `title`, `author`, and `year`. Implement a class `Library` that stores a list of books and provides methods `addBook(book: Book)` to add a book, `findBooksByAuthor(author: String): List<Book>` to find all books by a given author, and `getBooksAfterYear(year: Int): List<Book>` to get books published after the specified year. Input data: first, an integer N — the number of operations. Then N lines, each containing a command: `ADD <title> <author> <year>` — add a book; `FIND_AUTHOR <author>` — output the titles of books by this author, separated by commas, in the order they were added; `FIND_YEAR <year>` — output the titles of books published after the specified year, separated by commas, in the order they were added. If no books are found, output "Not found".
Input Format
Integer N (1 ≤ N ≤ 1000). Then N lines with commands.
Output Format
For each FIND_AUTHOR or FIND_YEAR command — a string with the result (book titles separated by commas) or "Not found".
Examples
Example 1
INPUT
3
ADD Мастер и Маргарита Булгаков 1967
ADD Собачье сердце Булгаков 1925
FIND_AUTHOR Булгаков
OUTPUT
Мастер и Маргарита, Собачье сердце
Example 2
INPUT
4
add TheHobbit J.R.R.Tolkien 1937
add TheLordOfTheRings J.R.R.Tolkien 1954
find J.R.R.Tolkien
borrow TheHobbit
OUTPUT
Example 3
INPUT
4
ADD 1984 Оруэлл 1949
ADD Скотный двор Оруэлл 1945
FIND_YEAR 1946
FIND_AUTHOR Оруэлл
OUTPUT
1984
1984, Скотный двор
main.kt