Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.HashMap;
- 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();
- 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){
- System.out.println(book);
- }
- }
- }
- }
- //Method to get a list of unfinished books.
- public void getUnfinishedBooks (HashMap<String,Boolean> library) {
- System.out.println();
- 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) {
- System.out.println(book);
- }
- }
- }
- }
- 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
- 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();
- //Call the getFinishedBooks method, with the HashMap as the parameter.
- myLibrary.getFinishedBooks(myBooks);
- myLibrary.getUnfinishedBooks(myBooks);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement