Answer to Question #253030 in C# for Israel

Question #253030

Write a program that calculates the take home pay of an employee. The two employees are salaried and hourly. Allow the user to input employee type. If an employee is salaried, allow the user to input the salary amount. If an employee is hourly, allow the user to input the hourly rate and the number of hours clocked for the week. For hourly employees, overtime is paid for hours over 40 at a rate of 1.5 of the base rate. For all employees’ take-home pay federal tax of 18% is deducted. A retirement contribution of 10% and a social security tax rate of 6% should be deducted. Use appropriate constants. Design an object oriented solution. 



1
Expert's answer
2021-10-18T15:07:10-0400
using System;
using System.Collections.Generic;


namespace App
{
    class Employee
    {
        protected const double FEDERAL_TAX = 0.18;
        protected const double RETIREMENT_CONTRIBUTION = 0.1;
        protected const double SOCIAL_SECURITY_TAX_RATE = 0.06;


        public virtual double calculateTakeHomePay()
        {
            return 0;
        }
    }


    class SalariedEmployee : Employee
    {
        private double salaryAmount;


        public SalariedEmployee(double salaryAmount)
        {
            this.salaryAmount = salaryAmount;
        }


        public override double calculateTakeHomePay()
        {
            return this.salaryAmount - this.salaryAmount * FEDERAL_TAX - this.salaryAmount * RETIREMENT_CONTRIBUTION - this.salaryAmount * SOCIAL_SECURITY_TAX_RATE;
        }
    }


    class HourlyEmployee : Employee
    {
        private double hourlyRate;
        private double hours;


        public HourlyEmployee(double hourlyRate, double hours)
        {
            this.hourlyRate = hourlyRate;
            this.hours = hours;
        }


        public override double calculateTakeHomePay()
        {
            double salary = this.hourlyRate * this.hours;


            if (this.hours > 40)
            {
                salary = this.hourlyRate * 40 + (this.hours - 40) * this.hourlyRate * 1.5;
            }


            return salary - salary * FEDERAL_TAX - salary * RETIREMENT_CONTRIBUTION - salary * SOCIAL_SECURITY_TAX_RATE;
        }
    }




    class Program
    {
        static void Main(string[] args)
        {


            int ch = -1;
            while (ch != 3)
            {
                Employee employee=null;
                Console.WriteLine("1. Salaried employee");
                Console.WriteLine("2. Hourly employee");
                Console.WriteLine("3. Exit");
                Console.Write("Your choice: ");
                int.TryParse(Console.ReadLine(), out ch);
                if (ch == 1)
                {
                    Console.Write("Enter the salary amount: ");
                    double salaryAmount;
                    double.TryParse(Console.ReadLine(), out salaryAmount);
                    employee = new SalariedEmployee(salaryAmount);
                }
                else if (ch == 2)
                {
                    Console.Write("Enter the hourly rate: ");
                    double hourlyRate;
                    double.TryParse(Console.ReadLine(), out hourlyRate);
                    Console.Write("Enter the number of hours clocked for the week: ");
                    double hours;
                    double.TryParse(Console.ReadLine(), out hours);
                    employee = new HourlyEmployee(hourlyRate, hours);
                }
                else if (ch == 3)
                {


                }
                if (employee != null) {
                    Console.WriteLine("\nThe take home pay of an employee: {0:N2}\n", employee.calculateTakeHomePay());
                }
            }


        }


    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS