Thursday, October 11, 2012

More if and else if

This is the code that is part of assignment 3

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

namespace Medicare
{
    class Program
    {
        static void Main(string[] args)
        {
            //load the program 
            Program p = new Program();
            //call the display method
            p.Display();
            //pause it
            Console.ReadKey();
        }

        private void Display()
        {
            //get the inputs
            Console.WriteLine("Enter Your pay rate");
            double rate = double.Parse(Console.ReadLine());
            Console.WriteLine("Enter your hours");
            double hours = double.Parse(Console.ReadLine());

            //call the CalculateGross method and store the value
            //it returns in the local variable gross
            double gross = CalculateGrossPay(rate, hours);
            Console.WriteLine("Your gross pay is {0:C}", gross);
            //call the method to CalculateMedicare
            double med = CalculateMedicare(gross);
            Console.WriteLine("Your Medicare Deduction is {0:C}", med);
        }

        private double CalculateGrossPay(double rate, double hours)
        {
            //this method calcuates pay 
            //it takes overtime into account
            double grossPay=0;
            if (hours > 40)
            {
                grossPay = rate * (40 + ((hours - 40) * 1.5));
            }
            else
            {
                grossPay = rate * hours;
            }

            return grossPay;
        }//end calculateGrosspay

        private double CalculateMedicare(double gross)
        {
            double medicare = 0;
            //5000 > .2
            //2000 to 4999 .1
            //1999 or less .03
            //uses and if, else if to calculate percentages
            if (gross >= 5000)
            {
                medicare = gross * .2;
            }
            else if (gross >= 2000)
            {
                medicare = gross * .1;
            }
            else
            {
                medicare = gross * .03;
            }

            return medicare;
        }
    }
}


Here are the other if, else if examples

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

namespace Medicare
{
    class Program
    {
        static void Main(string[] args)
        {
            //load the program 
            Program p = new Program();
            //call the display method
            p.Display();
            //pause it
            Console.ReadKey();
        }

        private void Display()
        {
            //get the inputs
            Console.WriteLine("Enter Your pay rate");
            double rate = double.Parse(Console.ReadLine());
            Console.WriteLine("Enter your hours");
            double hours = double.Parse(Console.ReadLine());

            //call the CalculateGross method and store the value
            //it returns in the local variable gross
            double gross = CalculateGrossPay(rate, hours);
            Console.WriteLine("Your gross pay is {0:C}", gross);
            //call the method to CalculateMedicare
            double med = CalculateMedicare(gross);
            Console.WriteLine("Your Medicare Deduction is {0:C}", med);
        }

        private double CalculateGrossPay(double rate, double hours)
        {
            //this method calcuates pay 
            //it takes overtime into account
            double grossPay=0;
            if (hours > 40)
            {
                grossPay = rate * (40 + ((hours - 40) * 1.5));
            }
            else
            {
                grossPay = rate * hours;
            }

            return grossPay;
        }//end calculateGrosspay

        private double CalculateMedicare(double gross)
        {
            double medicare = 0;
            //5000 > .2
            //2000 to 4999 .1
            //1999 or less .03
            //uses and if, else if to calculate percentages
            if (gross >= 5000)
            {
                medicare = gross * .2;
            }
            else if (gross >= 2000)
            {
                medicare = gross * .1;
            }
            else
            {
                medicare = gross * .03;
            }

            return medicare;
        }
    }
}

No comments:

Post a Comment