Exemplo n.º 1
0
    public Beverage ToBeverage()
    {
        //What project/assembly to look through
        Assembly assembly = typeof(EspressoMachine).Assembly;
        //The parent class that we're checking compatibility with
        Type target = typeof(Beverage);
        //Get all types that appear in the assembly that are inheriting from Beverage
        var types = assembly.GetTypes()
                    .Where(type => target.IsAssignableFrom(type) && type.Name != "Beverage" && type.Name != "CustomBeverage");

        //Check ingredients for every matching class
        foreach (Type type in types)
        {
            //Instantiate a new beverage to use for checking
            var beverage = (Beverage)Activator.CreateInstance(type);
            if (beverage.CheckIngredients(Ingredients))
            {
                return(beverage);
            }
        }

        //No matching recipes found
        CustomBeverage result = new CustomBeverage();

        result.SetIngredientList(Ingredients);
        return(result);
    }
Exemplo n.º 2
0
    public IBeverage ToBeverage()
    {
        IBeverage beverageToServe;

        beverageToServe = new CustomBeverage {
            Ingredients = this.Ingredients
        };
        bool IsComparableList = false;
        var  avaibleBevs      = avaibleBeverages.Where(bev => this.Ingredients.Count.Equals(bev.Ingredients.Count)).ToList();

        foreach (var bevToCheck in avaibleBevs)
        {
            // The beverage to be served contains only ingredients from the the avaible beverage types in this class
            IsComparableList = this.Ingredients.All(ingredient => bevToCheck.Ingredients.Contains(ingredient));
            if (IsComparableList == true)
            {
                beverageToServe = bevToCheck;
            }
        }
        BeverageDeclaration(beverageToServe);
        return(beverageToServe);
    }