Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.28 KB | None | 0 0
  1.  
  2. import java.util.Iterator;
  3. import java.util.HashMap;
  4. import java.util.Scanner;
  5.  
  6. /**
  7. * Write a description of class BookLibraryApp here.
  8. *
  9. * @author redacted
  10. * @version 0.1
  11. */
  12. public class BookLibraryApp
  13. {
  14. private final int ADD_BOOK = 1;
  15. private final int LIST_BOOK = 2;
  16. private final int SEARCH_FOR_BOOK = 3;
  17. private final int REMOVE_BOOK = 4;
  18. private final int QUIT = 5;
  19. private final BookLibrary lib;
  20.  
  21. public BookLibraryApp()
  22. {
  23. this.lib = new BookLibrary();
  24. }
  25.  
  26. public static void main(String[] args)
  27. {
  28. BookLibraryApp app = new BookLibraryApp();
  29. app.init();
  30. app.start();
  31. }
  32.  
  33. private void addBookToLibrary()
  34. {
  35. String title = "";
  36. String author = "";
  37. String publisher = "";
  38. int year = 0;
  39. int pages = 0;
  40. String ean = "";
  41. boolean rented = false;
  42. String r = null;
  43. boolean validInput = true;
  44.  
  45. Scanner sc = new Scanner(System.in);
  46.  
  47. System.out.println("Please enter the required details about the book");
  48. System.out.print("Enter book title: ");
  49. title = sc.nextLine();
  50. System.out.print("Enter book author: ");
  51. author = sc.nextLine();
  52. System.out.print("Enter book publisher: ");
  53. publisher = sc.nextLine();
  54. System.out.print("Enter book ean code: ");
  55. ean = sc.nextLine();
  56. System.out.print("Is this book rented? ");
  57. r = sc.nextLine();
  58. System.out.print("Enter book release year: ");
  59. year = sc.nextInt();
  60. System.out.print("Enter book page count: ");
  61. pages = sc.nextInt();
  62.  
  63.  
  64. if(r.contains("yes"))
  65. {
  66. rented = true;
  67. }
  68. else
  69. {
  70. rented = false;
  71. }
  72.  
  73. Book b = new Book(title,author,year,publisher,pages,ean,rented);
  74. this.lib.addBook(b);
  75.  
  76. System.out.println("The following book has been added to the library");
  77. printBook(b);
  78.  
  79. }
  80.  
  81. private void printBook(Book b)
  82. {
  83. System.out.println("Title: " + b.getTitle());
  84. System.out.println("Author: " + b.getAuthor());
  85. System.out.println("Publisher: " + b.getPublisher());
  86. System.out.println("Year: " + b.getYear());
  87. System.out.println("Pages: " + b.getPages());
  88. System.out.println("EAN: " + b.getEan());
  89. System.out.println("Rent Status: " + b.getRentStatus());
  90. }
  91.  
  92. private void removeBook()
  93. {
  94. Scanner sc = new Scanner(System.in);
  95. boolean notFound = true;
  96. System.out.println("Which book do you want to remove?");
  97. String bookToRemove = sc.nextLine().toLowerCase();
  98. System.out.println("Are you sure you wish to remove " + bookToRemove +"?");
  99. if(sc.nextLine().toLowerCase().equals("yes"))
  100. {
  101. Iterator<Book> it = lib.getIterator();
  102. while(it.hasNext() && notFound == true)
  103. {
  104. Book b = it.next();
  105. if(b.getTitle().toLowerCase().equals(bookToRemove))
  106. {
  107. lib.removeBook(b.getTitle().toLowerCase());
  108. notFound=false;
  109. }
  110. }
  111. if(notFound)
  112. {System.out.println("We could not find the book you wanted to delete");}
  113. else
  114. {System.out.println("The book " + bookToRemove + " has been deleted");}
  115. }
  116.  
  117. }
  118.  
  119. private void start()
  120. {
  121. Boolean finished = false;
  122.  
  123. while(!finished)
  124. {
  125. int menuChoice = this.showMenu();
  126.  
  127. switch (menuChoice)
  128. {
  129. case ADD_BOOK:
  130. addBookToLibrary();
  131. break;
  132.  
  133. case LIST_BOOK:
  134. listAllBooks();
  135. break;
  136.  
  137. case SEARCH_FOR_BOOK:
  138. searchForBook();
  139. break;
  140.  
  141. case REMOVE_BOOK:
  142. removeBook();
  143. break;
  144. case QUIT:
  145. System.out.println("Thank you for using Book Library App\n");
  146. finished =true;
  147. break;
  148.  
  149. default:
  150. System.out.println("You need to enter a valid input");
  151. }
  152. }
  153. }
  154.  
  155. private void searchForBook()
  156. {
  157. Scanner sc = new Scanner(System.in);
  158. int searchMethod = 0;
  159.  
  160. System.out.println("What method would you like to search with?");
  161. System.out.println("1. Title");
  162. System.out.println("2. Author");
  163. System.out.println("3. EAN");
  164.  
  165. searchMethod = sc.nextInt();
  166.  
  167. switch(searchMethod)
  168. {
  169. case 1:
  170. searchForBookByTitle();
  171. break;
  172.  
  173. case 2:
  174. searchForBookByAuthor();
  175. break;
  176.  
  177. case 3:
  178. searchForBookByEan();
  179. break;
  180.  
  181. default:
  182. System.out.println("This method is not supported");
  183.  
  184. }
  185. }
  186.  
  187.  
  188. private void searchForBookByAuthor()
  189. {
  190. Scanner sc = new Scanner(System.in);
  191. System.out.print("Which author do you want to search after? ");
  192. String authorA = sc.nextLine();
  193. String author = authorA.toLowerCase();
  194. HashMap<Double,Book> h = lib.booksFromAuthor(author);
  195.  
  196. if(h.size() != 0)
  197. {
  198. System.out.println("Here are the books we found from " + authorA);
  199. for(double key : h.keySet())
  200. {
  201. System.out.println("" + h.get(key).getTitle());
  202. }
  203. }
  204. else
  205. {
  206. System.out.println("We could not find any books from " + authorA);
  207. }
  208. }
  209.  
  210. private void searchForBookByEan()
  211. {
  212. Scanner sc = new Scanner(System.in);
  213. System.out.print("Please write the EAN of the book you are looking for");
  214. String ean = sc.nextLine();
  215. Book b = lib.findBookByEan(ean);
  216.  
  217. if(b != null)
  218. {
  219. System.out.println("The book " + ean + " was found");
  220. printBook(b);
  221. System.out.print("Was this the book you were looking for? ");
  222. if(sc.nextLine().equals("yes"))
  223. {
  224. System.out.print("Do you want to rent this book? ");
  225. if(sc.nextLine().equals("yes"))
  226. {
  227. if(!b.getRentStatus())
  228. {
  229. b.setRentStatus(true);
  230. System.out.println("You have now rented the book " + ean);
  231. System.out.println("Thank you for using Book Library App");
  232. }
  233. else
  234. {
  235. System.out.println("This book is currently rented out, please come back later");
  236. }
  237. }
  238. }
  239. else
  240. {
  241. System.out.println("Please try again, if that doesn't work please ask for help");
  242. }
  243. }
  244. else
  245. {
  246. System.out.println("We could sadly not find the book you were looking for");
  247. }
  248. }
  249.  
  250. private void searchForBookByTitle()
  251. {
  252. Scanner sc = new Scanner(System.in);
  253. System.out.print("Please write the title of the book you are looking for");
  254. String title = sc.nextLine();
  255. Book b = lib.findBook(title);
  256.  
  257. if(b != null)
  258. {
  259. System.out.println("The book " + title + " was found");
  260. printBook(b);
  261. System.out.print("Was this the book you were looking for? ");
  262. if(sc.nextLine().equals("yes"))
  263. {
  264. System.out.print("Do you want to rent this book? ");
  265. if(sc.nextLine().equals("yes"))
  266. {
  267. if(!b.getRentStatus())
  268. {
  269. b.setRentStatus(true);
  270. System.out.println("You have now rented the book " + title);
  271. System.out.println("Thank you for using Book Library App");
  272.  
  273. }
  274. else
  275. {
  276. System.out.println("This book is currently rented out, please come back later");
  277. }
  278. }
  279. }
  280. else
  281. {
  282. System.out.println("Please try again, if that doesn't work please ask for help");
  283. }
  284. }
  285. else
  286. {
  287. System.out.println("We could sadly not find the book you were looking for");
  288. }
  289. }
  290.  
  291. private void printWelcome()
  292. {
  293. System.out.println("\n***** Book Library Application v0.1\n\n");
  294. System.out.println("1. Add book");
  295. System.out.println("2. List all books");
  296. System.out.println("3. Search for book");
  297. System.out.println("4. Remove book");
  298. System.out.println("5. Quit");
  299. }
  300.  
  301. private int showMenu()
  302. {
  303. printWelcome();
  304. int menuChoice = 0;
  305. Scanner sc = new Scanner(System.in);
  306.  
  307.  
  308. if (sc.hasNextInt())
  309. {
  310. menuChoice = sc.nextInt();
  311. }
  312. else
  313. {
  314. System.out.println("You must enter a number");
  315. }
  316. return menuChoice;
  317. }
  318.  
  319. private Book rentBookByTitle(String title)
  320. {
  321. Iterator<Book> it = lib.getIterator();
  322. Book b = null;
  323. Book r = null;
  324. boolean found = false;
  325. while(it.hasNext() && !found)
  326. {
  327. b = it.next();
  328. if(b.getTitle().equals(title))
  329. {
  330. if(!b.getRentStatus())
  331. {
  332. r = b;
  333. b.setRentStatus(true);
  334. found = true;
  335. }
  336. else
  337. {
  338. System.out.println("This book is sadly already rented out at the moment");
  339. }
  340. }
  341. }
  342. return r;
  343. }
  344.  
  345. private void returnBook(String title)
  346. {
  347. Iterator<Book> it = lib.getIterator();
  348. while(it.hasNext())
  349. {
  350. Book b = it.next();
  351. if(b.getTitle().equals(title))
  352. {
  353. if(b.getRentStatus())
  354. {
  355. b.setRentStatus(false);
  356. System.out.println("You have successfully returned the book " + title);
  357. System.out.println("We look forward to seing you next");
  358. }
  359. else
  360. {
  361. System.out.println("This book is not rented out");
  362. }
  363. }
  364. }
  365. }
  366.  
  367. private void init()
  368. {
  369. lib.tester();
  370. }
  371.  
  372. private void listAllBooks()
  373. {
  374. Iterator<Book> it = lib.getIterator();
  375. Book b = null;
  376. int amount = 0;
  377. System.out.println("> here are all books currently in the library");
  378. while(it.hasNext())
  379. {
  380. b = it.next();
  381. System.out.println("> " + b.getTitle());
  382. amount++;
  383. }
  384. System.out.println("> Books returned -> " + amount);
  385. }
  386.  
  387. private Book test()
  388. {
  389. return lib.getIterator().next();
  390. }
  391.  
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement