Answer to Question #188615 in C# for CHANDRASENA REDDY

Question #188615

Extend Employee class and create two derived classes from this class. The derived classes will be ContractEmployee and PermanentEmployee

The contract Employee class will have Perks as an additional property. The PermanentEmployee will have NoOfLeaves and ProvidendFund Properties.


Task 1: Create these two classes by inheriting from the Employee class.


Task 2: Override the GetSalary Method in these two classes. For Contract employee the new salary will be Salary + Perks. For Permanent Employee the new salary will be Salary – Providend Fund.


Task 3: Create a console application to use these classes. Create a Menu driven application to select the Type of employee. Based on the user selection create the object and accept the details from the user. Also display the salary of the Employee.


Task 4: As we only need to create instance of Contract Employee and Permanent Employee classes, Convert the Employee class to Abstract class. Also make GetSalary method Abstract in the Base class.


1
Expert's answer
2021-05-05T23:59:23-0400
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace C_SHARP
{
    class Program
    {


        abstract class Employee
        {
            public double Salary { get; set; }
            public Employee() { }
            public Employee(double s)
            {
                this.Salary = s;
            }
            public abstract double GetSalary();
        }


        class ContractEmployee : Employee
        {
            public double Perks { get; set; }
            public ContractEmployee() { }
            public ContractEmployee(double p, double s)
                : base(s)
            {
                this.Perks = p;
            }
            public override double GetSalary()
            {
                return Salary + Perks;
            }
        }
        class PermanentEmployee : Employee
        {
            public int NoOfLeaves { get; set; }
            public double ProvidendFund { get; set; }
            public PermanentEmployee() { }
            public PermanentEmployee(int noOfLeaves, double providendFund, double salary)
                : base(salary)
            {
                this.NoOfLeaves = noOfLeaves;
                this.ProvidendFund = providendFund;
            }
            public override double GetSalary()
            {
                return Salary - ProvidendFund;
            }
        }






        static void Main(string[] args)
        {


            Employee newEmp = null;
            int choice = 0;
            while (choice != 3)
            {
                Console.WriteLine("1. Add contract employee");
                Console.WriteLine("2. Add permanent employee");
                Console.WriteLine("3. Exit");
                Console.Write("Your choice: ");
                choice = int.Parse(Console.ReadLine());
                switch (choice) {
                    case 1:
                        {
                            newEmp = new ContractEmployee();
                            Console.Write("Enter the salary: ");
                            newEmp.Salary = double.Parse(Console.ReadLine());
                            Console.Write("Enter the perks: ");
                            ((ContractEmployee)newEmp).Perks = double.Parse(Console.ReadLine());
                            Console.WriteLine("Contract employee");
                            Console.WriteLine("Salary: {0}", newEmp.Salary.ToString("C"));
                            Console.WriteLine("Perks: {0}", ((ContractEmployee)newEmp).Perks.ToString("C"));
                            Console.WriteLine("Salary: {0}", newEmp.GetSalary().ToString("C"));
                        }
                        break;
                    case 2:
                        {
                            newEmp = new PermanentEmployee();
                            Console.Write("Enter salary: ");
                            newEmp.Salary = double.Parse(Console.ReadLine());
                            Console.Write("Enter the number of leaves: ");
                            ((PermanentEmployee)newEmp).NoOfLeaves = int.Parse(Console.ReadLine());
                            Console.Write("Enter the providend fund: ");
                            ((PermanentEmployee)newEmp).ProvidendFund = double.Parse(Console.ReadLine());
                            Console.WriteLine("Permanent employee");
                            Console.WriteLine("Salary: {0}", newEmp.Salary.ToString("C"));
                            Console.WriteLine("No of leaves: {0}", ((PermanentEmployee)newEmp).NoOfLeaves);
                            Console.WriteLine("Providend fund: {0}", ((PermanentEmployee)newEmp).ProvidendFund.ToString("C"));
                            Console.WriteLine("Salary: {0}", newEmp.GetSalary().ToString("C"));
                        }
                        break;
                    case 3:
                        break;
                    default:
                        Console.WriteLine("Wrong menu item. Try again.");
                        break;
                }
            }


        }
    }
}

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
APPROVED BY CLIENTS