patron.java

来自「国外的数据结构与算法分析用书」· Java 代码 · 共 51 行

JAVA
51
字号
package library;

import dslib.list.LinkedListUos;
import dslib.exception.*;

/**	A library patron with a name, list of books borrowed, and methods to
	borrow and return a book. */
public class Patron
{
	/**	Name of the patron. */
	public String name;

	/**	List of the books borrowed by the patron. */
	protected LinkedListUos borrowedBooks;

	/**	Initialize the patron and the list of books on loan.
		Analysis: Time = O(1) */
	public Patron(String n)
	{
		name = n;
		borrowedBooks = new LinkedListUos();
	}

	/**	Process the borrowing of book b.
		Analysis: Time = O(1) */
	public void borrowBook(LibraryBook b)
	{
		borrowedBooks.insert(b);
	}

	/**	Process the returning of book b.
		Analysis: Time = O(k), k = number of borrowed books 
		PRECONDITION:
			borrowedBooks.has(b) */
	public void returnBook(LibraryBook b) throws ItemNotFoundUosException
	{
		if (!borrowedBooks.has(b))
			throw new ItemNotFoundUosException("Patron cannot return a book, "
					+ "if it isn't in the Patron's list of borrowed books");

		borrowedBooks.delete(b);
	}

	/**	String representation of the patron.
		Analysis: Time = O(k), k = number of borrowed books */
	public String toString()
	{
		return "\n" + name + " has on loan " + borrowedBooks.toString() + "\n";
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?