- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackerrankJava0059.java
More file actions
Latest commit
84 lines (66 loc) · 2.26 KB
/
Copy pathHackerrankJava0059.java
File metadata and controls
84 lines (66 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
importjava.security.Permission;
importjava.util.Scanner;
publicclassHackerrankJava0059 {
publicstaticvoidmain(Stringargs[]) {
// Java Factory Pattern
// https://www.hackerrank.com/challenges/java-factory/problem?isFullScreen=true
Do_Not_Terminate.forbidExit();
try {
Scannersc = newScanner(System.in);
// creating the factory
FoodFactoryfoodFactory = newFoodFactory();
// factory instantiates an object
Foodfood = foodFactory.getFood(sc.nextLine());
System.out.println("The factory returned " + food.getClass());
System.out.println(food.getType());
} catch (Do_Not_Terminate.ExitTrappedExceptione) {
System.out.println("Unsuccessful Termination!!");
}
}
}
interfaceFood {
publicStringgetType();
}
classPizzaimplementsFood {
publicStringgetType() {
return"Someone ordered a Fast Food!";
}
}
classCakeimplementsFood {
publicStringgetType() {
return"Someone ordered a Dessert!";
}
}
classFoodFactory {
publicFoodgetFood(Stringorder) {
// -------------------------------------------------------------------------------------------------
// --- You need to add this lines for solution hackerrank provides all other lines for challange ---
// -------------------------------------------------------------------------------------------------
if (order.equals("pizza")) {
returnnewPizza();
} elseif (order.equals("cake")) {
returnnewCake();
} else {
thrownewIllegalArgumentException("Invalid food type");
}
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
}
}
classDo_Not_Terminate {
publicstaticclassExitTrappedExceptionextendsSecurityException {
privatestaticfinallongserialVersionUID = 1L;
}
publicstaticvoidforbidExit() {
finalSecurityManagersecurityManager = newSecurityManager() {
@Override
publicvoidcheckPermission(Permissionpermission) {
if (permission.getName().contains("exitVM")) {
thrownewExitTrappedException();
}
}
};
System.setSecurityManager(securityManager);
}
}