Library类管理两个图书馆的书籍,每个图书馆开放时间均为9 AM 至5 PM,各自有不同的地点和收藏书籍,书籍可以用数组定义。请将Library类代码补充完整。 注意:有些方法可能需要定义为static,不要更改main里的方法调用
时间: 2023-05-27 15:07:50 浏览: 67
public class Library {
private String location;
private Book[] books;
private static final int OPEN_HOUR = 9;
private static final int CLOSE_HOUR = 17;
public Library(String location, Book[] books) {
this.location = location;
this.books = books;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public Book[] getBooks() {
return books;
}
public void setBooks(Book[] books) {
this.books = books;
}
public static int getOpenHour() {
return OPEN_HOUR;
}
public static int getCloseHour() {
return CLOSE_HOUR;
}
public static boolean isOpen(int hour) {
return hour >= OPEN_HOUR && hour < CLOSE_HOUR;
}
public static void printOpeningHours() {
System.out.println("The library is open from " + OPEN_HOUR + "AM to " + CLOSE_HOUR + "PM.");
}
public void printAddress() {
System.out.println("The library is located at " + location + ".");
}
public static void borrowBook(Book book) {
if (book.isBorrowed()) {
System.out.println("Sorry, this book is already borrowed.");
} else {
book.borrowed();
System.out.println("You successfully borrowed " + book.getTitle() + ".");
}
}
public static void printAvailableBooks(Book[] books) {
boolean empty = true;
for (Book book : books) {
if (!book.isBorrowed()) {
System.out.println(book.getTitle());
empty = false;
}
}
if (empty) {
System.out.println("No books available in this library.");
}
}
public void returnBook(Book book) {
book.returned();
System.out.println("You successfully returned " + book.getTitle() + ".");
}
public static void main(String[] args) {
// Create two libraries
Book[] books1 = new Book[]{
new Book("The Da Vinci Code"),
new Book("Le Petit Prince"),
new Book("A Tale of Two Cities")
};
Library library1 = new Library("Library 1", books1);
Book[] books2 = new Book[]{
new Book("The Hobbit"),
new Book("The Lord of the Rings"),
new Book("The Silmarillion")
};
Library library2 = new Library("Library 2", books2);
// Print opening hours and the addresses of the libraries
printOpeningHours();
library1.printAddress();
library2.printAddress();
// Try to borrow The Lord of the Rings from both libraries
borrowBook(books2[1]);
borrowBook(books2[1]);
// Print available books from library1
System.out.println("Available books in " + library1.getLocation() + ":");
printAvailableBooks(library1.getBooks());
// Print available books from library2
System.out.println("Available books in " + library2.getLocation() + ":");
printAvailableBooks(library2.getBooks());
// Return The Lord of the Rings to library2
library2.returnBook(books2[1]);
// Print available books from library2 again
System.out.println("Available books in " + library2.getLocation() + ":");
printAvailableBooks(library2.getBooks());
}
}
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)