コード例 #1
0
        public static void main(string[] args)
        {
            // create five-element Employee array
            Employee[] employees = new Employee[4];

            // initialize array with Employees
            employees[0] = new SalariedEmployee("John", "Smith",
               "111-11-1111", 800M);
            employees[1] = new HourlyEmployee("Karen", "Price",
               "222-22-2222", 16.75M, 40M);
            employees[2] = new CommissionEmployee("Sue", "Jones",
               "333-33-3333", 10000M, .06M);
            employees[3] = new BasePlusCommissionEmployee("Bob", "Lewis",
               "444-44-4444", 5000M, .04M, 300M);

            Console.WriteLine("Employees processed polymorphically:\n");

            // generically process each element in array employees
            foreach (var currentEmployee in employees)
            {
                Console.WriteLine(currentEmployee); // invokes ToString
                Console.WriteLine("earned {0:C}\n",
                   currentEmployee.GetPaymentAmount());
            } // end foreach

            Console.Read();
        }
コード例 #2
0
        public static void Main(string[] args)
        {
            IPayable[] payableObjects = new IPayable[7];

            payableObjects[0] = new Invoice("01234", "seat", 2, 375.00M );
            payableObjects[1] = new Invoice("56789", "tire", 4, 79.95M);
            payableObjects[ 2 ] = new HourlyEmployee( "John", "Smith",
                "111-11-1111", 15.75M, 40);
            payableObjects[ 3 ] = new SalariedEmployee( "Lisa", "Barnes",
                "888-88-8888", 1200.00M);
            payableObjects[4] = new CommissionEmployee("Kory", "Barnes",
                "777-77-7777", 1800.00M, .75M);
            payableObjects[5] = new BasePlusCommissionEmployee("Grant", "Brick",
                "555-55-5555", 5000.00M, .5M, 80000.00M);
            payableObjects[6] = new PieceWorker("Greg", "Jones", "999-99-999", 80.00M, 800.00M);

            Console.Write(
                "Invoices and Employees processed polymoprhically:\n");

            foreach(var currentPayable in payableObjects)
            {
                Console.WriteLine("{0}\npayment due: {1:C}\n",
                    currentPayable, currentPayable.GetPaymentAmount());
            }
        }
コード例 #3
0
        static void Main(string[] args)
        {
            // create birthday objects for each employee
            var birthday1 = new Date(2, 15, 1963);
            var birthday2 = new Date(4, 15, 1992);
            var birthday3 = new Date(9, 19, 1971);
            var birthday4 = new Date(12, 1, 1987);

            // create derived-class objects
            var salariedEmployee           = new SalariedEmployee("Jim", "Miller", "777-77-7777", birthday1, 2650.00M);
            var hourlyEmployee             = new HourlyEmployee("Thomas", "Swoon", "888-88-8888", birthday2, 27.0M, 40.0M);
            var commissionEmployee         = new CommissionEmployee("Sam", "Overton", "444-44-4444", birthday3, 8650.00M, .15M);
            var basePlusCommissionEmployee = new BasePlusCommissionEmployee("Rufus", "Mitchell", "333-33-3333", birthday4, 12520.00M, .08M, 100.00M);

            Console.WriteLine("Employee processed individually:\n");
            Console.WriteLine($"{salariedEmployee}\nearned: " + $"{salariedEmployee.Earnings():C}\n");
            Console.WriteLine($"{hourlyEmployee}\nearned: {hourlyEmployee.Earnings():C}\n");
            Console.WriteLine($"{commissionEmployee}\nearned: " + $"{commissionEmployee.Earnings():C}\n");
            Console.WriteLine($"{basePlusCommissionEmployee}\nearned: " + $"{basePlusCommissionEmployee.Earnings():C}\n");

            // create List<Employee> and initialize with employee objects
            var employees = new List <Employee>()
            {
                salariedEmployee, hourlyEmployee, commissionEmployee, basePlusCommissionEmployee
            };

            // create array with the employee's birthday months
            int[] birthdayMonths = { birthday1.Month, birthday2.Month, birthday3.Month, birthday4.Month };

            string[] monthsList = { "null", "Jan.", "Feb.", "Mar.", "Apr.", "May", "June", "July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec." };


            Console.WriteLine("Employees processed polymorphically:\n");

            // counter to index though employee's birthday months list
            int counter = 0;

            // generically process each element in employees
            foreach (var currentEmployee in employees)
            {
                // varibale to add the employees monthly eranings
                decimal total = 0;
                Console.WriteLine(currentEmployee); // invokes ToString

                // display each employee monthly earning and birthday month bonus
                for (int months = 1; months <= 12; ++months)
                {
                    int currentEmployeeBirthdayMonth = birthdayMonths[counter];

                    if (currentEmployeeBirthdayMonth == months)
                    {
                        decimal monthlyBonus = currentEmployee.Earnings() + 100.0M;
                        Console.WriteLine($"{monthsList[months],12}: {monthlyBonus,10:C} Bonus Month!");
                        total += monthlyBonus;
                    }
                    else
                    {
                        Console.WriteLine($"{monthsList[months],12}: {currentEmployee.Earnings(),10:C}");
                        total += currentEmployee.Earnings();
                    }
                }

                // display total earnings
                Console.WriteLine($"Total earned: {total:C}\n");

                // incrementing the index counter
                counter++;
            }
            Console.ReadLine();
        }