Advertisement
MrDoyle

OOP) Grades Analyser version 2

May 17th, 2021
739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.59 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Scanner;
  3.  
  4. public class Library {
  5.  
  6.     //Library constructor
  7.     public Library () {
  8.     }
  9.  
  10.     //Method to get a list of finished books. Parameter of a Hashmap.
  11.     public void getFinishedBooks (HashMap<String,Boolean> library){
  12.         System.out.println();
  13.         int counter = 0;
  14.         if (library.size()<1){
  15.             System.out.println("Your book-list is empty.");
  16.         } else {
  17.             System.out.println("Your completed list of finished books:");
  18.             //for each key (book) in the library keyset..."
  19.  
  20.             for(String book:library.keySet()){
  21.                 if ( library.get(book) == true){
  22.                     counter ++;
  23.                     System.out.println(book);
  24.                 }
  25.             }
  26.         }
  27.         if (counter==0){
  28.             System.out.println("You have not finished any books yet, get reading!");
  29.         }
  30.     }
  31.  
  32.     //Method to get a list of unfinished books.
  33.     public void getUnfinishedBooks (HashMap<String,Boolean> library) {
  34.         System.out.println();
  35.         int counter = 0;
  36.         if (library.size() < 1) {
  37.             System.out.println("Your book-list is empty.");
  38.         } else {
  39.             System.out.println("Your list of unfinished books:");
  40.             for (String book : library.keySet()) {
  41.                 if (library.get(book) == false) {
  42.                     counter ++;
  43.                     System.out.println(book);
  44.                 }
  45.             }
  46.         }
  47.         if (counter==0){
  48.             System.out.println("You have not finished any books yet, get reading!");
  49.         }
  50.     }
  51.  
  52.     //Method to print all books in the library
  53.     public void printAllBooks (HashMap<String,Boolean> library) {
  54.         System.out.println();
  55.         if (library.size() < 1) {
  56.             System.out.println("Your book-list is empty.");
  57.         }else {
  58.             System.out.println("All of the books currently in your library are:");
  59.             for (String book : library.keySet()) {
  60.                 System.out.println(book);
  61.             }
  62.         }
  63.     }
  64.  
  65.     //Method to add a book to the library, note it will return a HashMap
  66.     public HashMap addBook (HashMap<String,Boolean> library) {
  67.         Scanner input = new Scanner (System.in);
  68.  
  69.         System.out.print("Enter the name of the book: ");
  70.         //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
  71.         String bookName = input.next();
  72.         bookName += input.nextLine();
  73.  
  74.         System.out.println("Have you finished reading: " + bookName + "? Type yes or no");
  75.         String read = input.next();
  76.  
  77.         Boolean status;
  78.         if (read.equals("yes") || read.equals("Yes") || read.equals("YES")){
  79.             status = true;
  80.         } else{
  81.             status = false;
  82.         }
  83.  
  84.         //adds the new book to the HashMap brought as a parameter
  85.         library.put(bookName, status);
  86.  
  87.         //returns the HashMap, with the new book added
  88.         return library;
  89.     }
  90.  
  91.     public static void main(String[] args) {
  92.         //Create a new HashMap called myBooks, the key is a String and the value is a Boolean.
  93.         HashMap<String,Boolean> myBooks = new HashMap<>();
  94.  
  95.         /*//Add new keys and values to the Hashmap (the original assignment, commented for now)
  96.         myBooks.put("Road Down The Funnel", true);
  97.         myBooks.put("Rat: A Biology", false);
  98.         myBooks.put("TimeIn", true);
  99.         myBooks.put("3D Food Printing", false);
  100.         */
  101.  
  102.         //Create/instantiate a new Library object
  103.         Library myLibrary = new Library();
  104.  
  105.         //scanner for user input
  106.         Scanner input = new Scanner(System.in);
  107.  
  108.         do {
  109.             try {//try will catch any errors
  110.  
  111.                 //print menu
  112.                 System.out.println();
  113.                 System.out.println("Select from the options below:");
  114.                 System.out.println("1) Print all books");
  115.                 System.out.println("2) Print finished books");
  116.                 System.out.println("3) Print unfinished books");
  117.                 System.out.println("4) Add new books to list");
  118.                 System.out.println("5) Exit program");
  119.                 System.out.print("> ");
  120.  
  121.                 //get the menu input
  122.                 int option = input.nextInt();
  123.  
  124.                 if (option == 1) {
  125.                     myLibrary.printAllBooks(myBooks);
  126.                 } else if (option == 2) {
  127.                     myLibrary.getFinishedBooks(myBooks);
  128.                 } else if (option == 3) {
  129.                     myLibrary.getUnfinishedBooks(myBooks);
  130.                 } else if (option == 4) {
  131.                     myBooks = myLibrary.addBook(myBooks); //resets the original HashMap to one with the new book that has been added
  132.                 } else if (option == 5) {
  133.                     System.out.println("Thank you for using the Library application");
  134.                     myLibrary.printAllBooks(myBooks);
  135.                     break;
  136.                 } else {
  137.                     System.out.println("That was not a valid option, try again");
  138.                 }
  139.             } catch (Exception e) {
  140.                 System.out.println("That is not an acceptable value, try again.");
  141.                 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
  142.             }
  143.         }while (true);
  144.  
  145.     }
  146. }
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement