Isafe
Isafe

Reputation: 13

How to write an array data base on polymorphism subclass detail in c#

I have working on payroll programming with two type of employee.First is normal employee and salary = base salary + OT wage.Second is project employee,salary = worked hours * 3000. I have declare a subclass like this

class Employee
{
    protected string empName;
    protected double pay;
    protected int hour;

    public Employee (string e,int h,double p)
    {
        empName = e; 
        hour = h;
        pay = p;
    }

    public string EmpName
    {
        get { return empName; }
    }

    public int Hour
    {
        get { return hour; }
    }
    public int addHour (int addhour)
    {
        hour += addhour;
        return hour;
    }

    public virtual double Pay(int hour)
    {
        return 0;
    }
}
class Salary : Employee
{
   private double salary;

   public Salary (string e,int h,double p,double s) : base (e,h,p)
    {
        salary = s;
    }

    override public double Pay(int hour)
    {
        double OTWage;
        if (hour <= salary * 0.3)
        {
            OTWage = hour * 120;
        }
        else OTWage = salary * 0.3;

        return salary + wage;
    }
    public double paY
    {
        get { return pay ; }
    }
}
class Project : Employee
{
    private string projName;

    public Project (string e,int h,double p,string proj) : base(e,h,p)
    {
        projName = proj;
    }
    public string ProjName
    {
        get { return projName; }
    }
    override public double Pay(int hour)
    {
        return hour*3000;
    }
    public double paY
    {
        get { return pay; }
    } 

}

and arrays like this

class Program
{
    static void Main(string[] args)
    {
        Salary[] salary = new Salary[100];
        Project[] project = new Project[100];
        salary[0] = new Salary("Safe", 0,10000, 500000);
        project[0] = new Project("Aom", 3,100, "OOP");
        string pname,ename;
        int i,pindex=1,eindex = 1,cont,hour;
        do
        {
            Console.WriteLine("=================Main Menu===============");
            Console.WriteLine("1.Add hours for project employee");
              Console.WriteLine("2.Add overtime hour for salary employee");
            Console.WriteLine("3.Shows total pay for all employees");
            Console.WriteLine("=========================================");
            int choice = Convert.ToInt32(Console.ReadLine());
            if (choice == 1)
            {
                Console.WriteLine("What project do you want to add hour?");
                pname = Convert.ToString(Console.ReadLine());
                for (i = 0; (project[i].ProjName != pname) && i < pindex; i++) ; ;
                if (i < pindex)
                {
                    Console.WriteLine("How much hours do you want to add? ");
                    hour = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Total employee's times for this project is " + project[i].addHour(hour));
                }

            }
            if (choice == 2)
            {
                Console.WriteLine("Who do you want to add overtime hour? ");
                ename = Convert.ToString(Console.ReadLine());
                for (i = 0; (salary[i].EmpName != ename) && i < eindex; i++) ; ;
                if (i < eindex)
                {
                    Console.WriteLine("How much hours do you want to add? ");
                    hour = Convert.ToInt32(Console.ReadLine());
                    Console.WriteLine("Total employee's overtimes is" + salary[i].addHour(hour));
                }
            }
            if (choice == 3)
            {
                Console.WriteLine("=====Payroll Summary=====");  
                Console.WriteLine("You need to pay " + (Project.paY) + " Baht for " + project[0].EmpName);
                Console.WriteLine("You need to pay " + ((Salary)salary[0].Pay(Hour) + " Baht for " + salary[0].EmpName); //something wrong on this part

            }

            Console.WriteLine("Do you want to continue?(yes=1,no=2)");
            cont = Convert.ToInt32(Console.ReadLine());
        }
        while (cont == 1);
        Console.WriteLine("End");


    }
}

}

I can't show a payroll summary, what I need to do? Or something wrong with my program?

Upvotes: 0

Views: 77

Answers (1)

PaulF
PaulF

Reputation: 6773

Does this fix your problem :

        if (choice == 3)
        {
            Console.WriteLine("=====Payroll Summary=====");  
            Console.WriteLine("You need to pay " + (project[0].paY) + " Baht for " + project[0].EmpName);
            Console.WriteLine("You need to pay " + (salary[0].Pay(salary[0].Hour) + " Baht for " + salary[0].EmpName);
        }

if there is more than one of each Salary & Project then you need to loop through each array

Upvotes: 1

Related Questions