Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- import java.util.ArrayList;
- class Book
- {
- private String title;
- private String publisher;
- private int year;
- public Book(String title, String publisher, int year)
- {
- this.title = title;
- this.publisher = publisher;
- this.year = year;
- }
- public String title()
- {
- return title;
- }
- public String publisher()
- {
- return publisher;
- }
- public int year()
- {
- return year;
- }
- @Override
- public String toString()
- {
- return title + ", " + publisher + ", " + year;
- }
- }
- class Library
- {
- List<Book> books = new ArrayList<>();
- public Library()
- {
- }
- public void addBook(Book newBook)
- {
- books.add(newBook);
- }
- public void printBooks()
- {
- for (Book book : books)
- {
- System.out.println(book);
- }
- }
- }
- public class Program
- {
- public static void main(String[] args)
- {
- Library library = new Library();
- Book cheese = new Book("Cheese Problems Solved", "Woodhead Publishing", 2007);
- library.addBook(cheese);
- Book nhl = new Book("NHL Hockey", "Stanley Kupp", 1952);
- library.addBook(nhl);
- library.addBook(new Book("Battle Axes", "Tom A. Hawk", 1851));
- library.printBooks();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement