Advertisement
Guest User

Untitled

a guest
Aug 4th, 2015
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. public class Book
  2. {
  3. private String title;
  4. private String author;
  5. private String summary;
  6.  
  7. public Book() {
  8. title = "";
  9. author = "";
  10. summary = "";
  11. }
  12.  
  13. public Book(String title, String author, String summary) {
  14. this.title = title;
  15. this.author = author;
  16. this.summary = summary;
  17. }
  18.  
  19. public String getTitle() {
  20. return title;
  21. }
  22.  
  23. public String getAuthor() {
  24. return author;
  25. }
  26.  
  27. public String getSummary() {
  28. return summary;
  29. }
  30. }
  31.  
  32. import java.util.*;
  33.  
  34. public class Library
  35. {
  36. private String location;
  37. private String openingHours;
  38. private Book book;
  39. private Map<Book,Integer> bookList = new HashMap<Book,Integer>();
  40.  
  41. public Library(String location, String openingHours) {
  42. this.location = location;
  43. this.openingHours = openingHours;
  44. }
  45.  
  46. public void addBook(Book book) {
  47. //If the book doesn't exist add it: otherwise increase the quantity of the book
  48. if (bookList.containsKey(book))
  49. bookList.put(book, bookList.get(book) + 1);
  50. else
  51. bookList.put(book, 1);
  52. }
  53.  
  54. public String printOpeningHours() {
  55. return openingHours;
  56. }
  57.  
  58. public String printAddress() {
  59. return location;
  60. }
  61.  
  62. public void borrowBook(Book book) {
  63. bookList.put(book, bookList.get(book) - 1);
  64. }
  65.  
  66. public void returnBook() {
  67. bookList.put(book, bookList.get(book) + 1);
  68. }
  69.  
  70. public String printAvailableBooks() {
  71. ArrayList <String> available = new ArrayList();
  72.  
  73. for (Book books: bookList.keySet())
  74. available.add(books.toString());
  75.  
  76. return available.toString();
  77. }
  78.  
  79. public boolean isBorrowed() {
  80. int value = 0;
  81.  
  82. for (Book bq: bookList.keySet()) {
  83. value = bookList.get(bq);
  84. if (value > 0)
  85. return true;
  86. }
  87. return false;
  88. }
  89.  
  90. public static void main(String[] args) {
  91. Library l1 = new Library("111 Your Street", "9am");
  92. l1.addBook(new Book("Test", "Author", "Test summary"));
  93. l1.addBook(new Book("Test2", "Author2", "Test summary2"));
  94. l1.addBook(new Book("Test3", "Author3", "Test summary3"));
  95. System.out.println(l1.printAvailableBooks());
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement