0% found this document useful (0 votes)
265 views3 pages

Library

The document defines a Library class with methods to create libraries with addresses, add books to libraries, borrow and return books, and print available books. It then creates two Library objects, adds books to the first library, borrows and returns books, and prints available books to demonstrate the class functionality.

Uploaded by

Norzida
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
265 views3 pages

Library

The document defines a Library class with methods to create libraries with addresses, add books to libraries, borrow and return books, and print available books. It then creates two Library objects, adds books to the first library, borrows and returns books, and prints available books to demonstrate the class functionality.

Uploaded by

Norzida
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

package Library;

import java.util.ArrayList;
public class Library{
public ArrayList<Book> Library = new ArrayList<Book>();
public String address;
//add libraries METHOD
public Library(String LibraryAddress) {
address = LibraryAddress;
}
//add books METHOD
public void addBook(Book newBook) {
Library.add(newBook);
}
// Add the missing implementation to this class
public static void main(String[] args) {
// Create two libraries
Library firstLibrary = new Library("10 Main St.");
Library secondLibrary = new Library("228 Liberty St.");
// Add four books to the first library
firstLibrary.addBook(new Book("The Da Vinci Code"));
firstLibrary.addBook(new Book("Le Petit Prince"));
firstLibrary.addBook(new Book("A Tale of Two Cities"));
firstLibrary.addBook(new 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.out.println();
System.out.println("Library addresses:");
firstLibrary.printAddress();
secondLibrary.printAddress();
System.out.println();
// Try to borrow The Lords of the Rings from both libraries
System.out.println("Borrowing The Lord of the Rings:");
firstLibrary.borrowBook("The Lord of the Rings");
firstLibrary.borrowBook("The Lord of the Rings");
secondLibrary.borrowBook("The Lord of the Rings");
System.out.println();
// Print the titles of all available books from both libraries
System.out.println("Books available in the first library:");
firstLibrary.printAvailableBooks();
System.out.println();
System.out.println("Books available in the second library:");
secondLibrary.printAvailableBooks();
System.out.println();
// 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 Rings");
System.out.println();
// Print the titles of available from the first library
System.out.println ("Books available in the first library:");
firstLibrary.printAvailableBooks();
}
/* --------------M---E---T---H---O---D---S--------------*/
//METHOD #1 >> Library hours
public static void printOpeningHours() {
}
//METHOD #2 >> Library addresses
public void printAddress() {
System.out.println(address);
}
//METHOD #3 >> Borrowing The Lord of the Rings
public void borrowBook(String requestedBook) {
if (Library.isEmpty()) {
System.out.println("Sorry, this book is not in our catalog.");
}else {
for (int i = 0; i < Library.size(); i++) {
if (requestedBook.equals(Library.get(i).getTitle())) {
if (Library.get(i).isBorrowed()) {
System.out.println("Sorry, this book is already borrowed
.");
}
else {
System.out.println("You successfully 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 library
public void printAvailableBooks() {
if (Library.isEmpty()) {
System.out.println("No book in catalog");
} else {
for (int i = 0; i < Library.size(); i++) {
if (Library.get(i).isBorrowed()) {
} else {
System.out.println(Library.get(i).getTitle());
}
}
}
}
//METHOD #6 >> Returning The Lord of the Rings:
public void returnBook(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 tutorial 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 available in the first library:
The Da Vinci Code
Le Petit Prince
A Tale of Two Cities
The Lord of the Rings***
Books available in the second library:
No book in catalog
Returning The Lord of the Rings:
This book is not rented out***
Books available in the first library:
The Da Vinci Code
Le Petit Prince
A Tale of Two Cities
The Lord of the Rings

You might also like