groovy
class Book {
String title
String author
Book(String title, String author) {
this.title = title
this.author = author
}
}
class Author {
String name
int age
Author(String name, int age) {
this.name = name
this.age = age
}
}
class Library {
List<Book> books
List<Author> authors
Library() {
this.books = []
this.authors = []
}
void addBook(Book book) {
books.add(book)
}
void addAuthor(Author author) {
authors.add(author)
}
void printBooks() {
books.each { book ->
println("Title: " + book.title + ", Author: " + book.author)
}
}
void printAuthors() {
authors.each { author ->
println("Name: " + author.name + ", Age: " + author.age)
}
}
}
def library = new Library()
def book1 = new Book("Book 1", "Author 1")
def book2 = new Book("Book 2", "Author 2")
def author1 = new Author("Author 1", 30)
def author2 = new Author("Author 2", 40)
library.addBook(book1)
library.addBook(book2)
library.addAuthor(author1)
library.addAuthor(author2)
library.printBooks()
library.printAuthors()