Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- import java.util.Scanner;
- public class Library {
- //Library constructor
- public Library () {
- }
- //Method to get a list of finished books. Parameter of a Hashmap.
- public void getFinishedBooks (HashMap<String,Boolean> library){
- System.out.println();
- int counter = 0;
- if (library.size()<1){
- System.out.println("Your book-list is empty.");
- } else {
- System.out.println("Your completed list of finished books:");
- //for each key (book) in the library keyset..."
- for(String book:library.keySet()){
- if ( library.get(book) == true){
- counter ++;
- System.out.println(book);
- }
- }
- }
- if (counter==0){
- System.out.println("You have not finished any books yet, get reading!");
- }
- }
- //Method to get a list of unfinished books.
- public void getUnfinishedBooks (HashMap<String,Boolean> library) {
- System.out.println();
- int counter = 0;
- if (library.size() < 1) {
- System.out.println("Your book-list is empty.");
- } else {
- System.out.println("Your list of unfinished books:");
- for (String book : library.keySet()) {
- if (library.get(book) == false) {
- counter ++;
- System.out.println(book);
- }
- }
- }
- if (counter==0){
- System.out.println("You have not finished any books yet, get reading!");
- }
- }
- //Method to print all books in the library
- public void printAllBooks (HashMap<String,Boolean> library) {
- System.out.println();
- if (library.size() < 1) {
- System.out.println("Your book-list is empty.");
- }else {
- System.out.println("All of the books currently in your library are:");
- for (String book : library.keySet()) {
- System.out.println(book);
- }
- }
- }
- //Method to add a book to the library, note it will return a HashMap
- public HashMap addBook (HashMap<String,Boolean> library) {
- Scanner input = new Scanner (System.in);
- System.out.print("Enter the name of the book: ");
- //the next 2 lines allow the input to be an input with a space from: https://stackoverflow.com/questions/4058912/scanner-doesnt-read-whole-sentence-difference-between-next-and-nextline-o
- String bookName = input.next();
- bookName += input.nextLine();
- System.out.println("Have you finished reading: " + bookName + "? Type yes or no");
- String read = input.next();
- Boolean status;
- if (read.equals("yes") || read.equals("Yes") || read.equals("YES")){
- status = true;
- } else{
- status = false;
- }
- //adds the new book to the HashMap brought as a parameter
- library.put(bookName, status);
- //returns the HashMap, with the new book added
- return library;
- }
- public static void main(String[] args) {
- //Create a new HashMap called myBooks, the key is a String and the value is a Boolean.
- HashMap<String,Boolean> myBooks = new HashMap<>();
- /*//Add new keys and values to the Hashmap (the original assignment, commented for now)
- myBooks.put("Road Down The Funnel", true);
- myBooks.put("Rat: A Biology", false);
- myBooks.put("TimeIn", true);
- myBooks.put("3D Food Printing", false);
- */
- //Create/instantiate a new Library object
- Library myLibrary = new Library();
- //scanner for user input
- Scanner input = new Scanner(System.in);
- do {
- try {//try will catch any errors
- //print menu
- System.out.println();
- System.out.println("Select from the options below:");
- System.out.println("1) Print all books");
- System.out.println("2) Print finished books");
- System.out.println("3) Print unfinished books");
- System.out.println("4) Add new books to list");
- System.out.println("5) Exit program");
- System.out.print("> ");
- //get the menu input
- int option = input.nextInt();
- if (option == 1) {
- myLibrary.printAllBooks(myBooks);
- } else if (option == 2) {
- myLibrary.getFinishedBooks(myBooks);
- } else if (option == 3) {
- myLibrary.getUnfinishedBooks(myBooks);
- } else if (option == 4) {
- myBooks = myLibrary.addBook(myBooks); //resets the original HashMap to one with the new book that has been added
- } else if (option == 5) {
- System.out.println("Thank you for using the Library application");
- myLibrary.printAllBooks(myBooks);
- break;
- } else {
- System.out.println("That was not a valid option, try again");
- }
- } catch (Exception e) {
- System.out.println("That is not an acceptable value, try again.");
- input.next(); //resets the input, otherwise will be stuck in infinite error loop, from: https://stackoverflow.com/questions/3572160/how-to-handle-infinite-loop-caused-by-invalid-input-inputmismatchexception-usi
- }
- }while (true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement