Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Iterator;
- import java.util.HashMap;
- import java.util.Scanner;
- /**
- * Write a description of class BookLibraryApp here.
- *
- * @author redacted
- * @version 0.1
- */
- public class BookLibraryApp
- {
- private final int ADD_BOOK = 1;
- private final int LIST_BOOK = 2;
- private final int SEARCH_FOR_BOOK = 3;
- private final int REMOVE_BOOK = 4;
- private final int QUIT = 5;
- private final BookLibrary lib;
- public BookLibraryApp()
- {
- this.lib = new BookLibrary();
- }
- public static void main(String[] args)
- {
- BookLibraryApp app = new BookLibraryApp();
- app.init();
- app.start();
- }
- private void addBookToLibrary()
- {
- String title = "";
- String author = "";
- String publisher = "";
- int year = 0;
- int pages = 0;
- String ean = "";
- boolean rented = false;
- String r = null;
- boolean validInput = true;
- Scanner sc = new Scanner(System.in);
- System.out.println("Please enter the required details about the book");
- System.out.print("Enter book title: ");
- title = sc.nextLine();
- System.out.print("Enter book author: ");
- author = sc.nextLine();
- System.out.print("Enter book publisher: ");
- publisher = sc.nextLine();
- System.out.print("Enter book ean code: ");
- ean = sc.nextLine();
- System.out.print("Is this book rented? ");
- r = sc.nextLine();
- System.out.print("Enter book release year: ");
- year = sc.nextInt();
- System.out.print("Enter book page count: ");
- pages = sc.nextInt();
- if(r.contains("yes"))
- {
- rented = true;
- }
- else
- {
- rented = false;
- }
- Book b = new Book(title,author,year,publisher,pages,ean,rented);
- this.lib.addBook(b);
- System.out.println("The following book has been added to the library");
- printBook(b);
- }
- private void printBook(Book b)
- {
- System.out.println("Title: " + b.getTitle());
- System.out.println("Author: " + b.getAuthor());
- System.out.println("Publisher: " + b.getPublisher());
- System.out.println("Year: " + b.getYear());
- System.out.println("Pages: " + b.getPages());
- System.out.println("EAN: " + b.getEan());
- System.out.println("Rent Status: " + b.getRentStatus());
- }
- private void removeBook()
- {
- Scanner sc = new Scanner(System.in);
- boolean notFound = true;
- System.out.println("Which book do you want to remove?");
- String bookToRemove = sc.nextLine().toLowerCase();
- System.out.println("Are you sure you wish to remove " + bookToRemove +"?");
- if(sc.nextLine().toLowerCase().equals("yes"))
- {
- Iterator<Book> it = lib.getIterator();
- while(it.hasNext() && notFound == true)
- {
- Book b = it.next();
- if(b.getTitle().toLowerCase().equals(bookToRemove))
- {
- lib.removeBook(b.getTitle().toLowerCase());
- notFound=false;
- }
- }
- if(notFound)
- {System.out.println("We could not find the book you wanted to delete");}
- else
- {System.out.println("The book " + bookToRemove + " has been deleted");}
- }
- }
- private void start()
- {
- Boolean finished = false;
- while(!finished)
- {
- int menuChoice = this.showMenu();
- switch (menuChoice)
- {
- case ADD_BOOK:
- addBookToLibrary();
- break;
- case LIST_BOOK:
- listAllBooks();
- break;
- case SEARCH_FOR_BOOK:
- searchForBook();
- break;
- case REMOVE_BOOK:
- removeBook();
- break;
- case QUIT:
- System.out.println("Thank you for using Book Library App\n");
- finished =true;
- break;
- default:
- System.out.println("You need to enter a valid input");
- }
- }
- }
- private void searchForBook()
- {
- Scanner sc = new Scanner(System.in);
- int searchMethod = 0;
- System.out.println("What method would you like to search with?");
- System.out.println("1. Title");
- System.out.println("2. Author");
- System.out.println("3. EAN");
- searchMethod = sc.nextInt();
- switch(searchMethod)
- {
- case 1:
- searchForBookByTitle();
- break;
- case 2:
- searchForBookByAuthor();
- break;
- case 3:
- searchForBookByEan();
- break;
- default:
- System.out.println("This method is not supported");
- }
- }
- private void searchForBookByAuthor()
- {
- Scanner sc = new Scanner(System.in);
- System.out.print("Which author do you want to search after? ");
- String authorA = sc.nextLine();
- String author = authorA.toLowerCase();
- HashMap<Double,Book> h = lib.booksFromAuthor(author);
- if(h.size() != 0)
- {
- System.out.println("Here are the books we found from " + authorA);
- for(double key : h.keySet())
- {
- System.out.println("" + h.get(key).getTitle());
- }
- }
- else
- {
- System.out.println("We could not find any books from " + authorA);
- }
- }
- private void searchForBookByEan()
- {
- Scanner sc = new Scanner(System.in);
- System.out.print("Please write the EAN of the book you are looking for");
- String ean = sc.nextLine();
- Book b = lib.findBookByEan(ean);
- if(b != null)
- {
- System.out.println("The book " + ean + " was found");
- printBook(b);
- System.out.print("Was this the book you were looking for? ");
- if(sc.nextLine().equals("yes"))
- {
- System.out.print("Do you want to rent this book? ");
- if(sc.nextLine().equals("yes"))
- {
- if(!b.getRentStatus())
- {
- b.setRentStatus(true);
- System.out.println("You have now rented the book " + ean);
- System.out.println("Thank you for using Book Library App");
- }
- else
- {
- System.out.println("This book is currently rented out, please come back later");
- }
- }
- }
- else
- {
- System.out.println("Please try again, if that doesn't work please ask for help");
- }
- }
- else
- {
- System.out.println("We could sadly not find the book you were looking for");
- }
- }
- private void searchForBookByTitle()
- {
- Scanner sc = new Scanner(System.in);
- System.out.print("Please write the title of the book you are looking for");
- String title = sc.nextLine();
- Book b = lib.findBook(title);
- if(b != null)
- {
- System.out.println("The book " + title + " was found");
- printBook(b);
- System.out.print("Was this the book you were looking for? ");
- if(sc.nextLine().equals("yes"))
- {
- System.out.print("Do you want to rent this book? ");
- if(sc.nextLine().equals("yes"))
- {
- if(!b.getRentStatus())
- {
- b.setRentStatus(true);
- System.out.println("You have now rented the book " + title);
- System.out.println("Thank you for using Book Library App");
- }
- else
- {
- System.out.println("This book is currently rented out, please come back later");
- }
- }
- }
- else
- {
- System.out.println("Please try again, if that doesn't work please ask for help");
- }
- }
- else
- {
- System.out.println("We could sadly not find the book you were looking for");
- }
- }
- private void printWelcome()
- {
- System.out.println("\n***** Book Library Application v0.1\n\n");
- System.out.println("1. Add book");
- System.out.println("2. List all books");
- System.out.println("3. Search for book");
- System.out.println("4. Remove book");
- System.out.println("5. Quit");
- }
- private int showMenu()
- {
- printWelcome();
- int menuChoice = 0;
- Scanner sc = new Scanner(System.in);
- if (sc.hasNextInt())
- {
- menuChoice = sc.nextInt();
- }
- else
- {
- System.out.println("You must enter a number");
- }
- return menuChoice;
- }
- private Book rentBookByTitle(String title)
- {
- Iterator<Book> it = lib.getIterator();
- Book b = null;
- Book r = null;
- boolean found = false;
- while(it.hasNext() && !found)
- {
- b = it.next();
- if(b.getTitle().equals(title))
- {
- if(!b.getRentStatus())
- {
- r = b;
- b.setRentStatus(true);
- found = true;
- }
- else
- {
- System.out.println("This book is sadly already rented out at the moment");
- }
- }
- }
- return r;
- }
- private void returnBook(String title)
- {
- Iterator<Book> it = lib.getIterator();
- while(it.hasNext())
- {
- Book b = it.next();
- if(b.getTitle().equals(title))
- {
- if(b.getRentStatus())
- {
- b.setRentStatus(false);
- System.out.println("You have successfully returned the book " + title);
- System.out.println("We look forward to seing you next");
- }
- else
- {
- System.out.println("This book is not rented out");
- }
- }
- }
- }
- private void init()
- {
- lib.tester();
- }
- private void listAllBooks()
- {
- Iterator<Book> it = lib.getIterator();
- Book b = null;
- int amount = 0;
- System.out.println("> here are all books currently in the library");
- while(it.hasNext())
- {
- b = it.next();
- System.out.println("> " + b.getTitle());
- amount++;
- }
- System.out.println("> Books returned -> " + amount);
- }
- private Book test()
- {
- return lib.getIterator().next();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement