|
| 1 | +/* |
| 2 | +According to Wikipedia, a factory is simply an object that returns another object from some other method call, which is assumed to be "new". |
| 3 | +In this 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(). |
| 4 | +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. |
| 5 | +
|
| 6 | +Sample Input 1 |
| 7 | +cake |
| 8 | +Sample Output 1 |
| 9 | +The factory returned class Cake |
| 10 | +Someone ordered a Dessert! |
| 11 | +*/ |
| 12 | + |
| 13 | +import java.util.*; |
| 14 | +import java.security.*; |
| 15 | +interface Food { |
| 16 | + public String getType(); |
| 17 | + } |
| 18 | + class Pizza implements Food { |
| 19 | + public String getType() { |
| 20 | + return "Someone ordered a Fast Food!"; |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + class Cake implements Food { |
| 25 | + |
| 26 | + public String getType() { |
| 27 | + return "Someone ordered a Dessert!"; |
| 28 | + } |
| 29 | + } |
| 30 | + class FoodFactory { |
| 31 | + public Food getFood(String order) { |
| 32 | + if (order.equalsIgnoreCase("cake")) { |
| 33 | + Food c = new Cake(); |
| 34 | + return c; |
| 35 | + } |
| 36 | + else { |
| 37 | + Food p = new Pizza(); |
| 38 | + return p; |
| 39 | + } |
| 40 | + |
| 41 | + }//End of getFood method |
| 42 | + |
| 43 | + }//End of factory class |
| 44 | + |
| 45 | + public class Factory { |
| 46 | + |
| 47 | + public static void main(String args[]){ |
| 48 | + Do_Not_Terminate.forbidExit(); |
| 49 | + |
| 50 | + try{ |
| 51 | + |
| 52 | + Scanner sc=new Scanner(System.in); |
| 53 | + //creating the factory |
| 54 | + FoodFactory foodFactory = new FoodFactory(); |
| 55 | + |
| 56 | + //factory instantiates an object |
| 57 | + Food food = foodFactory.getFood(sc.nextLine()); |
| 58 | + |
| 59 | + |
| 60 | + System.out.println("The factory returned "+food.getClass()); |
| 61 | + System.out.println(food.getType()); |
| 62 | + } |
| 63 | + catch (Do_Not_Terminate.ExitTrappedException e) { |
| 64 | + System.out.println("Unsuccessful Termination!!"); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + } |
| 69 | + class Do_Not_Terminate { |
| 70 | + |
| 71 | + public static class ExitTrappedException extends SecurityException { |
| 72 | + |
| 73 | + private static final long serialVersionUID = 1L; |
| 74 | + } |
| 75 | + |
| 76 | + public static void forbidExit() { |
| 77 | + final SecurityManager securityManager = new SecurityManager() { |
| 78 | + @Override |
| 79 | + public void checkPermission(Permission permission) { |
| 80 | + if (permission.getName().contains("exitVM")) { |
| 81 | + throw new ExitTrappedException(); |
| 82 | + } |
| 83 | + } |
| 84 | + }; |
| 85 | + System.setSecurityManager(securityManager); |
| 86 | + } |
| 87 | + } |
| 88 | + |
0 commit comments