public void msgNewOrder(WaiterAgent w, int tableNum, String order) {
    print("msgNewOrder() from Waiter " + w.getName());
    long timeFinish = -1;
    if (findFood(order).amount > 0) {
      timeFinish =
          (System.currentTimeMillis() + (long) ((menu.menuItems.get(order)) * Constants.SECOND));
      int previousInventoryAmount = inventory.get(order);
      inventory.put(order, previousInventoryAmount - 1);
      print("Number of items of type " + order + " left: " + inventory.get(order));

      Order incomingOrder = new Order(w, tableNum, order, timeFinish);
      orders.add(incomingOrder);
      Food f = findFood(order);
      f.amount--;
      if (f.amount <= f.low) {
        marketOrders.add(new MarketOrder(f.choice, f.capacity - f.amount));
        f.ordered = true;
      }
    } else {
      Order incomingOrder = new Order(w, tableNum, order, timeFinish);
      incomingOrder.state = OrderState.UnableToBeSupplied;
      menu.menuList.remove(order);
      orders.add(incomingOrder);
      Food f = findFood(order);
      marketOrders.add(new MarketOrder(f.choice, f.capacity - f.amount));
    }
    stateChanged();
  }
  protected boolean pickAndExecuteAnAction() {

    synchronized (orders) {
      for (Order o : orders) {
        if (o.state == OrderState.UnableToBeSupplied) {
          TellWaiterWeAreOut(o);
          return true;
        }
      }
    }
    synchronized (orders) {
      for (Order o : orders) {
        if (o.state == OrderState.NotReady) {
          o.state = OrderState.BeingPrepared;
          PrepareOrder(o);
          return true;
        }
      }
    }
    synchronized (orders) {
      for (Order o : orders) {
        if (o.state == OrderState.Ready) {
          OrderIsReady(o);
          return true;
        }
      }
    }
    synchronized (marketOrders) {
      for (MarketOrder mo : marketOrders) {
        if (mo.os == moState.pending) {
          print("Ordering " + mo.amount + " " + mo.food + "'s");
          mo.os = moState.ordered;
          print("COWABUNGA ordering from market");
          goToMarket(restaurant, "Italian", mo.amount, mo.id);
          return true;
        }
      }
    }
    // Producer-consumer handling
    StandOrder orderFromStand = restaurant.revolvingStand.remove();
    if (orderFromStand != null) {

      WaiterAgent standWaiter = orderFromStand.waiter;
      int standTableNum = orderFromStand.tableNum;
      String standOrderType = orderFromStand.order;

      if (menu.menuItems.containsKey(orderFromStand.order)
          && inventory.get(orderFromStand.order) > 0) {
        long timeFinish =
            (System.currentTimeMillis()
                + (long) ((menu.menuItems.get(standOrderType)) * Constants.SECOND));
        Order newStandOrder = new Order(standWaiter, standTableNum, standOrderType, timeFinish);
        AddNewOrderFromStand(newStandOrder);
      }
      return true;
    }
    return false;
  }
 private void OrderIsReady(Order o) {
   print("OrderIsReady() called: " + o.order + " ready for table #: " + o.tableNum);
   DoGoToCookingArea();
   try {
     atDestination.acquire();
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
   o.state = OrderState.GoingToPlating;
   cookGui.DoDisplayOrder(o.order + "(d)");
   cookGui.DoDisplayCookingOrders(getCookingOrders());
   DoGoToPlatingArea();
   try {
     atDestination.acquire();
   } catch (InterruptedException e) {
     e.printStackTrace();
   }
   cookGui.DoDisplayOrder("");
   WaiterAgent w = o.waiter;
   w.msgOrderReady(o.tableNum, o.order);
   orders.remove(o);
 }
  private void PrepareOrder(Order o) {
    print("PrepareOrder() called");
    DoGoToFridge();
    try {
      atDestination.acquire();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    cookGui.DoDisplayOrder(o.order + "(nd)");
    DoGoToCookingArea();
    try {
      atDestination.acquire();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
    o.state = OrderState.Cooking;
    cookGui.DoDisplayOrder("");
    cookGui.DoDisplayCookingOrders(getCookingOrders());
    final Order theOrder = o;
    long currentTime = System.currentTimeMillis();
    // sets a timer that will notify when order is done cooking

    timer.schedule(
        new TimerTask() {
          public void run() {
            print("Finished order");
            msgOrderDone(theOrder);
            stateChanged();
          }
        },
        Math.max(o.finishTime - currentTime, 1)); // how long to wait before running task
    DoGoToPlatingArea();
    try {
      atDestination.acquire();
    } catch (InterruptedException e) {
      e.printStackTrace();
    }
  }
 public void msgOrderDone(Order o) {
   o.state = OrderState.Ready;
   stateChanged();
 }