Java Program: Quick Eat Restaurant


Objective

 Using queue in an application 

Task statement


QuickEat is a popular fast-food restaurant. As with most fast-food restaurants, QuickEat provides a limited set of food items in its menu. These food items are prepared continuously and the customers are served on a first-come-first-serve basis. A customer may order multiple dishes and it is not necessary to wait till all his dishes are ready before they are served to him. Instead, each dish is served the moment it is ready. When a dish is ready but there is no order for it, it will be thrown away (what a waste!). A customer is assigned a tag number for identification. 




Fish n chips




Chicken chop




Grilled salmon


Image sources: http://www.queenfishandchips.com/about-us.php, http://cynthij.blogspot.sg/2010/07/krazi-nights-with-kei-charles-aka.html, http://www.mccormick.com/Lawrys/Recipes/Main-Dishes/Mediterranean-Grilled-Salmon-with-Spinach-Salad


Input


The first line in the input consists of a positive integer N, which is the number of food items on the QuickEat menu. The next N lines are the names of dishes served in QuickEat. Each dish is given an identification number starting from 1, in the order they appear in the input. The next line consists of a positive integer K, which is the number of instructions.  This is followed by K lines, each of which can be either an order instruction or a ready instruction. The instructions are ordered according to the time they are issued.  An order instruction starts with the word ‘Order’, followed by the tag number assigned to the customer, followed by a positive integer D which is the number of dishes this customer has ordered. This is then followed by D integers (whose values are from 1 to N) specifying the dishes selected in this customer’s order. Tag numbers can be re-used. A ready instruction starts with the word ‘Ready’ followed by a positive integer specifying which dish is ready. It is possible that some customers might not get their food at the end of input.  Note that short symbols such as N, K and D are used above for convenience. In your program, you are expected to give them descriptive variable names. 

Output

For each ‘Ready’ instruction, print out the instruction to the server in the following format:  [Dish name] ready to be served to Tag [Tag number]. where [Dish name] is the name of the dish that is ready and [Tag number] refers to the customer who placed this order. If nobody is currently waiting for the dish, print the line  Throw away [Dish name]. 

Sample input

Fish n Chips 
Chicken Chop 
Grilled salmon 
Order 1 2 1 3 
Ready 3 
Order 2 1 2 
Order 3 2 1 1 
Ready 1 
Ready 1 
Ready 2 
Ready 2 
Order 1 1 2 

Sample output

Grilled salmon ready to be served to Tag 1. 
Fish n Chips ready to be served to Tag 1. 
Fish n Chips ready to be served to Tag 3. 
Chicken Chop ready to be served to Tag 2. 
Throw away Chicken Chop. 

Explanation


For the dishes:  

1 represents “Fish n Chips”,  
2 represents “Chicken Chop” and  
3 represents “Grilled salmon”.  

The first order is given tag 1, and he orders 2 dishes: dish 1 (“Fish n Chips”) and dish 3 (“Grilled salmon”). At this point, the queue for food consists of:  

“Fish n Chips” (to be given to tag 1), “Grilled salmon” (tag 1) 

Next, dish 3 (“Grilled salmon”) is ready, so it is given to the first customer in queue who ordered it: tag 1. The queue now consists of a single order: 

“Fish n Chips” (tag 1)  

After the next 2 orders, the queue is now: 

“Fish n Chips” (tag 1), “Chicken Chop” (tag 2), “Fish n Chips” (tag 3), “Fish n Chips” (tag 3)  

Dish 1 (“Fish n Chips”) is ready, so it is given to the first customer who ordered it, which is tag 1. The queue is now: 

 “Chicken Chop” (tag 2), “Fish n Chips” (tag 3), “Fish n Chips” (tag 3)  

Next, dish 1 (“Fish n Chips”) is ready again. It is given to the first customer who ordered it, tag 3. The queue now looks like: 

 “Chicken Chop” (tag 2), “Fish n Chips” (tag 3)  

Dish 2 (“Chicken Chop”) is then ready, given to tag 2, leaving the queue as: 

 “Fish n Chips” (tag 3)  

Another dish 2 (“Chicken Chop”) is ready. However, since there is no one in the queue who ordered Chicken Chop, it is thrown away.

Finally, a new order for chicken chop comes in for tag 1. The final queue is:        

“Fish n Chips” (tag 3), “Chicken Chop” (tag 1) 

Solution


// This program helps staff manage customers'
// orders and decide who should be given a ready dish.

import java.util.*;

// This class represents all orders of customers
class ListOrder {
    
    // Data member
    private int numDishes;
    // All dishes which the restaurant offers
    private String[] dishes;
    // Each dish has a queue of customers who ordered this dish
    // All such queues are put inside an ArrayList called dishQueues
    private ArrayList<Queue<Integer>> dishQueues;
    
    // Constructor
    public ListOrder(int numDishes, Scanner sc) {
  dishes = new String[numDishes];
  dishQueues = new ArrayList<Queue<Integer>>();

  for (int i=0; i<numDishes; i++) {
   dishes[i] = sc.nextLine();
   dishQueues.add(new LinkedList<Integer>());
  }
    }
    
    // Add new order to the list
    public void addNewOrder(int dishID, int tag) {
       dishQueues.get(dishID-1).offer(tag-1); 
    }
    
    // Process food when it is ready
    // Return the customer who currently ordered for the dish
    // if there is no customer order for this dish return -1
    public int processReadyFood(int dishID) {
  if (dishQueues.get(dishID-1).isEmpty())
   return -1;
  else
   return dishQueues.get(dishID-1).poll()+1;
    }

    // Get dish's name
    public String getDishName(int dishID) {
  return dishes[dishID-1];
    }

}

public class QuickEat {

    public static void main(String [] args) {
        
        Scanner sc = new Scanner(System.in);
        int numDishes = sc.nextInt();
        sc.nextLine();
        
        // Create the list order of food
        ListOrder listOrder = new ListOrder(numDishes, sc);
        
        int noOfCommands = sc.nextInt();
        sc.nextLine();
        
        for (int i=0; i<noOfCommands; i++) {
   String op = sc.next();
   
   switch(op) {
    case "Order":
    int tagID = sc.nextInt();
    int numOrders = sc.nextInt();
    for (int j=0; j<numOrders; j++) {
     int order = sc.nextInt();
     listOrder.addNewOrder(order, tagID);
    }
    break;

    case "Ready":
    int dishID = sc.nextInt();
    int customer = listOrder.processReadyFood(dishID);

    if (customer != -1)
     System.out.println(listOrder.getDishName(dishID) + " ready to be served to Tag " + customer + ".");
    else
     System.out.println("Throw away " + listOrder.getDishName(dishID) +  ".");
    break;
   }

  }

    }
}