waitingline.java

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

JAVA
63
字号
import dslib.dispenser.LinkedQueueUos;
import dslib.base.FormatUos; 

/**	A waiting line for a simulation. */
public class WaitingLine
{
	/**	The repesentation of the waiting line. */
	protected LinkedQueueUos waitingLine;

	/**	The total amount of time the customers spent waiting in the line. */
	protected int summationWaitingTimes;

	/**	Create the waiting line.
		Analysis: Time = O(1) */
	public WaitingLine()
	{
		summationWaitingTimes = 0;
		waitingLine = new LinkedQueueUos();
	}

	/**	Are there any customers waiting?
		Analysis: Time = O(1) */
	public boolean isEmpty()
	{
		return waitingLine.isEmpty();
	}

	/**	The first customer of the waiting line.
		Analysis: Time = O(1)
		PRECONDITION
			!isEmpty() */
	public Customer first() throws WaitingLineEmptyException
	{
		if (isEmpty())
			throw new WaitingLineEmptyException("No first customer when the line is empty.");
	
		return (Customer)waitingLine.item();
	}

	/**	Customer leaves the waiting line.
		Analysis: Time = O(1)
		PRECONDITION
			!isEmpty() */
	public void deleteFirst() throws WaitingLineEmptyException
	{
		if (isEmpty())
			throw new WaitingLineEmptyException("Customer cannot be deleted when the line is empty.");

		summationWaitingTimes += Simulation.time - first().arrivalTime;
		waitingLine.deleteItem();
	}

	/**	Customer joins the waiting line.
		Analysis: Time = O(1) */
	public void join(Customer c)
	{
		waitingLine.insert(c);
		System.out.println(FormatUos.pad("", 4, 'r') + FormatUos.pad("", 7, 'c')
				+ FormatUos.pad("enter queue", 14, 'l')
				+ FormatUos.pad(c.number, 9, 'r'));
	}
}

⌨️ 快捷键说明

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