Here is the Mileage class
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MileageForm { ////// this is a very simple mileage class /// it has three properties /// Distance, Gallons and PricePerGallon /// I used a short cut method to create /// the properties just expressing the get and set /// It has two methods one to calculate /// the gas mileage and one to calculate the /// price per mile /// class Mileage { //short cut for properties public double Distance {get; set;} public double Gallons { get; set; } public double PricePerGallon { get; set; } //methods public double CalculateMileage() { return Distance / Gallons; } public double CalculatePricePerMile() { return PricePerGallon * Gallons /Distance; } } }
Here is the XAML for the form
<Window x:Class="MileageForm.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" Name="Form1"> <Grid x:Name="labelCost"> <Label Content="Enter the miles traveled" HorizontalAlignment="Left" Margin="46,7,0,0" VerticalAlignment="Top"/> <TextBox x:Name="textMiles" HorizontalAlignment="Left" Height="23" Margin="227,11,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> <Label Content="Enter the total gallons" HorizontalAlignment="Left" Margin="46,55,0,0" VerticalAlignment="Top"/> <TextBox x:Name="textGallons" HorizontalAlignment="Left" Height="23" Margin="227,55,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> <Label Content="Enter the price per Gallon" HorizontalAlignment="Left" Margin="46,98,0,0" VerticalAlignment="Top"/> <TextBox x:Name="textPrice" HorizontalAlignment="Left" Height="23" Margin="227,98,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> <Button x:Name="ButtonCalculate" Content="Calculate" HorizontalAlignment="Left" Margin="56,142,0,0" VerticalAlignment="Top" Width="75" Click="ButtonCalculate_Click"/> <Label Content="Your Mileage is" HorizontalAlignment="Left" Margin="46,181,0,0" VerticalAlignment="Top"/> <Label x:Name="labelMileage" Content="Label" HorizontalAlignment="Left" Margin="227,181,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.474,-0.385"/> <Label Content="The cost per mile is" HorizontalAlignment="Left" Margin="46,212,0,0" VerticalAlignment="Top"/> <Label x:Name="labelCost1" Content="Label" HorizontalAlignment="Left" Margin="227,220,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.026,0.385"/> <Button x:Name="buttonClear" Content="Clear" HorizontalAlignment="Left" Margin="56,259,0,0" VerticalAlignment="Top" Width="75" Click="buttonClear_Click"/> <Button x:Name="buttonExit" Content="Exit" HorizontalAlignment="Left" Margin="156,259,0,0" VerticalAlignment="Top" Width="75" Click="buttonExit_Click"/> </Grid> </Window>
Here is the code behind the form
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace MileageForm { ////// Interaction logic for MainWindow.xaml /// this is the code behind the windows form /// public partial class MainWindow : Window { public MainWindow() { //initilize the form--don't remove thos InitializeComponent(); } private void ButtonCalculate_Click(object sender, RoutedEventArgs e) { //call the method to make sure the text boxes have content //they return true if there is a value missing bool good =CheckTextBoxes(); //if there is a value missing stop the action //here if (good) { return; } //delcare a new instance of Mileage class Mileage m = new Mileage(); //get inputs m.Distance = double.Parse(textMiles.Text); m.Gallons = double.Parse(textGallons.Text); m.PricePerGallon = double.Parse(textPrice.Text); //outputs labelMileage.Content = m.CalculateMileage().ToString(); EvaluateMileage(m.CalculateMileage()); labelCost1.Content= m.CalculatePricePerMile().ToString("C"); } private void buttonClear_Click(object sender, RoutedEventArgs e) { //this method clears the textboxes and labels textMiles.Clear(); textGallons.Clear(); textPrice.Clear(); labelCost1.Content=""; labelMileage.Content = ""; //resets the color to white Form1.Background = new SolidColorBrush(Colors.White); textMiles.Focus(); } private void buttonExit_Click(object sender, RoutedEventArgs e) { //close the form this.Close(); } private void EvaluateMileage(double mileage) { //this checks the mileage and returns a //background color based on the value if (mileage > 35) { Form1.Background = new SolidColorBrush(Colors.Green); } else if (mileage > 25) { Form1.Background = new SolidColorBrush(Colors.Yellow); } else { Form1.Background = new SolidColorBrush(Colors.Red); } } private bool CheckTextBoxes() { //this method checks to make sure the form //textboxes have values if (textMiles.Text=="") { MessageBox.Show("Enter a valid mileage"); return true; } if(textGallons.Text=="") { MessageBox.Show("Enter a valid Gallon amount"); return true; } if (textPrice.Text == "") { MessageBox.Show("Enter a valid Price per Gallon"); return true; } return false; } } }
Here is a picture of the form in design