In this HackerRank Java Factory Pattern problem, you are given an interface Food. There are two classes Pizza and Cake which implement the Food interface, and they both contain a method getType().

The main function in the Main class creates an instance of the FoodFactory class. The FoodFactory class contains a method getFood(String) that returns a new instance of Pizza or Cake according to its parameter. you are given the partially completed code in the editor. Please complete the FoodFactory class.

HackerRank Java Factory Pattern problem solution

HackerRank Java Factory Pattern problem solution

Food foodType = null;

        if(order.equalsIgnoreCase("Pizza")) {
            Pizza myPizza = new Pizza();
            foodType = myPizza;
        }

        if(order.equalsIgnoreCase("Cake")) {
            Cake myCake = new Cake();
            foodType = myCake;
        }

        return foodType;