librarypatron.java

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

JAVA
51
字号
package patronBook;

import simple.*;

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

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

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

	/**	Process the borrowing of book b.
		Analysis: Time = O(k), k = number of books borrowed */
	public void borrowBook(ComparableBook b)
	{
		borrowedBooks.insert(b);
	}

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

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

⌨️ 快捷键说明

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