Body Fat Calculator

BodyFatCalculator

BodyFatCalculator2

This was my first venture into saving files. Also I’ve been toying around with ideas on user interface design and notifications outside of just regular popup dialog boxes. So I expanded one of my class assignments for a body fat calculator. I wanted to have a logfile to save all of the calculations made in a text file. And I wanted to use a user selected debug mode to show the calculations being made if need be. I had to use 2 sets of calculations based on being either male or female.

For the user interface notifications I had the text turn red if the information wasn’t entered correctly. I also used tooltips so the user could hover their mouse over the red text to see the error and how to fix it. Again I separated the program into 2 classes. One class for the interface tasks while the other took care of the calculations and held the data.

BodyFatCalculator Class:


using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace BodyFatCalculator
{
    public partial class BodyFatCalculator : Form
    {
        Calculate myCalculate = new Calculate();

        public BodyFatCalculator()
        {
            InitializeComponent();

            //Default to female
            femaleRadioButton.Checked = true;
        }

        private void closeButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void calculateButton_Click(object sender, EventArgs e)
        {
            //Setting all the unit labels to the color black.
            weightUnitLabel.ForeColor = Color.Black;
            waistUnitLabel.ForeColor = Color.Black;
            wristUnitLabel.ForeColor = Color.Black;
            hipsUnitLabel.ForeColor = Color.Black;
            forearmUnitLabel.ForeColor = Color.Black;

            //See if male since female is the default.
            if (maleRadioButton.Checked)
                myCalculate.IsFemale = false;

            //See if the debug mode box is checked.
            if (debugCheckBox.Checked)
                myCalculate.EnableDebugging = true;
            
            //Attempt to convert the textbox values to doubles, marking the unit labels red for failure.
            double weightFactor = 0;
            double waistFactor = 0;
            double wristFactor = 0;
            double hipsFactor = 0;
            double forearmFactor = 0;

            if (!double.TryParse(weightTextBox.Text, out weightFactor))
                weightUnitLabel.ForeColor = Color.Red;
            if (!double.TryParse(waistTextBox.Text, out waistFactor))
                waistUnitLabel.ForeColor = Color.Red;

            if (myCalculate.IsFemale)
            {
                if (!double.TryParse(wristTextBox.Text, out wristFactor))
                    wristUnitLabel.ForeColor = Color.Red;
                if (!double.TryParse(hipsTextBox.Text, out hipsFactor))
                    hipsUnitLabel.ForeColor = Color.Red;
                if (!double.TryParse(forearmTextBox.Text, out forearmFactor))
                    forearmUnitLabel.ForeColor = Color.Red;
            }

            //Lets make sure all the values are above zero.
            if (weightFactor <= 0)
                weightUnitLabel.ForeColor = Color.Red;
            if (waistFactor <= 0)
                waistUnitLabel.ForeColor = Color.Red;
            
            if (myCalculate.IsFemale)
            {
                if (wristFactor <= 0)
                    wristUnitLabel.ForeColor = Color.Red;
                if (hipsFactor <= 0)
                    hipsUnitLabel.ForeColor = Color.Red;
                if (forearmFactor <= 0)
                    forearmUnitLabel.ForeColor = Color.Red;
            }

            //Seeing if there are any errors by checking the unit labels color with a conditional operators.
            //Weight
            setToolTip(weightLabel, weightTextBox, weightUnitLabel, weightUnitLabel.ForeColor == Color.Red ?
                "Weight (pounds) must be numeric and above 0." : "");

            //Waist
            setToolTip(waistLabel, waistTextBox, waistUnitLabel, waistUnitLabel.ForeColor == Color.Red ?
                "Waist (inches) must be numeric and above 0." : "");

            if (myCalculate.IsFemale)
            {
                //Wrist
                setToolTip(wristLabel, wristTextBox, wristUnitLabel, wristUnitLabel.ForeColor == Color.Red ?
                    "Wrist (inches) must be numeric and above 0." : "");

                //Hips
                setToolTip(hipsLabel, hipsTextBox, hipsUnitLabel, hipsUnitLabel.ForeColor == Color.Red ?
                    "Hips (inches) must be numeric and above 0." : "");

                //Forearm
                setToolTip(forearmLabel, forearmTextBox, forearmUnitLabel, forearmUnitLabel.ForeColor == Color.Red ?
                    "Forearm (inches) must be numeric and above 0." : "");
            }

            //Checking for errors. If no errors then the calculations run. If there are errors a message will display letting the user know.
            if (myCalculate.IsFemale)
                myCalculate.IsError = (weightUnitLabel.ForeColor == Color.Red || waistUnitLabel.ForeColor == Color.Red ||
                    wristUnitLabel.ForeColor == Color.Red || hipsUnitLabel.ForeColor == Color.Red || forearmUnitLabel.ForeColor == Color.Red);
            else
                myCalculate.IsError = (weightUnitLabel.ForeColor == Color.Red || waistUnitLabel.ForeColor == Color.Red);

            if (!myCalculate.IsError)
            {
                bodyFatLabel.Visible = true;
                bodyFatLabel.ForeColor = Color.Red;

                myCalculate.BodyFatPercentage = (myCalculate.calculateBodyFat(weightFactor, wristFactor, waistFactor, hipsFactor, forearmFactor) 
                    * 100) / weightFactor;
                bodyFatLabel.Text = "Body Fat Percentage: " + myCalculate.BodyFatPercentage.ToString("#0.00") + "%";
            }
            else
            {
                bodyFatLabel.Visible = true;
                bodyFatLabel.ForeColor = Color.Red;
                bodyFatLabel.Text = "Hover the mouse over the red text to see the error message.";
            }

            //Need to write some info to the debug and regular logs.
            writeToDebug();
        }

        private void setToolTip(Control labelControl, Control textBoxControl, Control labelUnitControl, string toolText)
        {
            ToolTip bodyfatToolTip = new ToolTip();

            //Setting the tooltips for each of the controls.
            bodyfatToolTip.SetToolTip(labelControl, toolText);
            bodyfatToolTip.SetToolTip(textBoxControl, toolText);
            bodyfatToolTip.SetToolTip(labelUnitControl, toolText);
        }

        //Output the text data, and if applicable, debug log data to a text file.
        private void writeToLog(string debugInfo, string bodyFatWeight, string isFemale, string isError)
        {
            //Getting the todays time and date.
            DateTime today = DateTime.Now;
            
            //Getting the path of the application and the location of the log files.
            string logFilePath = Application.StartupPath + @"\logFiles\log.txt";
            string debugLog = Application.StartupPath + @"\logFiles\debug.txt";

            //Formatting the contents of teh log files.
            StringBuilder logFileUpdate = new StringBuilder();
            StringBuilder debugFileUpdate = new StringBuilder();
            logFileUpdate.AppendLine(today.ToString());    //Adding the time and date to the logfile.

            if (!myCalculate.IsFemale)
            {
                logFileUpdate.AppendLine("Sex: Male");
                logFileUpdate.AppendLine("Weight: " + myCalculate.WeightFactor.ToString());
                logFileUpdate.AppendLine("Waist: " + myCalculate.WaistFactor.ToString());
                logFileUpdate.AppendLine("Bodyfat: " + myCalculate.BodyFatPercentage.ToString());
                logFileUpdate.AppendLine("====================================================");
                
                //Checking to see if the debug box is checked.
                if (myCalculate.EnableDebugging)
                {
                    debugFileUpdate.AppendLine(debugInfo);
                    debugFileUpdate.AppendLine(bodyFatWeight);
                    debugFileUpdate.AppendLine(isFemale);
                    debugFileUpdate.AppendLine(isError);
                    debugFileUpdate.AppendLine("====================================================");
                }
            }
            else
            {
                logFileUpdate.AppendLine("Sex: Female");
                logFileUpdate.AppendLine("Weight: " + myCalculate.WeightFactor.ToString());
                logFileUpdate.AppendLine("Waist: " + myCalculate.WaistFactor.ToString());
                logFileUpdate.AppendLine("Wrist: " + myCalculate.WristFactor.ToString());
                logFileUpdate.AppendLine("Hips: " + myCalculate.HipsFactor.ToString());
                logFileUpdate.AppendLine("Forearm: " + myCalculate.ForearmFactor.ToString());
                logFileUpdate.AppendLine("Bodyfat: " + myCalculate.BodyFatPercentage.ToString());
                logFileUpdate.AppendLine("====================================================");

                //Checking to see if the debug box is checked.
                if (myCalculate.EnableDebugging)
                {
                    debugFileUpdate.AppendLine(debugInfo);
                    debugFileUpdate.AppendLine(bodyFatWeight);
                    debugFileUpdate.AppendLine(isFemale);
                    debugFileUpdate.AppendLine(isError);
                    debugFileUpdate.AppendLine("====================================================");
                }
            }

            //Add the contents to the logfile. Appending the data so each entry is captured.
            using (StreamWriter logFile = new StreamWriter(File.Open(logFilePath, FileMode.Append)))
            {
                logFile.Write(logFileUpdate);
            }

            //Add the contents to the debug file. Appending the data for records.
            using (StreamWriter debugFile = new StreamWriter(File.Open(debugLog, FileMode.Append)))
            {
                debugFile.Write(debugFileUpdate);
            }
        }

        //Method for the debug log.
        private void writeToDebug()
        {
            //Getting the items to look at in the debug log.
            string debugInfo = "Body Fat Calulator Debug Information";
            string bodyFatWeight = myCalculate.BodyFatWeight.ToString();
            string isFemale = myCalculate.IsFemale.ToString();
            string isError = myCalculate.IsError.ToString();
            writeToLog(debugInfo, bodyFatWeight, isFemale, isError);
        }
    }
}

Calculate Class:


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

namespace BodyFatCalculator
{
    public class Calculate
    {
        private bool _enableDebugging = false;
        private bool _isFemale = true;
        private bool _isError = false;
        private double _leanBodyMass = 0;
        private double _bodyFatWeight = 0;
        private double _bodyWeight = 0;
        private double _weightFactor = 0;
        private double _wristFactor = 0;
        private double _waistFactor = 0;
        private double _hipsFactor = 0;
        private double _forearmFactor = 0;
        private double _bodyFatPercentage = 0;

        public bool EnableDebugging
        {
            get { return _enableDebugging; }
            set { _enableDebugging = value; }
        }

        public bool IsFemale
        {
            get { return _isFemale; }
            set { _isFemale = value; }
        }

        public bool IsError
        {
            get { return _isError; }
            set { _isError = value; }
        }

        public double LeanBodyMass
        {
            get { return _leanBodyMass; }
            set { _leanBodyMass = value; }
        }

        public double BodyFatWeight
        {
            get { return _bodyFatWeight; }
            set { _bodyFatWeight = value; }
        }

        public double BodyWeight
        {
            get { return _bodyWeight; }
            set { _bodyWeight = value; }
        }

        public double WeightFactor
        {
            get { return _weightFactor; }
            set { _weightFactor = value; }
        }

        public double WristFactor
        {
            get { return _wristFactor; }
            set { _wristFactor = value; }
        }

        public double WaistFactor
        {
            get { return _waistFactor; }
            set { _waistFactor = value; }
        }

        public double HipsFactor
        {
            get { return _hipsFactor; }
            set { _hipsFactor = value; }
        }

        public double ForearmFactor
        {
            get { return _forearmFactor; }
            set { _forearmFactor = value; }
        }

        public double BodyFatPercentage
        {
            get { return _bodyFatPercentage; }
            set { _bodyFatPercentage = value; }
        }

        public double calculateBodyFat(double weightFactor, double wristFactor, double waistFactor,
            double hipsFactor, double forearmFactor)
        {
            if (IsFemale)
            {
                WeightFactor = weightFactor * 0.732 + 8.987;
                WristFactor = wristFactor /= 3.140;
                WaistFactor = waistFactor *= 0.157;
                HipsFactor = hipsFactor *= 0.249;
                ForearmFactor = forearmFactor *= 0.434;
                LeanBodyMass = WeightFactor + WristFactor - WaistFactor - HipsFactor + ForearmFactor;
                BodyFatWeight = weightFactor - LeanBodyMass;
            }
            else
            {
                WeightFactor = weightFactor * 1.082 + 94.42;
                WaistFactor = waistFactor *= 4.15;
                LeanBodyMass = WeightFactor - WaistFactor;
                BodyFatWeight = weightFactor - LeanBodyMass;
            }
            return BodyFatWeight;
        }
    }
}

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s