Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.HashMap;
  4.  
  5. /**
  6. * Library to organize books
  7. *
  8. * @author (redacted)
  9. * @version (0.21)
  10. */
  11. public class BookLibrary
  12. {
  13. //ArrayList to store book objects
  14. private ArrayList<Book> library;
  15.  
  16. //Creates a Library
  17. public BookLibrary()
  18. {
  19. library = new ArrayList<>();
  20. }
  21.  
  22. public void addBook(Book book)
  23. {
  24. library.add(book);
  25. }
  26.  
  27. //Adds book to library
  28. public void addBook(String author, String title,String publisher,int pages, int year,String ean,Boolean rented)
  29. {
  30. library.add(new Book(title,author,year,publisher,pages,ean,rented));
  31. }
  32.  
  33. //Searches through the library and returns first book matching the string "title"
  34. public Book findBook(String title)
  35. {
  36. boolean notFound = true;
  37. Iterator<Book> it = library.iterator();
  38. Book book = null;
  39. while(it.hasNext()&& notFound)
  40. {
  41. Book b = it.next();
  42.  
  43. if(b.getTitle().toLowerCase().equals(title.toLowerCase()))
  44. {
  45. book = b;
  46. notFound = false;
  47. }
  48. }
  49. return book;
  50. }
  51.  
  52. public Book findBookByEan(String ean)
  53. {
  54. boolean notFound = true;
  55. Iterator<Book> it = library.iterator();
  56. Book book = null;
  57. while(it.hasNext()&& notFound)
  58. {
  59. Book b = it.next();
  60.  
  61. if(b.getEan().toLowerCase().equals(ean.toLowerCase()))
  62. {
  63. book = b;
  64. notFound = false;
  65. }
  66. }
  67. return book;
  68. }
  69. //Goes down the library and returns all books where the author matches the string "author" in a HashMap
  70. public HashMap booksFromAuthor(String author)
  71. {
  72. HashMap<Double,Book> collection = new HashMap<>();
  73. Iterator<Book> it = library.iterator();
  74. double index = 1;
  75. while(it.hasNext())
  76. {
  77. Book b = it.next();
  78. if(b.getAuthor().toLowerCase().equals(author))
  79. {
  80. collection.put(index,b);
  81. index++;
  82. }
  83. }
  84. return collection;
  85. }
  86.  
  87. public void removeBook(String title)
  88. {
  89. Iterator<Book> it = library.iterator();
  90. boolean notFound =true;
  91. while(it.hasNext() && notFound)
  92. {
  93. Book b = it.next();
  94. if(b.getTitle().toLowerCase().equals(title))
  95. {library.remove(b);
  96. notFound= false;
  97. }
  98. }
  99. }
  100.  
  101. //Returns iterator to go through the library
  102. public Iterator<Book> getIterator()
  103. {
  104. Iterator <Book> it = library.iterator();
  105. return it;
  106. }
  107.  
  108. //Goes down the library and returns the first book with a matching title
  109. public int findIndexByName(String title)
  110. {
  111. boolean notFound = true;
  112. int ind = 0;
  113. int bookIndex = 9999999;
  114.  
  115. while(notFound == true && ind < library.size())
  116. {
  117. Book book = library.get(ind);
  118.  
  119. if(title.equals(book.getTitle()))
  120. {
  121. notFound = false;
  122. bookIndex = ind;
  123. }
  124. ind++;
  125. }
  126. return bookIndex;
  127. }
  128.  
  129. //Returns title of book with index "index"
  130. public void getTitle(int index)
  131. {
  132. Book book = library.get(index);
  133. System.out.println(book.getTitle());
  134. }
  135.  
  136. //Returns author of book with index "index"
  137. public void getAuthor(int index)
  138. {
  139. Book book = library.get(index);
  140. System.out.println(book.getAuthor());
  141. }
  142.  
  143. //Returns year of book with index "index"
  144. public void getYear(int index)
  145. {
  146. Book book = library.get(index);
  147. System.out.println(book.getYear());
  148. }
  149.  
  150. //Returns pages of book with index "index"
  151. public void getPages(int index)
  152. {
  153. Book book = library.get(index);
  154. System.out.println(book.getPages());
  155. }
  156.  
  157. //Returns publisher of book with index "index"
  158. public void getPublisher(int index)
  159. {
  160. Book book = library.get(index);
  161. System.out.println(book.getPublisher());
  162. }
  163.  
  164.  
  165. //Prints all books in the library
  166. public void listAllBooks()
  167. {
  168. Iterator<Book> it = library.iterator();
  169.  
  170. while(it.hasNext())
  171. {
  172. Book book = it.next();
  173. System.out.println(book.getTitle());
  174. }
  175. }
  176.  
  177. //Adds 6 books to the library for testing
  178. public void tester()
  179. {
  180. library.add(new Book("The Hunger Games", "Suzanne Collins", 2008,"Scholastic Corporation" , 374,"1627384950843",false));
  181. library.add(new Book("Gone", "Michael Grant", 2008, "HarperCollins", 576,"1029384756648",false));
  182. library.add(new Book("Harry Potter", "J. K. Rowling", 1997,"Schooltastic" , 309,"6574839201985",false));
  183. library.add(new Book("BlueJ", "David J. Barnes and Michael Kölling", 2003,"Pearson" , 630,"1920384756123",false));
  184. library.add(new Book("The Hobbit", "J.R.R. Tolkien", 1937,"Allen & Unwin" , 310,"1230987654321",false));
  185. library.add(new Book("S","Michael Grant",12,"",1, "1234567890123",false));
  186. }
  187.  
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement