某镇上有两个图书馆,请按照下面要求及提示帮助这两个图书馆实现图书电子借阅。
完成Library.java,该类中的main()方法已经给出。但类成员和类方法需要完善。完成该类的时候请注意以下要求:
A.该类中有类成员方法,也有实例成员方法;
B. 不能改变main()方法的内容。
C.main( )方法的输出为:
Library hours:
Libraries 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
Sorry, this book is already borrowed.
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
Books available in the second library:
No book in catalog
Returning The Lord of the Rings:
You successfully returned The Lord of the Rings
Books available in the first library:
The Da Vinci Code
Le Petit Prince
A Tale of Two Cities
The Lord of the Rings
import java.util.ArrayList;
/**
* @version 1.0
*/
public class Library {
// Add the missing implementation to this class
String Library_title;
public static class Book {
String title;
boolean borrowed;
public Book(String title) {
this.title = title;
borrowed = false;
}
}
//Book[] books = new Book[6];
ArrayList<Book> books= new ArrayList<>();
public Library(String library_title) {
Library_title = library_title;
// books = new Book[6];
}
public void addBook(Book book) {
// Book_num++;
// books[Book_num].title = book.title;
// books[Book_num].borrowed = book.borrowed;
books.add(book);
}
public static void printOpeningHours() {
System.out.println("Libraries are open daily from 9am to 5pm.");
}
public void printAddress() {
System.out.println(Library_title);
}
public void borrowBook(String name) {
int i;
for (i = 0; i < books.size(); i++) {
if (name.equals(books.get(i).title)) {
break;
}
}
if (i == books.size()) {
System.out.println("Sorry, this book is not in our catalog.");
} else {
if (!books.get(i).borrowed) {
System.out.println("You successfully borrowed The Lord of the Rings");
books.get(i).borrowed = true;
} else {
System.out.println("Sorry, this book is already borrowed.");
}
}
}
public void printAvailableBooks(){
int i,j=0;
for(i=0;i<books.size();i++){
if(!books.get(i).borrowed){
System.out.println(books.get(i).title);
j++;
}
}
if(j==0){
System.out.println("No book in catalog");
}
}
public void returnBook(String name){
int i;
for (i = 0; i < books.size(); i++) {
if (name.equals(books.get(i).title)) {
break;
}
}
if (i == books.size()) {
System.out.println("Sorry, this book is not in our catalog.");
} else {
System.out.println("You successfully returned "+name);
books.get(i).borrowed = false;
}
}
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:");
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();
}
}