{importjava.util.ArrayList;publicclassMyLibrary{Stringname;ArrayList<Book>books;ArrayList<Person>people;publicMyLibrary(Stringname){this.name=name;books=newArrayList<Book>();people=newArrayList<Person>();}publicStringgetName(){returnname;}publicArrayList<Book>getBooks(){returnbooks;}publicArrayList<Person>getPeople(){returnpeople;}publicvoidaddBook(Bookb1){this.books.add(b1);}publicvoidremoveBook(Bookb1){this.books.remove(b1);}publicvoidaddPerson(Personp1){this.people.add(p1);}publicvoidremovePerson(Personp1){this.people.remove(p1);}publicbooleancheckOut(Bookb1,Personp1){intbooksOut=this.getBooksForPerson(p1).size();if((b1.getPerson()==null)&&(booksOut<p1.getMaximumBooks())){b1.setPerson(p1);returntrue;}else{returnfalse;}}publicbooleancheckIn(Bookb1){if(b1.getPerson()!=null){b1.setPerson(null);returntrue;}else{returnfalse;}}publicArrayList<Book>getBooksForPerson(Personp1){ArrayList<Book>result=newArrayList<Book>();for(BookaBook:this.getBooks()){if((aBook.getPerson()!=null)&&(aBook.getPerson().getName().equals(p1.getName()))){result.add(aBook);}}returnresult;}publicArrayList<Book>getAvailableBooks(){ArrayList<Book>result=newArrayList<Book>();for(BookaBook:this.getBooks()){if(aBook.getPerson()==null){result.add(aBook);}}returnresult;}publicArrayList<Book>getUnavailableBooks(){ArrayList<Book>result=newArrayList<Book>();for(BookaBook:this.getBooks()){if(aBook.getPerson()!=null){result.add(aBook);}}returnresult;}publicStringtoString(){returnthis.getName()+": "+this.getBooks().size()+" books; "+this.getPeople().size()+" people.";}publicstaticvoidmain(String[]args){//createanewMyLibrary//ThisallowsJavaprogramtoreadparmsfromacommandlineMyLibrarytestLibrary=newMyLibrary("Test Drive Library");Bookb1=newBook("War And Peace");Bookb2=newBook("Great Expectations");b1.setAuthor("Tolstoy");b2.setAuthor("Dickens");Personjim=newPerson();Personsue=newPerson();jim.setName("Jim");sue.setName("Sue");testLibrary.addBook(b1);testLibrary.addBook(b2);testLibrary.addPerson(jim);testLibrary.addPerson(sue);System.out.println("Just Created new library");testLibrary.printStatus();System.out.println("Check out War And Peace to Sue");testLibrary.checkOut(b1,sue);testLibrary.printStatus();System.out.println("Do some more stuff");testLibrary.checkIn(b1);testLibrary.checkOut(b2,jim);testLibrary.printStatus();}privatevoidprintStatus(){System.out.println("Status Report of MyLibrary \n"+this.toString());for(BookthisBook:this.getBooks()){System.out.println(thisBook);}for(Personp:this.getPeople()){intcount=this.getBooksForPerson(p).size();System.out.println(p+" (has "+count+" of my books)");}System.out.println(" Books Available: "+this.getAvailableBooks().size());System.out.println("---End of Status Report ---");}}}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
package org.totalbeginner.tutorial;