📄 cashier.java
字号:
import dslib.exception.ItemNotComparableUosException;
import dslib.base.FormatUos;
/** A Cashier for a simple waiting-line simulation. The cashier knows which
customer is being serviced (if any), knows when the last idle time period
started, and accumulates the total amount of idle time. */
public class Cashier
{
/** Customer currently being served. */
protected Customer customer;
/** Is the cashier currently busy?
Analysis : Time = O(1) */
public boolean busy()
{
return customer != null;
}
/** The total amount of time that the cashier has been idle. */
protected int totalIdleTime;
/** The time when the cashier started his/her last idle period. */
protected int startIdleTime;
/** Time to serve a customer. */
protected final int serviceTime = 20;
/** Create a cashier not busy, and initialize the startIdleTime and totalIdleTime.
Analysis : Time = O(1) */
public Cashier()
{
customer = null;
startIdleTime = 0;
totalIdleTime = 0;
}
/** Start the cashier serving the next customer.
Analysis: Time = O(1)
PRECONDITION
!Simulation.waitingLine.isEmpty()
!busy() */
public void startNextService() throws WaitingLineEmptyException, CashierBusyException
{
if (Simulation.waitingLine.isEmpty())
throw new WaitingLineEmptyException("Cannot start next customer if line is empty.");
if (busy())
throw new CashierBusyException("Cashier cannot start a new customer as the cashier is already busy.");
customer = Simulation.waitingLine.first();
Simulation.waitingLine.deleteFirst();
totalIdleTime = totalIdleTime + Simulation.time - startIdleTime;
traceStartService();
FinishServiceEvent event = new FinishServiceEvent(Simulation.time + serviceTime);
Simulation.eventslist.insert(event);
traceScheduleFinishService();
}
/** Complete the service of the present customer.
Analysis : Time = O(1) */
public void completeService()
{
traceFinishService();
customer = null;
startIdleTime = Simulation.time;
if (!Simulation.waitingLine.isEmpty())
try
{
startNextService();
} catch (WaitingLineEmptyException e)
{
/* Should not occur: !isEmpty() is checked. */
e.printStackTrace();
throw new Error("Error: possible fault in isEmpty()");
} catch (CashierBusyException e)
{
/* Should not occur: the cashier was just set to the idle state. */
e.printStackTrace();
throw new Error("Error: possible fault in setting cashier idle");
}
}
/** Print information for the start of service of the current customer.
Analysis: Time = O(1) */
public void traceStartService()
{
System.out.println(FormatUos.pad("", 4, 'r') + FormatUos.pad("", 7, 'c') + FormatUos.pad("start service", 14, 'l')
+ FormatUos.pad(customer.number, 9, 'r') + FormatUos.pad("", 12, 'r')
+ FormatUos.pad(Simulation.time - customer.arrivalTime, 11, 'r') // customer waiting time
+ FormatUos.pad(Simulation.time - startIdleTime, 11, 'r')); // cashier idle time
}
/** Print information for the scheduling of a finish service event.
Analysis: Time = O(1) */
public void traceScheduleFinishService()
{
System.out.println(FormatUos.pad("", 4, 'r') + FormatUos.pad("sched", 7, 'c')
+ FormatUos.pad("finish service", 14, 'l')
+ FormatUos.pad(customer.number, 9, 'r')
+ FormatUos.pad(Simulation.time + serviceTime, 12, 'r')); // scheduled finish time
}
/** Print information for a finish service event to facilitate tracing.
Analysis: Time = O(1) */
public void traceFinishService()
{
System.out.println(FormatUos.pad(Simulation.time, 4, 'r') + FormatUos.pad("", 7, 'c')
+ FormatUos.pad("finish service", 14, 'l') + FormatUos.pad(customer.number, 9, 'r'));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -