Monday, November 7, 2011

More Classes

Here are the classes we did for the evening session.

Program.cs


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

namespace MultiClassExample
{
    class Program
    {
        static void Main(string[] args)
        {
            Display dis = new Display();
            //Mileage m = new Mileage();


            //m.BeginningMiles = 1000;
            //m.EndingMiles = 1450;
            //m.Gallons = 25;
            //m.PricePerGallon = 3.71;

            //Console.WriteLine("Your miles per gallon are {0:F2}", m.MilesPerGallon());
            //Console.WriteLine("The cost per Mile is {0:C}", m.CostPerMile());

            //Mileage m3 = new Mileage(2000, 2400);
            //m3.Gallons = 20;
            //Console.WriteLine("The MPG is {0}", m3.MilesPerGallon());


            //Mileage m2 = new Mileage(12000, 12500, 25);
            //Console.WriteLine("The MPG is {0}", m2.MilesPerGallon());

            //Console.ReadKey();
        }
    }

    //I could write a new class right here
}

Display.cs


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

namespace MultiClassExample
{
    class Display
    {
        Mileage m; 

        public Display()
        {
            m = new Mileage();
            GetInputs();
            ShowOutputs();
            PauseIt();
        }

        private void GetInputs()
        {
            Console.WriteLine("This program calculates MPG");

            Console.WriteLine("Enter the Beginning Miles");
            m.BeginningMiles = double.Parse(Console.ReadLine());

            Console.WriteLine("Enter the Ending Miles");
            m.EndingMiles = double.Parse(Console.ReadLine());

            Console.WriteLine("Enter the Gallons");
            m.Gallons = double.Parse(Console.ReadLine());

        }

        private void ShowOutputs()
        {
            Console.WriteLine("the Miles perGallon is {0}", m.MilesPerGallon());
        }

        private void PauseIt()
        {
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }//end PauseIt

    }
}

Mileage.cs


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

namespace MultiClassExample
{
    public class Mileage
    {
        //fields
        double beginningMiles;
        double endingMiles;
        double pricePerGallon;
        double gallons;

        public Mileage()
        {
            beginningMiles=0;
             endingMiles=0;
            pricePerGallon=0;
            gallons=0;
        }

        public Mileage(double begMiles, double endMiles)
        {
            beginningMiles = begMiles;
            endingMiles = endMiles;
            pricePerGallon = 0;
            gallons = 0;
        }

        public Mileage(double begMiles, double endMiles, double gals)
        {
            beginningMiles = begMiles;
            endingMiles = endMiles;
            pricePerGallon = 0;
            gallons = gals;
        }
     
        #region Properties

        public double BeginningMiles
        {
            get { return beginningMiles; }
            set { beginningMiles = value; }
        }
        

        public double EndingMiles
        {
            get { return endingMiles; }
            set { endingMiles = value; }
        }
        

        public double PricePerGallon
        {
            get { return pricePerGallon; }
            set { pricePerGallon = value; }
        }
      

        public double Gallons
        {
            get { return gallons; }
            set { gallons = value; }
        }
        #endregion

        //methods
        private double GetTotalMileage()
        {
            return EndingMiles - BeginningMiles;
        }

        public double MilesPerGallon()
        {
            return GetTotalMileage() / Gallons;
        }

        public double CostPerMile()
        {
            return PricePerGallon / MilesPerGallon();
        }
            
    }
}

No comments:

Post a Comment