Advertisement
Dido09

Library

Jun 1st, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. package Practice;
  2.  
  3. public class Book {
  4.  
  5. private String name;
  6. private String genre;
  7. private int pages;
  8. private String rating;
  9.  
  10.  
  11. public Book(String name, String genre, int duration, String rating) {
  12. this.name = name;
  13. this.genre = genre;
  14. this.pages = duration;
  15. this.rating = rating;
  16.  
  17. }
  18.  
  19. public void review() {
  20.  
  21. System.out.println("The book's name is: " + name);
  22. System.out.println("It's genre is: " + genre);
  23. System.out.println("It has: " + pages + " pages");
  24. System.out.println("And it's rated: " + rating);
  25. }
  26.  
  27. public String getName() {
  28. return name;
  29. }
  30.  
  31. public void setName(String name) {
  32. this.name = name;
  33. }
  34.  
  35. public String getGenre() {
  36. return genre;
  37. }
  38.  
  39. public void setGenre(String genre) {
  40. this.genre = genre;
  41. }
  42.  
  43. public int getPages() {
  44. return pages;
  45. }
  46.  
  47. public void setPages(int pages) {
  48. this.pages = pages;
  49. }
  50.  
  51. public String getRating() {
  52. return rating;
  53. }
  54.  
  55. public void setRating(String rating) {
  56. this.rating = rating;
  57. }
  58. }
  59. ////////////////////////////////////////////////////////////////////////
  60. package Practice;
  61.  
  62. import java.util.ArrayList;
  63. import java.util.Arrays;
  64. import java.util.List;
  65. import java.util.Scanner;
  66.  
  67. public class Library {
  68.  
  69.  
  70. public static void main(String[] args) {
  71.  
  72. Book book1 = new Book("Rocky", "Drama, Action",560, "R");
  73. Book book2= new Book("Harry Potter", "Fantasy", 330, "PG");
  74. Book book3 = new Book("Parasite", "Drama, Thriller", 140, "R");
  75.  
  76.  
  77. Scanner scanner = new Scanner(System.in);
  78.  
  79. List<Book> books = new ArrayList<>(Arrays.asList(book1, book2, book3));
  80.  
  81. System.out.println("Enter a book name: ");
  82. while (true) {
  83. String command = scanner.nextLine();
  84. try {
  85. if (searchByName(books, command)) break;
  86. } catch (noBookFound e) {
  87. System.out.println(e.getMessage());
  88. }
  89.  
  90. System.out.println();
  91. System.out.println("IF YOU ARE DONE, TYPE 'stop'");
  92. }
  93. }
  94.  
  95. private static boolean searchByName(List<Book> books, String command) throws noBookFound {
  96. int counter = 1;
  97. boolean isBookFound = false;
  98.  
  99. for (int i = 0; i < books.size(); i++) {
  100.  
  101.  
  102. if (command.equalsIgnoreCase(books.get(i).getName())) {
  103. books.get(i).review();
  104. isBookFound = true;
  105.  
  106.  
  107. } else if (command.equalsIgnoreCase("Stop")) {
  108. counter = 0;
  109.  
  110. }
  111. if (counter == 0) {
  112. return true;
  113. }
  114. }
  115. if (isBookFound == false) {
  116. throw new noBookFound();
  117. }
  118. return false;
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement