Guest User

Untitled

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