我们需要构建代表所有图书馆的类,并管理书籍的集合。
所有图书馆的开馆时间都一样:每天的上午9点至下午5点。但它们具有不同的地址和藏书(即一系列的图书对象)。
创建一个名为Library的类。我们已经为你提供了一种拥有两个图书馆对象并能对各馆内的书籍执行一些操作的主方法。但是,类中缺少方法和成员变量,需要你去定义并实现。
注意,有些方法需要是静态方法,而有些则需要是实例方法。
比较String的对象时要小心,使用string1.equals(string2)
比较string1和string2。
不允许修改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
代码👇
package homework20201007;
import java.util.Scanner;
public class _05 {
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:")