Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. import java.util.List;
  2. import java.util.ArrayList;
  3.  
  4. class Book
  5. {
  6. private String title;
  7. private String publisher;
  8. private int year;
  9.  
  10. public Book(String title, String publisher, int year)
  11. {
  12. this.title = title;
  13. this.publisher = publisher;
  14. this.year = year;
  15. }
  16.  
  17. public String title()
  18. {
  19. return title;
  20. }
  21.  
  22. public String publisher()
  23. {
  24. return publisher;
  25. }
  26.  
  27. public int year()
  28. {
  29. return year;
  30. }
  31.  
  32. @Override
  33. public String toString()
  34. {
  35. return title + ", " + publisher + ", " + year;
  36. }
  37. }
  38.  
  39. class Library
  40. {
  41. List<Book> books = new ArrayList<>();
  42.  
  43. public Library()
  44. {
  45.  
  46. }
  47.  
  48. public void addBook(Book newBook)
  49. {
  50. books.add(newBook);
  51. }
  52.  
  53. public void printBooks()
  54. {
  55. for (Book book : books)
  56. {
  57. System.out.println(book);
  58. }
  59. }
  60. }
  61.  
  62. public class Program
  63. {
  64. public static void main(String[] args)
  65. {
  66. Library library = new Library();
  67. Book cheese = new Book("Cheese Problems Solved", "Woodhead Publishing", 2007);
  68. library.addBook(cheese);
  69.  
  70. Book nhl = new Book("NHL Hockey", "Stanley Kupp", 1952);
  71. library.addBook(nhl);
  72.  
  73. library.addBook(new Book("Battle Axes", "Tom A. Hawk", 1851));
  74.  
  75. library.printBooks();
  76. }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement