Friday, October 7

Age Calculator using User define Methods


Another example of Console application using c sharp language. lets find out how this program works. i have made different classes like validateMonth, validateDay  which does not need to be made. it works fine if you do not make these classes. you want to calculate your age(Years, Months, Days) you only need to enter date of birth and subtract date of birth from today's Date. you will be shown output very accurate but this application for those who will not commit a mistake while entering the data of birth. to make it generic it requires some validations that will not allow the user to enter incorrect date of birth.



First of all user is allowed to enter Year which is validated with today's date. if user enters 3000 or something which shows that we are predicting one's Date of birth. it will not be allowed.
I have made Public method in class validateMonth which returns int value. while entering month this iteration will stick to the user until and unless he/she enters the correct month.
After entering Year and month user is then provided to enter day. if he enters 2000 in year field you can see that 2000 is a leap year so in this year February will have 29 days. so user can choose month 2 and day 29. It will not give an error. in DayValidation i have made an array of int type. which is fed with days of different months. this method is taking three arguments and returning an integer value.
after entering these values(year, month, day) .these three values are fed into datetime constructor as an argument. date of birth is subtracted from today's date and it is then calculated in the timespan object. as we now timespan gives the total duration. and by using timespan.day property you can separate years, months and days. and then you can display them as per your requirement.
/////////////////////////////////////////////////////////////////////////// Begins /////////////////////////////////////////////////////////////////////////////

namespace Dob_umair_Q_1_
{
    class validateMonth
    {
        public int validateMonths(int m)
        {
            if (m > 0 && m <= 12)
            {
                return m;
            }
            else
            {
                return 0;
            }
        }
    }


    class validateDay {
       public int validateDays(int m, int d,int y)
        {
            int flag = 0;
           int[] monthCheck = new int[13] {0 , 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
           if (d == 29 && m == 2)
           {
               if (y % 4 == 0)
               {
                   flag = 1;
               }
           }
        
           else
           {
               if (d <= monthCheck[m])
               {
                   flag = 1;
               }
               else
               {
                   flag = 0;
               }
           }
           if (flag == 1)
           {
               return d;
           }
           else
           {
               return 0;
           }
        }
    }
    class Program
    {
    
        static void Main()
        {
            DateTime today = DateTime.Now;
            int y, m, d, tdays;
            validateDay obj = new validateDay();
            validateMonth ob = new validateMonth();
            int ny = today.Year;
            do
            {
                Console.Write("Enter Year: ");
                y = int.Parse(Console.ReadLine());
            }
            while (y > ny);
            do{
                Console.Write("Enter Month: ");
                 m = int.Parse(Console.ReadLine());
            }
            while(ob.validateMonths(m) == 0);
            do
            {
                Console.Write("Enter Day: ");
                d = int.Parse( Console.ReadLine() );
            }
            while (obj.validateDays(m, d, y) == 0);
            DateTime dob = new DateTime(y, m, d);
            TimeSpan dobT = today - dob;
            tdays = int.Parse(dobT.Days.ToString());
            y = tdays / 365;
            m = tdays % 365;
            m = m / 30;
            d = m % 30;
            Console.Clear();
            Console.WriteLine("Your DOB: "+dob.ToString("MMM-dd-yyyy"));
            Console.Write("You are {0} Year(s) {1} Month(s) and {2} Day(s) old",y,m,d);
            Console.ReadLine();
        }
    }
}

/////////////////////////////////////////////////////////////////////////// Ends ///////////////////////////////////////////////////////////////////////////////

No comments:

Post a Comment