我正在使用Event对象的优先级队列(是的,家庭作业)为快餐驱动器建模。有三个站,一个订单,付款和收集站,每个站都有自己的队列。我在收集站(客户访问的最后一站)中删除项目时遇到问题。当收集队列填满时,模拟开始无限循环,计时器不再递增。我认为这是因为这一行:
public void processFoodCollection(Customer c, Event e) {
collection.remove(c);//this is the issue I believe
collection.setServerStatus(false);
但是,如果我尝试使用我的标准remove()方法(它只是在station类中调用queue.poll()),它将返回null。我不知道为什么在刚刚添加到队列之后会发生这种情况,在其他站点使用它时,它给我零问题(我只是为每个站点复制粘贴方法,它们是都一样)。我非常感谢有关识别导致此循环的原因的任何帮助,或者,如果是删除(c),我将如何解决此问题。
这是我的餐厅课程(包含模拟,我知道很懒),但希望没有必要查看,因为我的文档尚未完成:http://pastebin.com/cHj3xqJN[1]
这是方法,特别是我遇到的问题(集合是一个由队列字段组成的CollectionStation var,remove(Customer c)和remove()方法只是用于访问队列方法):
public void processFoodCollection(Customer c, Event e) {
collection.remove(c);//Customer c should be head of queue
collection.setServerStatus(false);//cashier not helping anyone
if (collection.getQueueSize() < 2) {
//process event if room available in collection queue
collection.add(payment.remove());//remove customer from payment queue, add to collection queue
payment.setServerStatus(false);
if (!collection.getServerStatus())
createFinishCollection(collection.peek());//creates event to be processed in the future
//generate new finish payment event for new head of payment queue
if (payment.getQueueSize()>0) {
double b = this.exponentialPay.next();//minutes until finished paying
while (b == 0.0) {
b = this.exponentialPay.next();//ensure return > 0.0
}
createFinishPayment(b, payment.peek());
}
//check if head of order queue can move up
if (order.getQueueSize() > 0) {
if (order.peek().getTimeFinished() == this.clockTime && order.getServerStatus()) {
processFinishOrder(order.peek(), eventList.peek());
} else if (!order.getServerStatus()) {
double timeToOrder = (order.peek().getOrderSize() * this.timeToOrderItem);
createFinishOrder(timeToOrder, order.peek());
}
}
}
}//end method processFoodCollection
如果我尝试使用remove()方法,则会在以下位置获得空指针异常:
Event newE = new Event(Events.FINISHFOODCOLLECTION, this.clockTime + (c.getOrderSize() * this.timeToProcessItem), c);
这是我正在调用的remove方法(在CollectionStation类中):
/**
* Removes and returns object at front of queue.
* @return
*/
public Customer remove() {
return customerQueue.poll();
}//end method remove
老实说,我很难过,为什么这不起作用。任何指导都将受到赞赏(不寻求答案,只是帮助)。