Thursday, November 6, 2014

Square Footage Class Examples (Morming)

Here is the SquareFootage class

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

namespace ClassExampleMorning
{
    class SquareFootage
    {
        /************************
         * this class calculates square footage and
         * the total cost of that footage
         * *****************************/


        //fields--class level variables that describe the object
        private double length;
        private double width;
        private double pricePerSquareFoot;

        //this is a default constructor
        //constructors initialize a class
        //setting default values for variables
        //and maybe calling a method
        public SquareFootage()
        {
            Length = 0;
            Width = 1;
            PricePerSquareFoot = 0;
        }

        //this is an overloaded constructor.
        //You can only initialize a class one way
        //but you can set up choices for how
        //to initialize the class
        //In this case you can pass the width and length
        //directly to the constructor
        public SquareFootage(double width, double length)
        {
            Width = width;
            Length = length;
            PricePerSquareFoot = 0;
        }

        //public properties
        //properties expose the private variables
        //to the world
        public double PricePerSquareFoot
        {
            //return lets another class see the value of the field
            get { return pricePerSquareFoot; }
            //set lets another class change the value of the field
            set { pricePerSquareFoot = value; }
        }


        public double Width
        {
            //you can do some validation in a property
            get { return width; }
            set { 
                if (value >0)
                { 
                width = value;
                }
                else
                {
                    //an exception is an error object
                    //here we make our own error with a message
                    Exception ex = new Exception("Must be greater than zero");
                    //we don't have a way to display it here
                    //so we throw it back to the class
                    //where it was called
                    throw ex;
                }
            }
        }
       

        //properties
        public double Length
        {
            get { return length; }
            set { length = value; }
        }

        //public methods
        //these are just methods 
        //just like any other method
        public double CalculateSquareFootage()
        {
            return Width * Length;
        }

        public double CalculateTotalCost()
        {
            return CalculateSquareFootage() * PricePerSquareFoot;
        }
    }
}


Here is the Display class

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

namespace ClassExampleMorning
{
    class Display
    {
        //Square footage is a class level field
        private SquareFootage sf;

        //the constructor calls the methods
        //for getting inputs and showing outputs
        public Display()
        {
            //I commented this to use the 
            //overloaded constructor in SquareFootage
            //sf = new SquareFootage();
            GetInputs();
            ShowOutputs();
        }

        private void GetInputs()
        {
            //I declare these two variables to
            //store the input, then I pass them
            //to SquareFootage through its 2nd constructor
            double w, l; 
            //the try "tries" all the code. If there is an error
            //it falls to the catch
            try
            {
                Console.WriteLine("Enter the Width");
                //sf.Width = double.Parse(Console.ReadLine());
                w = double.Parse(Console.ReadLine());
                Console.WriteLine("Enter the Length");
                //sf.Length = double.Parse(Console.ReadLine());
                l = double.Parse(Console.ReadLine());
                //initialize SquareFootage and pass it width and length
                sf = new SquareFootage(w, l);

                //but we use the property to assign
                //the value to PricePerSquareFoot
                Console.WriteLine("Enter the Price per square foot");
                sf.PricePerSquareFoot = double.Parse(Console.ReadLine());
            }
            catch(Exception ex)
            {
                //display the error message
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }

        private void ShowOutputs()
        {
            //call the methods from squareFootage
            //and display the results
            Console.WriteLine("the Square footage is " +
                sf.CalculateSquareFootage());
            Console.WriteLine("the total cost is " +
                sf.CalculateTotalCost());
            Console.ReadKey();
        }

    }
}


Here is the Program class


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

namespace ClassExampleMorning
{
    class Program
    {
        static void Main(string[] args)
        {
            //this initilizes the Display class
            //and class the constructor
            //that calls the getInputs method
            Display d = new Display();
            
        }
    }


}

No comments:

Post a Comment