Factory Pattern HackerRank Solution

The Factory Design Pattern is a creational pattern that provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created. In this Java program, we have a Food interface and two classes, Pizza and Cake, that implement this interface. The FoodFactory class is used to create an instance of either Pizza or Cake based on the user's input.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the Factory Pattern HackerRank Solution HackerRank solution using CodeRankGPT:

The solution uses the Factory Design Pattern to create objects of different classes based on the user's input. The FoodFactory class has a method getFood() that takes a string as input. If the input string is 'Pizza', it creates and returns a new Pizza object. If the input string is 'Cake', it creates and returns a new Cake object. The main method in the JavaFactoryPattern class uses a scanner to get the user's input, creates the appropriate Food object using the FoodFactory, and then prints the type of food ordered.


/**
 * 
 */
package com.javaaid.hackerrank.solutions.languages.java.advanced;

import java.security.Permission;
import java.util.Scanner;

/**
 *
 */

interface Food {
	public String getType();
}

class Pizza implements Food {
	public String getType() {
		return "Someone ordered a Fast Food!";
	}
}

class Cake implements Food {

	public String getType() {
		return "Someone ordered a Dessert!";
	}
}

class FoodFactory {
	public Food getFood(String order) {

		/**
		 * main code starts from here
		 */

		if (order.equalsIgnoreCase("Pizza")) {
			return new Pizza();
		} else if (order.equalsIgnoreCase("Cake")) {
			return new Cake();
		}
		return null;

		/** main code end herer */

	}// End of getFood method

}// End of factory class

public class JavaFactoryPattern {

	public static void main(String args[]) {
		Do_Not_Terminate.forbidExit();
		Scanner sc = null;
		try {

			sc = new Scanner(System.in);
			// creating the factory
			FoodFactory foodFactory = new FoodFactory();

			// factory instantiates an object
			Food food = foodFactory.getFood(sc.nextLine());

			System.out.println("The factory returned " + food.getClass());
			System.out.println(food.getType());
		} catch (Do_Not_Terminate.ExitTrappedException e) {
			System.out.println("Unsuccessful Termination!!");
		} finally {
			sc.close();
		}
	}

}

class Do_Not_Terminate {

	public static class ExitTrappedException extends SecurityException {

		private static final long serialVersionUID = 1L;
	}

	public static void forbidExit() {
		final SecurityManager securityManager = new SecurityManager() {
			@Override
			public void checkPermission(Permission permission) {
				if (permission.getName().contains("exitVM")) {
					throw new ExitTrappedException();
				}
			}
		};
		System.setSecurityManager(securityManager);
	}
}

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform