Java作业PTA

本文档描述了如何设计一个`Library`类,该类管理图书馆的开馆时间、地址和书籍集合。类需要包含静态和实例方法,用于比较字符串、借阅和归还书籍等操作。示例输出展示了类的使用,包括书籍的借阅和归还情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们需要构建代表所有图书馆的类,并管理书籍的集合。

所有图书馆的开馆时间都一样:每天的上午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:");
		Library.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();
	}
}

class Library {
	// Add the missing implementation to this class
	String realLocation;
	String[][] bookCollection = new String[2][10];
	int index = 0;

	public Library(String Location) {
		realLocation = Location;
	}

	static void printOpeningHours() {
		System.out.println("Libraries are open daily from 9am to 5pm.");
	}

	void printAddress() {
		System.out.println(realLocation);
	}

	void addBook(Book collection) {
		bookCollection[0][index] = collection.title;
		bookCollection[1][index] = "1";
		index++;
	}

	void borrowBook(String S) {
		int index1 = 0;
		while (!S.equals(bookCollection[0][index1])) {
			index1++;
			if (bookCollection[0].length == index1)
				break;
		}
		if (index1 == bookCollection[0].length) {
			System.out.println("Sorry, this book is not in our catalog.");
		} else if (bookCollection[1][index1] == "1") {
			System.out.println("You successfully borrowed The Lord of the Rings");
			bookCollection[1][index1] = "0";
		} else if (bookCollection[1][index1] == "0") {
			System.out.println("Sorry, this book is already borrowed.");
		}
	}

	void printAvailableBooks() {
		int index2 = 0;
		int flag = 0;
		while (true) {
			if (index2 == bookCollection[0].length) {
				break;
			}
			if (bookCollection[1][index2] == "1") {
				flag = 1;
				System.out.println(bookCollection[0][index2]);
			}

			index2++;

		}
		if (flag == 0) {
			System.out.println("No book in catalog");
		}

	}

	void returnBook(String bookName) {
		for (int i = 0; i < bookCollection[0].length; i++) {
			if (bookName.equals(bookCollection[0][i])) {
				bookCollection[1][i] = "1";
				break;
			}
		}
		System.out.println("You successfully returned The Lord of the Rings");
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值