1

In main thread user can give order from console after selecting the option to get the order . After selecting the option I am firing one thread chef.start(); which is responsible to get the customer from the queue and prepare their order. once the order is prepared I have listener which is called from thread and listener simply show the message .

Problem is my main thread is getting dead even after my code is loop I am not able to give further input to ask for another order .

public class Driver {
    private static LinkedBlockingQueue<Customer> waitingCustomer=new LinkedBlockingQueue<Customer>();


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        OrderPrepared op=new OrderPrepared(waitingCustomer);

        while (true) {
            System.out.println("would you like to place new Order Press Y or N");
            String input = sc.nextLine();
            if ("y".equalsIgnoreCase(input)) {
                Customer customer = null;
                System.out.println("Enter Customer Name");
                String name = sc.nextLine();
                System.out.println("What would you like to order");
                System.out.println(" ");
                System.out.println("Our Menu Card ");
                System.out.println(" ");
                System.out.println("Item Name\t\t" + "     Price\t\t" + "Press to order");
                System.out.println(" ");
                System.out.println("Cheese And Tomato\t\t" + "60\t\t" + "1");
                System.out.println("Margerita        \t\t" + "40\t\t" + "2");
                System.out.println("Burger           \t\t" + "50\t\t" + "3");
                System.out.println("Chicken Fried    \t\t" + "90\t\t" + "4");
                System.out.println(" ");
                System.out.println("Please Press Number to place order");
                int number=sc.nextInt();
                Food food=null;

                switch(number) {
                case 1: 
                    food=new Food("Cheese And Tomato", 60.0d);
                case 2:
                    food=new Food("Margerita", 40.0d);
                case 3:
                    food=new Food("Burger", 50.0d);
                case 4:
                    food=new Food("Chicken Fried", 90.0d);
                }
                Random ran=new Random(709);
                customer=new Customer(name,new Order(ran.nextInt(), food));
                waitingCustomer.add(customer);
                Chef chef=new Chef();
                chef.setChefListener(op);
                chef.setCustomers(waitingCustomer);
                chef.setFood(food);
                chef.start();

            } else {
                break;
            }
        }

    }

}


public class Chef extends Thread {


    Food food;
    ChefListener chefListener;
    LinkedBlockingQueue<Customer> customers;

    public Chef() {

    }
    public Chef(Food piza, ChefListener chefListener, LinkedBlockingQueue<Customer> customers) {
        super();
        this.food = piza;
        this.chefListener = chefListener;
        this.customers = customers;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        for (Customer customer : customers) {
            chefListener.pizzaCooked(customer, food);
            customers.remove();
            System.out.println("customer has been removed from queue");
        }

    }
    public Food getFood() {
        return food;
    }

    public void setFood(Food food) {
        this.food = food;
    }

    public ChefListener getChefListener() {
        return chefListener;
    }

    public void setChefListener(ChefListener chefListener) {
        this.chefListener = chefListener;
    }

    public LinkedBlockingQueue<Customer> getCustomers() {
        return customers;
    }

    public void setCustomers(LinkedBlockingQueue<Customer> customers) {
        this.customers = customers;
    }
}



public class OrderPrepared implements ChefListener{
    Logger logger=Logger.getLogger("OrderPrepared");
    public OrderPrepared(LinkedBlockingQueue<Customer> waitingCustomer) {

    }

    @Override
    public void pizzaCooked(Customer customer, Food food) {
        // TODO Auto-generated method stub
        logger.info(customer.getName()+" "+food.getName() + " has been cooked. Enjoy");

    }

}


Output
import java.util.concurrent.LinkedBlockingQueue;
import java.util.logging.Logger;

public class OrderPrepared implements ChefListener{
    Logger logger=Logger.getLogger("OrderPrepared");
    public OrderPrepared(LinkedBlockingQueue<Customer> waitingCustomer) {
        // TODO Auto-generated constructor stub
    }

    @Override
    public void pizzaCooked(Customer customer, Food food) {
        // TODO Auto-generated method stub
        logger.info(customer.getName()+" "+food.getName() + " has been cooked. Enjoy");

    }

}

Output

would you like to place new Order Press Y or N
Y
Enter Customer Name
Kalis
What would you like to order

Our Menu Card 

Item Name            Price      Press to order

Cheese And Tomato       60      1
Margerita               40      2
Burger                  50      3
Chicken Fried           90      4

Please Press Number to place order
2
would you like to place new Order Press Y or N
customer has been removed from queue
Nov 12, 2019 2:32:45 AM OrderPrepared pizzaCooked
INFO: Kalis Chicken Fried has been cooked. Enjoy
ankur jadiya
  • 129
  • 2
  • 3
  • 11

1 Answers1

2

nextInt() does not consume the LF. Follow the nextInt() with nextLine() so that the line terminator (lf) is consumed. Without this, the nextLine at the top of the loop will exit with an empty line.

Also see Scanner is skipping nextLine() after using next() or nextFoo()?

fedup
  • 1,045
  • 8
  • 22