Monday, January 14, 2013

MVC Example With C# Console Program.

This example consists of four classes.
* Tip--the model, where the calculations are done
* Display--The class that gets inputs and shows outputs
* TipCalculatorController--the class that brings the Model and view together
* Program which calls the controller class in the Main() method

The Display class is totally unaware of the Tip or the Controller class. The Tip class is totally unaware of the Display or the controller. Only the controller class is aware of the other two. The point is to create as loose a coupling of the classes as possible. The more independent they are of each other, the easier to update and maintain.

The code is commented

Tip.cs


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

namespace MVCSample
{
    class Tip
    {
        /// 
        /// This class does a very simple tip 
        /// calculation. It has two fields amount and
        /// tip percent. We are ignoring tax and whether
        /// we tip before or after tax. The point is the
        /// MVC model. This is the model part of MVC.
        /// It does the calculations and handles the data
        /// it is totally unaware of the Display class
        /// or the controller
        /// 
        /// 
        //private fields
        private double amount;
        private double tipPercent;

        //default constructor
        public Tip()
        {
            Amount = 0;
            TipPercent = 0;
        }

        //overloaded constructor
        public Tip(double amt, double percent)
        {
            Amount = amt;
            TipPercent = percent;
        }

        //public properties
        public double Amount
        {
            get { return amount; }
            set { amount = value; }
        }
       
        public double TipPercent
        {
            get { return tipPercent; }
            set 
            {
                //here we check to see if 
                //they entered the percent
                //as a decimal or a whole number
                //if it is a whole number
                //larger than 1 we divide it by
                //100, so the highest possible tip
                //is 100%
                if (value > 1)
                {
                    value /= 100;
                }
                tipPercent = value; 
            }
        }

        public double CalculateTip()
        {
            //very simplistic tip calculation
            return Amount * TipPercent;
        }

        public double CalculateTotal()
        {
            //simple total calculation
            return CalculateTip() + Amount;
        }

    }
}


Display.cs


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

namespace MVCSample
{
    class Display
    {
        /// 
        /// this is the Display class. Its purpose is
        /// to gather the input and display the output
        /// Unlike our usual display I have made the 
        /// variables Class level fields. This is so
        /// the Controller can have access to the fields
        /// through the properties. The Display class
        /// is totally unaware of the Tip class (the model)
        /// or the controller
        /// 
        /// 
        //private fields
        private double perc;
        private double amt;
        private double total;
        private double tipAmount;

        //constructor
        public Display()
        {
            Percentage= 0;
            TipAmount = 0;
            Amt = 0;
            Total = 0;
            GetValues();
        }

        //public properties
        public double TipAmount
        {
            get { return tipAmount; }
            set { tipAmount = value; }
        }


        public double Total
        {
            get { return total; }
            set { total = value; }
        }
       

        public double Percentage
        {
            get { return perc; }
            set { perc = value; }
        }
       
        public double Amt
        {
            get { return amt; }
            set { amt = value; }
        }

        //private method for getting input
        //it is called in the constructor
        private void GetValues()
        {
            Console.WriteLine("Enter the Amount of the meal");
            Amt=double.Parse(Console.ReadLine());

            Console.WriteLine("Enter the percent you want to tip");
            Percentage = double.Parse(Console.ReadLine());
        }

        //public method to show output
        //public so I can access it from the controller
        public void ShowTipandTotal()
        {
            Console.WriteLine("Your tip is {0:C}", TipAmount);
            Console.WriteLine("The total will be {0:C}", Total);
            Console.ReadKey();
        }



    }
}


TipCalculatorController.cs


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

namespace MVCSample
{
    class TipCalculatorController
    {
        /// 
        /// The TipCalculatorController class brings together 
        /// the display and the tip or model classes
        /// I use the constructor to instantiate the Display.
        /// Instantiating the Display calls its constructor
        /// which calls the Get input method
        /// Once the input is entered I can instantiate
        /// the Tip class and pass the values from the 
        /// Display class. Notice the dot notation and observe
        /// how the two classes interact
        /// 
        private Tip tip;
        private Display display;

        public TipCalculatorController()
        {
            display = new Display();
            tip = new Tip(display.Amt, display.Percentage);
            display.TipAmount = tip.CalculateTip();
            display.Total = tip.CalculateTotal();
            display.ShowTipandTotal();
        }
    }
}


Program.cs


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

namespace MVCSample
{
    class Program
    {
        static void Main(string[] args)
        {
            TipCalculatorController t= new TipCalculatorController();
        }
    }
}


Here is a picture of the program running

5 comments: