Skip to content
Snippets Groups Projects
Commit 2c457f9c authored by Peter Hertkorn's avatar Peter Hertkorn
Browse files

Add dynamic loading of pizza classes (NY style)

parent 7c0cb856
No related merge requests found
package pizzeria;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Properties;
public class NYPizzaStore extends PizzaStore {
Pizza createPizza(String item) {
if (item.equals("cheese")) {
return new NYStyleCheesePizza();
} else if (item.equals("veggie")) {
return new NYStyleVeggiePizza();
} else if (item.equals("clam")) {
return new NYStyleClamPizza();
} else if (item.equals("pepperoni")) {
return new NYStylePepperoniPizza();
} else return null;
Pizza pizza = null;
Properties properties = new Properties();
try {
properties.load(this.getClass().getResourceAsStream("nypizza.properties"));
String pizzaName = properties.getProperty(item);
Class<?> classOfPizza = Class.forName(pizzaName);
Constructor<Pizza> constructor = (Constructor<Pizza>) classOfPizza.getConstructor(new Class[]{});
pizza = constructor.newInstance();
} catch (IOException | ClassNotFoundException | InstantiationException | IllegalAccessException |
NoSuchMethodException | InvocationTargetException e) {
e.printStackTrace();
}
return pizza;
}
}
cheese=pizzeria.NYStyleCheesePizza
clam=pizzeria.NYStyleClamPizza
pepperoni=pizzeria.NYStylePepperoniPizza
veggie=pizzeria.NYStyleVeggiePizza
salami=pizzeria.NYStyleSalamiPizza
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment