简体   繁体   English

查找热量最高的产品的方法c#

[英]method to find the product with the highest number of calories c#

I have to find the product with the highest number of calories. 我必须找到卡路里含量最高的产品。 My program knows to print all the products with their calories and now I'm working how to print only the product with the highest number. 我的程序知道要打印所有带有卡路里的产品,现在我正在研究如何仅打印数量最多的产品。

I have my class Food: 我上课有食物:

 class Food : Product{
 private NutritionalData nutritionalData;
    private DateTime expirationTerm;
    public FoodCategory Category;

    public Food(int ProductId, string SKU, string Name, decimal Price, string Description, string Producer,
        NutritionalData nutritionalData, FoodCategory Category, DateTime expirationTerm)
        : base(ProductId, SKU, Name, Price, Description, Producer)
    {

        this.nutritionalData = nutritionalData;
        this.Category = Category;
        this.expirationTerm = expirationTerm;
    }
    public Food() { }
 public NutritionalData GetNutritionalData()
    {
        return nutritionalData;
    }
    public int GetCalories()
    {
        return nutritionalData.Calories;
    }

    public override void PrintCalories()
    {

            int i = this.GetCalories();
            Console.WriteLine("Product: {0}, Calories: {1},ProductId: {2}", this.GetName(), i, this.GetProductId());

    }
    }

//here I have some class and some struct but there is no relation with my idea so I don't put here all the stuff. //这里我有一些类和一些结构,但是与我的想法没有关系,所以我不会在这里放所有东西。 And here is the main: 这是主要的:

class Program
{
    static void Main(string[] args)
    {
        Product f = new Food(1, "a", "Food", 15, "Good Quality", "Sana",new NutritionalData(2,12,1,3,2) //(2=calories,12=,1,3,2=doens't matter,
            FoodCategory.Meat, new DateTime(2015,4,12));
        Product f1 = new Food(2, "v", "Pizza1", 2, "Bad Quality", "Ariel", new NutritionalData(8, 17, 5, 3, 2),
            FoodCategory.Meat, new DateTime(2015, 4, 11));
        f.PrintCalories();
        f1.PrintCalories();
    }
}
}

Sorry if you don't understand many things but first I wanted to put here only a few things but then I thought you wouldn't have understood nothing.What's important is that I don't know how to verify all the products, for example if I have the first one f, it has nr of calories=2, how can I verify the following product if has a number of calories greater of smaller than this one and so on? 抱歉,如果您不了解很多事情,但首先我只想在这里说几件事,然后我以为您不会一无所知。重要的是,我不知道如何验证所有产品,例如如果我有第一个f,则它的nr卡路里= 2,如果卡路里的数量大于或小于该卡路里,我如何验证以下产品? With the method GetCalories I return the product with the calories but now I'm stucked. 使用GetCalories方法,我将卡路里归还给产品,但现在我被卡住了。 Some ideas? 有什么想法吗? If you don't understand what I want to do please tell me.Thank you. 如果您不明白我要做什么,请告诉我。谢谢。

Update: I tried something but without result: 更新:我尝试了一些但没有结果:

 public override void PrintCalories()
    {
        int max = 0;
        int i = this.GetCalories();
        if (i > max)
        {
            Console.WriteLine("Product: {0}, Calories: {1},ProductId: {2}", this.GetName(), i, this.GetProductId());
        }
        else
            Console.WriteLine("Product: {0}, Calories: {1},ProductId: {2}", this.GetName(), max, this.GetProductId());
        max = i;

    }

In order for you to print the product with the highest calories, you need to keep track of that in some way as you're dealing with your new product instances. 为了打印具有最高卡路里的产品,您在处理新产品实例时需要以某种方式进行跟踪。

I would suggest you interrogate each instance and maintain a reference to the instance that has the highest calories value. 我建议您询问每个实例并维护对具有最高卡路里值的实例的引用。 For example: 例如:

var highestCaloriesSoFar = f;  // store your first instance since you haven't seen any others yet.

// --- whatever code you ultimately have in between getting each instance. 

if (nextInstance.NutritionData.Calories > highestCaloriesSoFar.NutritionData.Calories) 
    highestCaloriesSoFar = nextInstance;

Then you could just print that instance at the end. 然后,您可以只在最后打印该实例。

Now, I'm saying this without actually knowing how you're going to ultimately code this because it looks like what you have in your Main is a work in progress. 现在,我说这句话的时候并没有真正知道如何最终编写此代码,因为您的Main中的内容似乎正在进行中。 It seems unlikely that you are just going to hard-code, as it were, a bunch of instances directly in your main method and more likely that you are going to receive them or build them externally and then produce the output from that collection. 似乎不太可能直接在main方法中对一堆实例进行硬编码,并且更有可能您将要接收它们或在外部构建它们,然后该集合中产生输出。 Therefore, you could have a method utilizing the code above that takes a collection of Food instances and returns the one with the highest calories: 因此,您可以使用上面的代码来获取一组Food实例并返回卡路里最高的实例:

private static Food GetHighestCalories(IEnumerable<Food> fromCollection)
{
    Food highestCaloriesSoFar = null;

    foreach (Food nextInstance in fromCollection)
    {
        if(highestCaloriesSoFar != null)
        {
            if (nextInstance.NutritionData.Calories > highestCaloriesSoFar.NutritionData.Calories) 
                highestCaloriesSoFar = nextInstance;
        }
        else // first loop iter.
        {
            highestCaloriesSoFar = nextInstance;
        }
    }

    return highestCaloriesSoFar;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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