Sunteți pe pagina 1din 4

package Library; import java.util.

ArrayList; public class Library{ public ArrayL ist<Book> Library = new ArrayList<Book>(); public String address; //add librarie s METHOD public Library(String LibraryAddress) { address = LibraryAddress; } //a dd books METHOD public void addBook(Book newBook) { Library.add(newBook); } // A dd the missing implementation to this class public static void main(String[] arg s) { // Create two libraries Library firstLibrary = new Library("10 Main St."); Library secondLibrary = new Library("228 Liberty St."); // Add four books to the firstLibrary.addBook(new firstLibrary.addBook(new firstLibrary.addBook(new firs tLibrary.addBook(new first library Book("The Da Vinci Code")); Book("Le Petit Pr ince")); Book("A Tale of Two Cities")); Book("The Lord of the Rings")); // Print opening hours and the addresses System.out.println("Library hours:" + " \n" + "Library are open daily fro m 9AM to 5PM"); printOpeningHours(); System.ou t.println(); System.out.println("Library addresses:"); firstLibrary.printAddress (); secondLibrary.printAddress(); System.out.println(); // Try to borrow The Lor ds of the Rings from both libraries System.out.println("Borrowing The Lord of th e Rings:"); firstLibrary.borrowBook("The Lord of the Rings"); firstLibrary.borro wBook("The Lord of the Rings"); secondLibrary.borrowBook("The Lord of the Rings" ); System.out.println(); // Print the titles of all available books from both li braries System.out.println("Books available in the first library:"); firstLibrar y.printAvailableBooks(); System.out.println(); System.out.println("Books availab le in the second library:"); secondLibrary.printAvailableBooks(); System.out.pri ntln(); // Return The Lords of the Rings to the first library System.out.println ("Returning The Lord of the Rings:"); firstLibrary.returnBook("The Lord of the R ings"); System.out.println();

// Print the titles of available from the first library System.out.println ("Boo ks available in the first library:"); firstLibrary.printAvailableBooks(); } /* -------------M---E---T---H---O---D---S--------------*/ //METHOD #1 >> Library ho urs public static void printOpeningHours() { } //METHOD #2 >> Library addresses public void printAddress() { System.out.println(address); } //METHOD #3 >> Borro wing The Lord of the Rings public void borrowBook(String requestedBook) { if (Li brary.isEmpty()) { System.out.println("Sorry, this book is not in our catalog.") ; }else { for (int i = 0; i < Library.size(); i++) { if (requestedBook.equals(Li brary.get(i).getTitle())) { if (Library.get(i).isBorrowed()) { System.out.printl n("Sorry, this book is already borrowed ."); } else { System.out.println("You su ccessfully borrowed " + Librar y.get(i).getTitle()); Library.get(i).returned(); } } else if (Library.size() == i + 1) { System.out.println("Sorry, this book is not in our catalog." ); } } } } //METHOD #4 >> Books available in the first libr ary public void printAvailableBooks() { if (Library.isEmpty()) { System.out.prin tln("No book in catalog"); } else { for (int i = 0; i < Library.size(); i++) { i f (Library.get(i).isBorrowed()) { } else { System.out.println(Library.get(i).get Title()); } } } } //METHOD #6 >> Returning The Lord of the Rings: public void re turnBook(String requestedBook) { for (int i = 0; i < Library.size(); i++) { if ( requestedBook.equals(Library.get(i).getTitle())) {

if (Library.get(i).isBorrowed()) { Library.get(i).returned(); System.out.println ("You successfully returned " + Library.ge t(i).getTitle()); } else { System.out .println("This book is not rented out"); Library.get(i).returned(); } } } } } -------------------------------------------output--------------------------------------------------output marked with *** shows different output compared to tu torial 4 -------------------------------------------------------------------------------------------------------Library hours: Library are open daily from 9AM to 5PM Library addresses: 10 Main St. 228 Liberty St. Borrowing The Lord of the Rings: You successfully borrowed The Lord of the Rings You successfully borrowed The Lord of the Rings*** Sorry, this book is not in our catalog. Books availabl e in the first library: The Da Vinci Code Le Petit Prince A Tale of Two Cities T he Lord of the Rings*** Books available in the second library: No book in catalo g Returning The Lord of the Rings: This book is not rented out*** Books availabl e in the first library: The Da Vinci Code Le Petit Prince A Tale of Two Cities T he Lord of the Rings

S-ar putea să vă placă și