⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 newarrivalevent.java

📁 国外的数据结构与算法分析用书
💻 JAVA
字号:
import java.util.Random;
import dslib.exception.ItemNotComparableUosException;
import dslib.base.FormatUos; 

/**	The arrival of a new customer or customers into the system. */
public class NewArrivalEvent extends Event
{
	/**	Construct a new NewArrivalEvent object. */
	public NewArrivalEvent(int newTime)
	{
		time = newTime;
	}
	
	/**	The total number of customers that have entered the simulation. */
	public static int numberOfCustomers = 0;

	/**	Have the next group of customers arrive at the line up.
		Analysis: Time = O(size of the group of customers) */
	public void execute()
	{
		traceArrivalEvent();
		for (int k = 0; k < numberOfArrivals(); k++)
		{
			numberOfCustomers++;
			Customer c = new Customer(numberOfCustomers, time);
			Simulation.waitingLine.join(c);
		}
	
		if (!Simulation.cashier.busy())
			try 
			{
				Simulation.cashier.startNextService();
			} catch (WaitingLineEmptyException e) 
			{
				/*	Should not occur: new customers just arrived so numberOfArrivals is always > 0. */
				e.printStackTrace();
				throw new Error("Error: possible fault in isEmpty() or numberOfArrivals()");
			} catch (CashierBusyException e) 
			{
				/*	Should not occur: the cashier was just checked for !busy(). */
				e.printStackTrace();
				throw new Error("Error: possible fault in busy()");
			}
		scheduleNextArrivalEvent();
	}

	/**	The random number generator used.
		Analysis: Time = O(1) */
	protected static Random randomNumberGenerator = new Random(729417);

	/**	Determine the number of arrivals for this arrival event.
		Analysis: Time = O(1) */
	public int numberOfArrivals()
	{
		double randomItem = randomNumberGenerator.nextDouble();
		if (randomItem < 0.5)
			return 1;
		if (randomItem < 0.9)
			return 2;
		return 4;
	}

	/**	The last possible time for an arrival.
		Analysis: Time = O(1) */
	protected final int lastArrivalTime = 100;

	/**	Schedule the next set of arrivals for an appropriate time in the future.
		Analysis: Time = O(1) */
	public void scheduleNextArrivalEvent()
	{
		int delay = 0;
		switch (randomNumberGenerator.nextInt(3))
		{
			case 0: 
				delay = 15;
				break;
			case 1: 
				delay = 35;
				break;
			case 2: 
				delay = 70;
				break;
		}
	
		if ((time + delay) <= lastArrivalTime)
		{
			Event e = new NewArrivalEvent(time + delay);
			Simulation.eventslist.insert(e);
			traceScheduleNextArrival(time + delay);
		}
	}

	/**	Print inFormatUosion for an arrival event to facilitate tracing. 
		Analysis: Time = O(1) */
	public void traceArrivalEvent()
	{
		System.out.println(FormatUos.pad(time, 4, 'r') + FormatUos.pad("", 7, 'c') 
				+ FormatUos.pad("new arrivals", 14, 'l'));
	}

	/**	Print inFormatUosion for the scheduling of a next arrivals event at time arrivalTime. 
		Analysis: Time = O(1) */
	public void traceScheduleNextArrival(int arrivalTime)
	{
		System.out.println(FormatUos.pad("", 4, 'r') + FormatUos.pad("sched", 7, 'c') 
				+ FormatUos.pad("new arrivals", 14, 'l') + FormatUos.pad("", 9, 'r') 
				+ FormatUos.pad(arrivalTime, 12, 'r'));
	}
}

⌨️ 快捷键说明

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