Guest User

Untitled

a guest
Apr 23rd, 2021
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. package SandBox;
  2. public interface IDish {
  3. }
  4.  
  5. package SandBox;
  6. public class Coctail implements IDish {
  7.     double water;
  8.     double alcohol;
  9.     double topping;
  10.  
  11.     public Coctail(double water, double alcohol, double topping) {
  12.         this.water = water;
  13.         this.alcohol = alcohol;
  14.         this.topping = topping;
  15.     }
  16. }
  17.  
  18. package SandBox;
  19. public class Chips implements IDish{
  20.     double potatoes;
  21.     double salt;
  22.  
  23.     public Chips(double potatoes, double salt) {
  24.         this.potatoes = potatoes;
  25.         this.salt = salt;
  26.     }
  27. }
  28.  
  29. package SandBox;
  30. import java.lang.reflect.Constructor;
  31. import java.lang.reflect.InvocationTargetException;
  32.  
  33. public class DishFactory {
  34.     public static IDish getMyFuckingDish(double[] ingredients, Class<? extends IDish> dishType) throws IllegalAccessException, InvocationTargetException, InstantiationException {
  35.         Constructor<?>[] cons = dishType.getConstructors();
  36.         return dishType.cast(cons[0].newInstance((Object) ingredients));
  37.     }
  38.  
  39.     public static void main(String[] args) throws IllegalAccessException, InstantiationException, InvocationTargetException {
  40.         double[] ingredSet = {20, 30, 50};
  41.         Coctail cock = (Coctail) getMyFuckingDish(ingredSet, Coctail.class);
  42.     }
  43. }
Add Comment
Please, Sign In to add comment