Tuesday, October 29, 2013

Morning Class Example 2

View the Evening class post to see commented code

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

namespace ClassicExamples
{
    class Mileage
    {
        //private fields
        private int beginningMileage;
        private int endingMileage;
        private double pricePerGallon;
        private double gallons;

        public Mileage()
        {
            BeginningMileage = 0;
            EndingMileage = 0;
            Gallons = 0;
            PricePerGallon = 0;
        }

        public Mileage(int begin, int end, double gals)
        {
            BeginningMileage = begin;
            EndingMileage = end;
            Gallons = gals;
            PricePerGallon = 0;
        }

        //public properties
        public int EndingMileage
        {
            get { return endingMileage; }
            set {
                if (value > 0)
                    endingMileage = value;
                else
                {
                    endingMileage = -1;
                }
            }
        }
       

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

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

      
        public int BeginningMileage
        {
            get { return beginningMileage; }
            set { beginningMileage = value; }
        }
       // public int EndingMileage { get; set; }

        //methods
        private int CalculateTotalMiles()
        {
            return EndingMileage - BeginningMileage;
        }

        public double CalculateMilesPerGallon()
        {
            int totalMiles = CalculateTotalMiles();
            double mPG = totalMiles / Gallons;
            return mPG;
        }

        public double CalculateMilesPerGallon(int begin, int end, double gals)
        {
            BeginningMileage = begin;
            EndingMileage = end;
            Gallons = gals;
            int totalMiles = CalculateTotalMiles();
            double mPG = totalMiles / Gallons;
            return mPG;

        }

        public void CalculateCostPerMile()
        {
            throw new System.NotImplementedException();
        }

        public override string ToString()
        {
            return "Your miles per gallon is " + CalculateMilesPerGallon().ToString();
        }
    }
}

No comments:

Post a Comment