简体   繁体   中英

Comparing the ingredients of various lists of enums

I am trying to compare the ingredients of two Enum-Lists on a button click and I want to receive different messages based on the match.

More precisely: I have different recipes at hand and if my selected ingredients match with one of them I will receive a special message. If my ingredients don't match with anything I will receive a standard message.

Here is what I tried but did't worked properly:

public void DrinkButton_Click(object sender, RoutedEventArgs e)
{
    foreach (var recipe in RecipeList)
    {
        List<Ingredients> copy = new List<Ingredients>(selectedPotion.MyIngredients);

        if (copy.Count == recipe.Recipe.Count)
        {
            for (int i = copy.Count - 1; i >= 0; i--)
            {
                Ingredients item = selectedPotion.MyIngredients[i];

                if (recipe.Recipe.Contains(item))
                {
                    copy.Remove(item);
                }
                if (copy.Count == 0)
                {
                    recipe.DrinkEffect();
                }
            }
        }
        else
        {
            MessageBox.Show("Doesn't taste like anything!", "Announcement!", MessageBoxButton.OK, MessageBoxImage.Information);
        }
    }
}

You could use Linq's All to check if both Lists of Ingredients contain the same elements:

public void DrinkButton_Click(object sender, RoutedEventArgs e)
{
    if (selectedPotion == null)
    {
        MessageBox.Show("Please select a potion to drink", "Help Window", MessageBoxButton.OK, MessageBoxImage.Information);
        return;
    }

    foreach (var recipe in RecipeList)
    {
        bool equalIngredients = recipe.Recipe.All(selectedPotion.MyIngredients.Contains) &&
                                    recipe.Recipe.Count == selectedPotion.MyIngredients.Count;

        if (equalIngredients)
        {
            recipe.DrinkEffect();
            return;
        }
    }

    MessageBox.Show("Doesn't taste like anything!", "Announcement!",
                        MessageBoxButton.OK, MessageBoxImage.Information);
}

This will loop over all items in RecipeList and check if the item's Recipe equals selectedPotion.MyIngredients . If so it will call the DrinkEffect() method on the current item, otherwise it displays the "Doesn't taste like anything!"-MessageBox.

A few remarks:

  • recipe.Recipe just looks wrong, maybe go for a more precise naming
  • The code currently doesn't check if selectedPotion is null or not, i see potential for a NullReferenceException

Based on answers his my final solution: (I still received "System.NullReferenceException: 'Object reference not set to an instance of an object." error.)

    public void DrinkButton_Click(object sender, RoutedEventArgs e)
    {            
        foreach (var recipe in RecipeList)
        {               
            bool equalIngredients = recipe.Recipe.All(selectedPotion.MyIngredients.Contains) && recipe.Recipe.Count == selectedPotion.MyIngredients.Count;

            if (equalIngredients)
            {
                recipe.DrinkEffect();
                goto NextStep;
            }
        }
        MessageBox.Show("Doesn't taste like anything!", "Announcement!", MessageBoxButton.OK, MessageBoxImage.Information);
        NextStep: return;      
    }  

Here is latest:

    public void DrinkButton_Click(object sender, RoutedEventArgs e)
    {
        if (selectedPotion == null)
        {
            MessageBox.Show("Please select a potion to drink", "Help Window", MessageBoxButton.OK, MessageBoxImage.Information);
            return;
        }
        else
        {
            foreach (var recipe in RecipeList)
            {
                bool equalIngredients = recipe.Recipe.All(selectedPotion.MyIngredients.Contains) && recipe.Recipe.Count == selectedPotion.MyIngredients.Count;

                if (equalIngredients)
                {
                    recipe.DrinkEffect();
                    goto NextStep;
                }
            }
            MessageBox.Show("Doesn't taste like anything!", "Announcement!", MessageBoxButton.OK, MessageBoxImage.Information);
        NextStep: return;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM