C#

Most recent work in C#.

Return to home page

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

namespace TripCalculator

{
    /* Trip Calculator: calculates gas mileage for a trip using a form.
    (Problem #5)
    Sam Kuney
    11/1/2017
    */
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void calculateButton_Click(object sender, EventArgs e)
        {
            string inValue, inValue2; //string entered into text boxes
            double milesAmt, gallonsAmt, ans; //will be parsed to these, last variable will be the MPG
            inValue = mileTraveledBox.Text;
            while(double.TryParse(mileTraveledBox.Text, out milesAmt) == false) //if a non-numerical value is entered for miles traveled, it will be rejected.
            {
                MessageBox.Show("Value entered must be a numerical value!");
                mileTraveledBox.Text = "0.0";
                mileTraveledBox.Focus();
            }

            inValue2 = gallonsOfGasBox.Text;
            while (double.TryParse(gallonsOfGasBox.Text, out gallonsAmt) == false) //if a non-numerical value is entered for gallons, it will be rejected.
            {
                MessageBox.Show("Value entered must be a numerical value!");
                gallonsOfGasBox.Text = "0.0";
                gallonsOfGasBox.Focus();
            }

            ans = (milesAmt / gallonsAmt); //miles divided by gallon for mileage.
            MessageBox.Show("Milage from current location to " + destinationEntry.Text + ": " + ans + " miles per gallon", "Calculating trip mileage..."); //displays message box showing destination & gas mileage

            if(ans < 20) //displays a warning if gas mileage is below 20 mpg.
            {
                MessageBox.Show("Your vehicle is getting below average gas mileage. Seek an inspection immediately.", "WARNING!!!!!", MessageBoxButtons.OK , MessageBoxIcon.Warning);
            }

            if(ans >= 20) //lets user know they are getting good gas mileage if it is at or above 20 mpg.
            {
                MessageBox.Show("Good news! You are getting great mileage.", "Super!", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void resetButton_Click(object sender, EventArgs e) //resets all text boxes in the form.
        {
            destinationEntry.Clear();
            gallonsOfGasBox.Clear();
            mileTraveledBox.Clear();
        }
    }
}