static void Main(string[] args)
        {
            List <Employee> myEmployees = new List <Employee>();

            HourlyEmployee hourEmp = new HourlyEmployee("Luke", "Skywalker", 201.00m);

            hourEmp.Hours.Add(80.00);
            hourEmp.Hours.Add(90.00);
            hourEmp.Hours.Add(70.00);

            SalaryEmployee salEmp = new SalaryEmployee("Darth", "Vader", 850000.00m);

            salEmp.Hours.Add(80.00);
            salEmp.Hours.Add(90.00);
            salEmp.Hours.Add(70.00);

            myEmployees.Add(hourEmp);
            myEmployees.Add(salEmp);

            decimal payroll = 0;

            foreach (Employee e in myEmployees)
            {
                payroll += e.Pay(0, 2);
            }

            DisplayEmployeeInfo(hourEmp);
            Console.WriteLine();
            DisplayEmployeeInfo(salEmp);
            Console.WriteLine("\nPayroll total for 0-2: " + payroll.ToString("C"));
            Console.WriteLine();
            Console.WriteLine();

            foreach (Employee emp in myEmployees)
            {
                Console.WriteLine("Employee: " + emp);
                if (emp is HourlyEmployee)
                {
                    HourlyEmployee hourlyEmp = emp as HourlyEmployee;
                    Console.WriteLine("Hourly rate: " + hourlyEmp.HourlyRate);
                }
                if (emp is SalaryEmployee)
                {
                    // SalaryEmployee salaryEmp = emp as SalaryEmployee;   alternate below-->> seen more
                    // unboxing
                    SalaryEmployee salaryEmp = (SalaryEmployee)emp;
                    Console.WriteLine("Salary rate: " + salaryEmp.Salary);
                }
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            HourlyEmployee hourEmp = new HourlyEmployee(2, "Bill", "Gates", 15.00M);

            hourEmp.Hours.Add(80.0);
            hourEmp.Hours.Add(90.0);
            hourEmp.Hours.Add(70.0);
            hourEmp.Phone = "555-1212";
            hourEmp.Email = "*****@*****.**";

            DisplayEmployeeInfo(hourEmp, 0, 3);

            SalaryEmployee salaryEmp = new SalaryEmployee();

            salaryEmp.EmpNum    = 2;
            salaryEmp.FirstName = "Alan";
            salaryEmp.LastName  = "Turing";
            salaryEmp.Salary    = 40000.0M;
            salaryEmp.Hours.Add(80.0);
            salaryEmp.Hours.Add(80.0);
            salaryEmp.Hours.Add(80.0);
            salaryEmp.Phone = "123-4566";
            salaryEmp.Email = "What's an email?";

            DisplayEmployeeInfo(salaryEmp, 0, 3);

            List <Employee> myEmployees = new List <Employee>();

            myEmployees.Add(salaryEmp);
            myEmployees.Add(hourEmp);
            decimal payroll = 0;

            foreach (Employee employee in myEmployees)
            {
                payroll += employee.Pay(0, 3);
            }
            Console.WriteLine("Payroll total for 0-2: " + payroll.ToString());
            Console.WriteLine();

            foreach (Employee employee in myEmployees)
            {
                Console.WriteLine("Employee: " + employee);
                if (employee is HourlyEmployee)
                {
                    //HourlyEmployee hourlyEmp = employee as HourlyEmployee;
                    HourlyEmployee hourlyEmp = (HourlyEmployee)employee;
                    Console.WriteLine("Hourly rate: " + hourlyEmp.HourlyRate);
                }
                if (employee is SalaryEmployee)
                {
                    SalaryEmployee salEmp = employee as SalaryEmployee;
                    Console.WriteLine("Salary: " + salEmp.Salary);
                }
            }

            Department dep   = new Department("Sales Department");
            Department redep = new Department("Department of redundancy department");

            PhoneBook pb = new PhoneBook();

            pb.PhoneBookEntries.Add(hourEmp);
            pb.PhoneBookEntries.Add(salaryEmp);
            pb.PhoneBookEntries.Add(dep);
            pb.PhoneBookEntries.Add(redep);

            Console.WriteLine("\nCompany Phone Book");
            Console.WriteLine(pb.GetPhoneBook());
        }