Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Practice;
- public class Book {
- private String name;
- private String genre;
- private int pages;
- private String rating;
- public Book(String name, String genre, int duration, String rating) {
- this.name = name;
- this.genre = genre;
- this.pages = duration;
- this.rating = rating;
- }
- public void review() {
- System.out.println("The book's name is: " + name);
- System.out.println("It's genre is: " + genre);
- System.out.println("It has: " + pages + " pages");
- System.out.println("And it's rated: " + rating);
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getGenre() {
- return genre;
- }
- public void setGenre(String genre) {
- this.genre = genre;
- }
- public int getPages() {
- return pages;
- }
- public void setPages(int pages) {
- this.pages = pages;
- }
- public String getRating() {
- return rating;
- }
- public void setRating(String rating) {
- this.rating = rating;
- }
- }
- ////////////////////////////////////////////////////////////////////////
- package Practice;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Scanner;
- public class Library {
- public static void main(String[] args) {
- Book book1 = new Book("Rocky", "Drama, Action",560, "R");
- Book book2= new Book("Harry Potter", "Fantasy", 330, "PG");
- Book book3 = new Book("Parasite", "Drama, Thriller", 140, "R");
- Scanner scanner = new Scanner(System.in);
- List<Book> books = new ArrayList<>(Arrays.asList(book1, book2, book3));
- System.out.println("Enter a book name: ");
- while (true) {
- String command = scanner.nextLine();
- try {
- if (searchByName(books, command)) break;
- } catch (noBookFound e) {
- System.out.println(e.getMessage());
- }
- System.out.println();
- System.out.println("IF YOU ARE DONE, TYPE 'stop'");
- }
- }
- private static boolean searchByName(List<Book> books, String command) throws noBookFound {
- int counter = 1;
- boolean isBookFound = false;
- for (int i = 0; i < books.size(); i++) {
- if (command.equalsIgnoreCase(books.get(i).getName())) {
- books.get(i).review();
- isBookFound = true;
- } else if (command.equalsIgnoreCase("Stop")) {
- counter = 0;
- }
- if (counter == 0) {
- return true;
- }
- }
- if (isBookFound == false) {
- throw new noBookFound();
- }
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement