Monday, November 8, 2010

Kilometers Conversion

Conversion.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace ConvertToKilometers
{
class Conversion
{
//private fields
private double miles;
private const double CONVERTFACTOR = 1.6;

//default constructor
public Conversion()
{
Miles = 0;
}

//overloaded constructor
public Conversion(double totalMiles)
{
Miles = totalMiles;
}

//public property
public double Miles
{
get { return miles; }
set { miles = value; }
}

//public method

public double Convert()
{
return Miles * CONVERTFACTOR;
}


}
}


Display.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace ConvertToKilometers
{
class Display
{
private double totMiles;

public void GetMiles()
{
bool isNumber;
Console.WriteLine("Enter the Miles");
isNumber = double.TryParse(Console.ReadLine(), out totMiles);
if (isNumber==false )
{
Console.WriteLine("Must be a number");
return;
}
}//end getmiles

public void DisplayKilometers()
{
Conversion c = new Conversion(totMiles);
Console.WriteLine("{0} is equal to {1} Kilometers", totMiles.ToString(), c.Convert().ToString());
}


}
}


Program.cs

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

namespace ConvertToKilometers
{
class Program
{
static void Main(string[] args)
{
Display d = new Display();
d.GetMiles();
d.DisplayKilometers();
Console.ReadKey();
}
}
}

No comments:

Post a Comment