Advertisement
Guest User

Untitled

a guest
Jun 20th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 KB | None | 0 0
  1. package day58;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. /*While writing his next book George wanted to program a digital representation of his book
  7. * shelf, he already made a book class so he will be able to save the books title, author name,
  8. * id, number of pages on the same object and manipulate it easily even if he has thousands
  9. * of books. Instead of using an array to hold the books he decided to put it in a class
  10. * called Shelf representing a book shelf. Shelf has a List of Book objects. Using static
  11. * methods of Shelf class, George can add, remove, read, get title etc of the books.
  12. * */
  13.  
  14. public class Shelf {
  15. /**
  16. * Constructor is made private to prevent any instantiation. Since all methods
  17. * in this class are static that can be called without an object
  18. */
  19. private Shelf() {
  20. // nothing todo here.
  21. }
  22.  
  23. /**
  24. * List<Book> theBooks will hold all book object in the shelf in form of a List
  25. * of Book objects
  26. */
  27. private static List<Book> theBooks = new ArrayList<>();
  28.  
  29. /**
  30. * public static getter method for theBooks list
  31. *
  32. * @return theBooks
  33. */
  34. public static List<Book> getTheBooks() {
  35.  
  36. return theBooks;
  37. }
  38.  
  39. /**
  40. * isShelfEmpty is used to see if shelf is empty or not. Shelf is empty when
  41. * List<Book> theBooks has no Book objects added
  42. *
  43. * @return true if list is empty or false if not
  44. */
  45. public static boolean isShelfEmpty() {
  46.  
  47. boolean result = false;
  48.  
  49. if (!theBooks.isEmpty()) {
  50. result = true;
  51. }
  52. return result;
  53. }
  54.  
  55. /**
  56. * isIDUnique accepts an id and loops though each Book object in theBooks list
  57. * ,and if id was already assigned to another book, it will return false, If id
  58. * was never used, it will return true
  59. *
  60. * @param id
  61. * @return true or false
  62. */
  63. public static boolean isIDUnique(int id) {
  64. boolean result = true;
  65. for (int i = 0; i < theBooks.size(); i++) {
  66. if (theBooks.get(i).getId() == id) {
  67. result = false;
  68. break;
  69. }
  70. }
  71. return result;
  72.  
  73. }
  74.  
  75. /**
  76. * static addBook method is used to add a book to the Shelf. to List<Book>
  77. * theBooks
  78. *
  79. * @param id
  80. * @param author
  81. * @param title
  82. * @param pages
  83. * @returns true if Book was successfully added or false if not Conditions:
  84. * returns false and does NOT add a book to the list if:
  85. * - id is not unique. was already used by other books. Call isIDUnique to check that
  86. * - title is empty
  87. * - pages <= 0
  88. * returns true and adds a new Book object to List<Book> theBooks if:
  89. * - all above checks passed.
  90. * Shelf.addBook(100,"j.K. rowLing","harry potter",556); => returns true
  91. * and adds the book to the list: theBooks.add(new Book(id,author,title,pages));
  92. */
  93. public static boolean addBook(int id, String author, String title, int pages) {
  94.  
  95. boolean result = false;
  96. if (isIDUnique(id) && !title.isEmpty() && pages > 0) {
  97.  
  98. theBooks.add(new Book(id, author, title, pages));
  99. result = true;
  100. }
  101.  
  102. return result;
  103. }
  104.  
  105. /**
  106. * static addBook method is used to add a book to the Shelf. to List<Book> theBooks
  107. * @param book object
  108. * @return true if Book was successfully added or false if not
  109. Conditions:
  110. * returns false and does NOT add a book to the list if:
  111. * - book.getId() is not unique. was already used by other books. Call isIDUnique to check that
  112. * - book's title is empty
  113. * - book's pages <= 0
  114. * returns true and adds the Book object to List<Book> theBooks if:
  115. * - all above checks passed.
  116. *
  117. * Book book1, book2;
  118. * book1 = new Book(44, "shel silverstein" ,"the Giving tRee",532);
  119. * book2 = new Book(434, "Dan Brown" ,"the da Vinci coDe",1532);
  120. *
  121. * Shelf.addBook(book1); => true
  122. * Shelf.addBook(book1); => true
  123. *
  124. */
  125. public static boolean addBook(Book book) {
  126.  
  127. boolean result = false;
  128. if (isIDUnique(book.getId()) && !book.getTitle().isEmpty() && book.getPages() > 0) {
  129.  
  130. theBooks.add(book);
  131. result = true;
  132. }
  133.  
  134. return result;
  135. }
  136.  
  137. /**
  138. * static getTitleByID method looks up title of the Book by id
  139. * @param id
  140. * @return title
  141. * Loop through each book in Shelf (the theBooks list) and if you find a book
  142. * matching id then return that book's title
  143. * Ex:
  144. * Assume we have Book id=100, title=Start With Why
  145. * Shelf.getTitleByID(100); => "Start With Why"
  146. * Shelf.getTitleByID(100534534); => "book not found"
  147. */
  148. public static String getTitleByID(int id) {
  149. String title = "book not found";
  150. for (int i = 0; i < theBooks.size(); i++) {
  151. if (theBooks.get(i).getId() == id) {
  152. title = theBooks.get(i).getTitle();
  153. }
  154. }
  155. return title;
  156. }
  157.  
  158. /**
  159. * static method findBookByPartialTitle
  160. * Looks for a book and if title parameter matches partially
  161. * return that book object.
  162. * @param title: partial string to match
  163. * @return Book object
  164. *
  165. * Assume we have Book id=100, title=Start With Why, Author: Simon Sinek
  166. * Shelf.findBookByPartialTitle("start"); => returns that Book object
  167. * Shelf.findBookByPartialTitle("why"); => returns that Book object
  168. * Shelf.findBookByPartialTitle("Wooden Spoon); returns null
  169. *
  170. */
  171. public static Book findBookByPartialTitle(String title) {
  172.  
  173. Book object = null;
  174. for (int i = 0; i < theBooks.size(); i++) {
  175. if (theBooks.get(i).getTitle().toLowerCase().contains(title.toLowerCase())) {
  176. object = theBooks.get(i);
  177. }
  178. }
  179. return object;
  180. }
  181.  
  182. /**
  183. * static method getAllBooksForAuthor.
  184. * Loops through each book in shelf(theBooks list) and returns
  185. * a new List<Book> objects for that author.
  186. * If no book is found for that author, returns an empty list
  187. * @param author
  188. * @return List<Book> that belongs to that Author
  189. */
  190. public static List<Book> getAllBooksForAuthor(String author) {
  191. List<Book> specified = new ArrayList<Book>();
  192. for (int i = 0; i < theBooks.size(); i++) {
  193. if (theBooks.get(i).getAuthor().toLowerCase().contains(author.toLowerCase())) {
  194. specified.add(theBooks.get(i));
  195. }
  196. }
  197. return specified;
  198. }
  199.  
  200. /**
  201. * static removeBook method removes the Book from the Shelf(theBooks list)
  202. *
  203. * @param id Look for the book in the theBooks list that matches the id and
  204. * remove it if there is not match, no action needed
  205. */
  206. public static void removeBook(int id) {
  207. for (int i = 0; i < theBooks.size(); i++) {
  208. if (theBooks.get(i).getId() == id) {
  209. theBooks.remove(i);
  210. }
  211. }
  212. }
  213.  
  214. /**
  215. * static removeBook method removes the Book from the Shelf(theBooks list)
  216. *
  217. * @param author Look for the book in the theBooks list that matches the author
  218. * and remove ALL of matches if there is not match, no action
  219. * needed
  220. */
  221. public static void removeBook(String author) {
  222. for (int i = 0; i < theBooks.size(); i++) {
  223. if (theBooks.get(i).getAuthor().equalsIgnoreCase(author)) {
  224. theBooks.remove(i);
  225. }
  226. }
  227. }
  228.  
  229. /**
  230. * Clears the BookShelf of all books
  231. */
  232. public static void clearBookShelf() {
  233. theBooks.clear();
  234. }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement