Simple, Quality, Awesome Software

BMI

Below is the complete code for the BMI Try It Out! problem.

The Program Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BMI
{
    class Program
    {
        static void Main(string[] args)
        {
            // Build a list of "random" people.
            List<Person> people = new List<Person>();
            people.Add(new Person() { FirstName = "Link",
                Age = 26, Height = 72, Weight = 200 });

            people.Add(new Person() { FirstName = "Zelda",
                Age = 24, Height = 66, Weight = 140 });

            people.Add(new Person() { FirstName = "Sheik",
                Age = 24, Height = 66, Weight = 140 });
                // Well that's suspicious...

            people.Add(new Person() { FirstName = "Ganon",
                Age = 48, Height = 80, Weight = 290 });

            people.Add(new Person() { FirstName = "Ruto",
                Age = 21, Height = 63, Weight = 125 });

            people.Add(new Person() { FirstName = "Dampe",
               Age = 81, Height = 59, Weight = 170 });

            people.Add(new Person() { FirstName = "Darunia",
                Age = 35, Height = 70, Weight = 490 });

            // The query expression for finding overweight people.
            // For what it's worth, people with a lot of muscle are
            // often mislabeled as being overwheight. Medically speaking,
            // BMI is not enough by itself.
            IEnumerable<Person> overweightPeople =
                            from Person person in people
                            where CalculateBMI(person) > 25
                            select person;

            // The query expression for finding ideal weight people.
            IEnumerable<Person> idealWeightPeople =
                            from Person person in people
                            where CalculateBMI(person) <= 25
                            where CalculateBMI(person) >= 20
                            select person;

            Console.ReadKey();
        }

        /// <summary>
        /// Does the BMI calculation. Note that this assumes all units
        /// are in inches and pounds. There is a similar but different
        /// formula for cm and kg.
        /// </summary>
        /// <param name="person"></param>
        /// <returns></returns>
        private static int CalculateBMI(Person person)
        {
            return 703 * person.Weight / (person.Height * person.Height);
        }
    }
}

The Person Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BMI
{
    public class Person
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
        public int Height { get; set; }
        public int Weight { get; set; }
    }
}