.Here is the windows form XML
<Window x:Class="MileageApplication.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="MPGWindow"> <Grid> <Label Content="Enter Beginning Mileage" HorizontalAlignment="Left" Margin="40,30,0,0" VerticalAlignment="Top"/> <TextBox x:Name="txtBeginMiles" HorizontalAlignment="Left" Height="23" Margin="208,30,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> <Label Content="Enter Ending Mileage" HorizontalAlignment="Left" Margin="40,74,0,0" VerticalAlignment="Top"/> <TextBox x:Name="txtEndingMiles" HorizontalAlignment="Left" Height="23" Margin="208,74,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> <Label Content="Enter Gallons" HorizontalAlignment="Left" Margin="50,116,0,0" VerticalAlignment="Top"/> <TextBox x:Name="txtGallons" HorizontalAlignment="Left" Height="23" Margin="208,119,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/> <Button x:Name="btnCalculate" Content="Calculate" HorizontalAlignment="Left" Margin="50,157,0,0" VerticalAlignment="Top" Width="75" Click="btnCalculate_Click"/> <Label x:Name="lblResult" Content="Label" HorizontalAlignment="Left" Margin="208,177,0,0" VerticalAlignment="Top" Width="131" Height="56"/> <Button x:Name="btnExit" Content="Exit" HorizontalAlignment="Left" Margin="50,213,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.507,0.1" Click="btnExit_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 MileageApplication { ////// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); MPGWindow.Background = new LinearGradientBrush(Colors.BlueViolet, Colors.AliceBlue, 45); } private void btnCalculate_Click(object sender, RoutedEventArgs e) { int bMiles; Mileage m = new Mileage(); bool isGoodBegMiles = int.TryParse(txtBeginMiles.Text, out bMiles); if (isGoodBegMiles) { m.BeginMileage = bMiles; } else { MessageBox.Show("Enter a valid integer for beginning Miles"); return; } m.EndMileage = int.Parse(txtEndingMiles.Text); m.Gallons = double.Parse(txtGallons.Text); double mpg = m.CalculateGasMileage(); lblResult.Content = mpg.ToString(); if (mpg > 40) lblResult.Background = new SolidColorBrush(Colors.Green); else if (mpg > 30) lblResult.Background = new SolidColorBrush(Colors.GreenYellow); else if (mpg > 20) lblResult.Background = new SolidColorBrush(Colors.Yellow); else lblResult.Background = new SolidColorBrush(Colors.Red); } private void btnExit_Click(object sender, RoutedEventArgs e) { Application.Current.Shutdown(); } } }
Here is the Mileage Class, the same as we had before with one change: the Namespace was changed to match the rest of the application.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MileageApplication { class Mileage { //class variables or fields //fields should be private private int beginMileage; private int endMileage; private double pricePerGallon; private double gallons; //constructors are ways of constructing //the class, they set the initial values //a constructor always has the same name //as the the class and no return type //A class can have several "overloaded" constructors //only one is used per instance of the class //the user chooses which constructor he or she //wants to use by the signature (what parameters it takes) //this is a default constructor. It has no arguments //and sets all the properties to their minumum value; //If you write no constructor at all this is what you get //if you do write a constructor it is up to you to //provide the initial values public Mileage() { PricePerGallon = 0; BeginMileage = 0; EndMileage = 0; Gallons = 0; } //this is an overloaded constructor that takes three //values. A user could choose to do this rather than //set the values using the properties public Mileage(int begin, int end, double gal) { BeginMileage = begin; EndMileage = end; Gallons = gal; PricePerGallon = 0; } //this constructor takes four values public Mileage(int begin, int end, double gal, double price) { BeginMileage = begin; EndMileage = end; Gallons = gal; PricePerGallon = price; } //public properties are ways //to expose the fields in a //controlled way //you can do some validation //and testing in a property public double PricePerGallon { get { return pricePerGallon; } set { pricePerGallon = value; } } public double Gallons { get { return gallons; } set { gallons = value; } } public int BeginMileage { get { return beginMileage; } set { beginMileage = value; } } public int EndMileage { get { return endMileage; } set { endMileage = value; } } //a class can contain both private (internal) //methods and public methods private int TotalMiles() { return EndMileage - BeginMileage; } //public method accessible from other //classes public double CalculateGasMileage() { int total = TotalMiles(); double milesPerGallon = total / Gallons; return milesPerGallon; } //this is an overloaded method. It has the same //name but a different signature than the previous method //again it gives a user of the class another option //of how to use the class and call the method public double CalculateGasMileage(int bMiles, int eMiles, double gals) { BeginMileage = bMiles; EndMileage = eMiles; Gallons = gals; int total = TotalMiles(); double milesPerGallon = total / Gallons; return milesPerGallon; } //this is "overriding" the ToString method //which every class inherits from Object //the parent of all classes in .NET //the combination of overloading and overwriting is //what constitutes the object oriented principle of //polymorphism--the ability of a class //to behave differently in different environments public override string ToString() { double mpg = CalculateGasMileage(); return "Your gas mileage is" + mpg.ToString("##.##"); } } }
Here is a picture of the application running