We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
class Cake implements Food{
public String getType(){
return"The factory returned class Cake\nSomeone ordered a Dessert!";
}
}
class Pizza implements Food{
public String getType(){
return"The factory returned class Pizza\nSomeone ordered a Fast Food!";
}
}
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner s = new Scanner(System.in);
String str =s.next();
if(str.compareTo("cake") ==0){
Food obj = new Cake();
System.out.println(obj.getType());
}
if(str.compareTo("pizza") ==0){
Food obj = new Pizza();
System.out.println(obj.getType());
}
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
An unexpected error occurred. Please try reloading the page. If problem persists, please contact support@hackerrank.com
Java Factory Pattern
You are viewing a single comment's thread. Return to all comments →
my Code :
import java.io.; import java.util.;
interface Food {
}
class Cake implements Food{ public String getType(){
}
class Pizza implements Food{ public String getType(){
}
public class Solution {
}