static void Main(string[] args) { Employee salariedEmployee = new SalariedEmployee("Alice", "Cooper", "123456789", 1000); Employee hourlyEmployee = new HourlyEmployee("Bob", "Dilan", "987654321", 20, 40); Employee commissionEmployee = new CommissionEmployee("Patrick", "Frost", "135798462", 500, 0.2); Employee basePlusCommissionEmployee = new BasePlusCommissionEmployee("Kristine", "Chen", "095478236", 600, 0.3, 300); Console.WriteLine(salariedEmployee.ToString()); Console.WriteLine(hourlyEmployee.ToString()); Console.WriteLine(commissionEmployee.ToString()); Console.WriteLine(basePlusCommissionEmployee.ToString()); Console.Read(); }
static void Main(string[] args) { SalariedEmployee salariedEmployee = new SalariedEmployee("James", "Stuart", 123, 10000m); HourlyEmployee hourlyEmployee = new HourlyEmployee("Ruth", "Rutherford", 124, 40, 500m); CommissionEmployee commissionEmployee = new CommissionEmployee("Ruth", "Rutherford", 124, 0.08, 50000m); BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee("Ruth", "Rutherford", 124, 7000m, 0.06, 20000m); Invoice invoice1 = new Invoice("Fan", "Century Standing Rechargeable Fan", 18000M, 2); Invoice invoice2 = new Invoice("Projector", "EPSON Projector 3456", 68000M, 1); Console.WriteLine(salariedEmployee); Console.WriteLine($"Earnings: {salariedEmployee.Earnings():C}\n"); Console.WriteLine(hourlyEmployee); Console.WriteLine($"Earnings: {hourlyEmployee.Earnings():C}\n"); Console.WriteLine(commissionEmployee); Console.WriteLine($"Earnings: {commissionEmployee.Earnings():C}\n"); Console.WriteLine(basePlusCommissionEmployee); Console.WriteLine($"Earnings: {basePlusCommissionEmployee.Earnings():C}\n"); Console.WriteLine("=== POLYMORPHICALLY ACCESSING METHODS OF OBJECTS CREATED FROM CONCRETE DERIVED-CLASSES OF AN ABSTRACT BASE CLASS ======="); List <IPayment> paymentsDue = new List <IPayment> () { salariedEmployee, hourlyEmployee, commissionEmployee, basePlusCommissionEmployee, invoice1, invoice2 }; foreach (var payment in paymentsDue) { Console.WriteLine(payment); if (payment is BasePlusCommissionEmployee) { var baseEmployee = (BasePlusCommissionEmployee)payment; baseEmployee.BaseSalary *= 1.10M; Console.WriteLine($"Salary incremented by: 10% - {baseEmployee.BaseSalary:C}"); } Console.WriteLine($"Amount to be paid: {payment.GetPaymentAmount():C}\n"); } }
static void Main(string[] args) { SalariedEmployee salariedEmployee = new SalariedEmployee("James", "Stuart", 123, 10000m); HourlyEmployee hourlyEmployee = new HourlyEmployee("Ruth", "Rutherford", 124, 40, 500m); CommissionEmployee commissionEmployee = new CommissionEmployee("Ruth", "Rutherford", 124, 0.08, 50000m); BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee("Ruth", "Rutherford", 124, 7000m, 0.06, 20000m); Invoice invoice1 = new Invoice("Fan", "Century Standing Rechargeable Fan", 18000M, 2); Invoice invoice2 = new Invoice("Projector", "EPSON Projector 3456", 68000M, 1); Console.WriteLine("=== POLYMORPHICALLY ACCESSING METHODS OF OBJECTS FROM A GENERIC COLLECTION CLASS ======="); // List<IPayment> paymentsDue = new List<IPayment> () { salariedEmployee, hourlyEmployee, commissionEmployee, basePlusCommissionEmployee, invoice1, invoice2 }; PaymentCollection <IPayment> payments = new PaymentCollection <IPayment> (6); payments.Add(salariedEmployee); payments.Add(hourlyEmployee); payments.Add(basePlusCommissionEmployee); payments.Add(commissionEmployee); payments.Add(invoice1); payments.Add(invoice2); foreach (var payment in payments) { try { Console.WriteLine(payment); if (payment is BasePlusCommissionEmployee) { var baseEmployee = (BasePlusCommissionEmployee)payment; baseEmployee.BaseSalary *= 1.10M; Console.WriteLine($"Salary incremented by: 10% - {baseEmployee.BaseSalary:C}"); } Console.WriteLine($"Amount to be paid: {payment.GetPaymentAmount():C}\n"); } catch (System.Exception ex) { Console.WriteLine(ex.Message); } } }
static void Main(string[] args) { SalariedEmployee salariedEmployee = new SalariedEmployee("James", "Stuart", 123, 10000m); HourlyEmployee hourlyEmployee = new HourlyEmployee("Ruth", "Rutherford", 124, 40, 500m); CommissionEmployee commissionEmployee = new CommissionEmployee("Ruth", "Rutherford", 124, 0.08, 50000m); BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee("Ruth", "Rutherford", 124, 7000m, 0.06, 20000m); Console.WriteLine(salariedEmployee); Console.WriteLine($"Earnings: {salariedEmployee.Earnings():C}\n"); Console.WriteLine(hourlyEmployee); Console.WriteLine($"Earnings: {hourlyEmployee.Earnings():C}\n"); Console.WriteLine(commissionEmployee); Console.WriteLine($"Earnings: {commissionEmployee.Earnings():C}\n"); Console.WriteLine(basePlusCommissionEmployee); Console.WriteLine($"Earnings: {basePlusCommissionEmployee.Earnings():C}\n"); Console.WriteLine("=== POLYMORPHICALLY ACCESSING METHODS OF OBJECTS CREATED FROM CONCRETE DERIVED-CLASSES OF AN ABSTRACT BASE CLASS ======="); List <Employee> employees = new List <Employee> () { salariedEmployee, hourlyEmployee, commissionEmployee, basePlusCommissionEmployee }; foreach (var employee in employees) { Console.WriteLine(employee); if (employee is BasePlusCommissionEmployee) { var baseEmployee = (BasePlusCommissionEmployee)employee; baseEmployee.BaseSalary *= 1.10M; Console.WriteLine($"Salary incremented by: 10% - {baseEmployee.BaseSalary:C}"); } Console.WriteLine($"Earnings: {employee.Earnings():C}\n"); } }
static void Main(string[] args) { SalariedEmployee salariedEmployee = new SalariedEmployee("John", "Smith", "111-11-1111", 800.00M); HourlyEmployee hourlyEmployee = new HourlyEmployee("Karen", "Price", "222-22-2222", 16.75M, 40.0M); CommissionEmployee commissionEmployee = new CommissionEmployee("Sue", "Jones", "333-33-3333", 10000.00M, .06M); BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee("Bob", "Lewis", "444-44-4444", 5000.00M, .04M, 300.00M); Console.WriteLine("Employees processed individually:\n"); Console.WriteLine("{0}\nearned: {1:C}\n", salariedEmployee, salariedEmployee.Earnings()); Console.WriteLine("{0}\nearned: {1:C}\n", hourlyEmployee, hourlyEmployee.Earnings()); Console.WriteLine("{0}\nearned: {1:C}\n", commissionEmployee, commissionEmployee.Earnings()); Console.WriteLine("{0}\nearned: {1:C}\n", basePlusCommissionEmployee, basePlusCommissionEmployee.Earnings()); Employee[] employees = new Employee[4]; employees[0] = salariedEmployee; employees[1] = hourlyEmployee; employees[2] = commissionEmployee; employees[3] = basePlusCommissionEmployee; foreach (var currentEmployee in employees) { Console.WriteLine(currentEmployee); if (currentEmployee is BasePlusCommissionEmployee) { //downcast Employee reference to BasePlusCommissionEmployee reference BasePlusCommissionEmployee employee = (BasePlusCommissionEmployee)currentEmployee; employee.BaseSalary *= 1.10M; Console.WriteLine("new base salary with 10% increase is: {0:C}", employee.BaseSalary); } Console.WriteLine("earned{0:C}\n", currentEmployee.Earnings()); } for (int j = 0; j < employees.Length; j++) { Console.WriteLine("Employee {0} is a {1}", j, employees[j].GetType()); } Console.ReadLine(); }
static void Main(string[] args) { // declarations Employee[] employees = new Employee[5]; Date tempDate; // construct employees employees[0] = new SalariedEmployee("John", "Smith", "111-11-1111", "5/1/1970", 800.00M); employees[1] = new HourlyEmployee("Karen", "Price", "222-22-2222", "10/2/1980", 16.75M, 40.0M); employees[2] = new CommissionEmployee("Sue", "Jones", "333-33-3333", "6/3/1990", 10000.00M, .06M); employees[3] = new BasePlusCommissionEmployee("Bob", "Lewis", "444-44-4444", "1/4/1965", 5000.00M, .04M, 300.00M); employees[4] = new HourlyEmployee("Michael", "DeSanto", "555-55-5555", "7/9/1985", 20.00M, 45.0M); // display payroll for (MonthNames month = MonthNames.January; month <= MonthNames.December; ++month) { Console.WriteLine("{0} payroll:", month); foreach (Employee selectedEmployee in employees) { tempDate = (Date)selectedEmployee.BirthDate; if (tempDate.Month == (int)month) // eligible for birth month bonus { Console.WriteLine("{0}, {1} earned {2:C}.", selectedEmployee.LastName, selectedEmployee.FirstName, selectedEmployee.Earnings() + 100.00M); } else // regular pay { Console.WriteLine("{0}, {1} earned {2:C}.", selectedEmployee.LastName, selectedEmployee.FirstName, selectedEmployee.Earnings()); } } Console.WriteLine(); } // hold console Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
static void Main(string[] args) { // create derived class objects SalariedEmployee salariedEmployee1 = new SalariedEmployee("John", "Smith", "111-11-1111", 6, 15, 1944, 800M); HourlyEmployee hourlyEmployee1 = new HourlyEmployee("Karen", "Price", "222-22-2222", 12, 29, 1960, 16.75M, 40M); CommissionEmployee commissionEmployee1 = new CommissionEmployee("Sue", "Jones", "333-33-3333", 9, 8, 1954, 10000M, .06M); BasePlusCommissionEmployee basePlusCommissionEmployee1 = new BasePlusCommissionEmployee("Bob", "Lewis", "444-44-4444", 3, 2, 1965, 5000M, .04M, 300M); SalariedEmployee salariedEmployee2 = new SalariedEmployee("Afatarli", "Valeri", "511-11-1111", 1, 5, 1915, 600M); HourlyEmployee hourlyEmployee2 = new HourlyEmployee("Henry", "Buris", "622-22-2222", 2, 9, 1970, 14.75M, 36M); CommissionEmployee commissionEmployee2 = new CommissionEmployee("Anthony", "Doss", "733-33-3333", 4, 18, 1944, 10045M, .07M); BasePlusCommissionEmployee basePlusCommissionEmployee2 = new BasePlusCommissionEmployee("Bob", "Lewis", "844-44-4444", 5, 1, 1960, 4300M, .05M, 290M); SalariedEmployee salariedEmployee3 = new SalariedEmployee("Linda", "Ha", "911-11-1111", 7, 25, 1968, 400M); HourlyEmployee hourlyEmployee3 = new HourlyEmployee("Brian", "Louie", "122-22-2222", 8, 6, 1966, 18.85M, 60M); CommissionEmployee commissionEmployee3 = new CommissionEmployee("Leon", "Powell", "233-33-3333", 10, 22, 1959, 10100M, .07M); BasePlusCommissionEmployee basePlusCommissionEmployee3 = new BasePlusCommissionEmployee("David", "Tong", "344-44-4444", 11, 8, 1970, 5080M, .08M, 400M); // printing all types of Employee's info and their earnings Console.WriteLine("Employees processed individually:\n"); Console.WriteLine("{0}\n{1}: {2:C}\n", salariedEmployee1, "earned", salariedEmployee1.Earnings()); Console.WriteLine("{0}\n{1}: {2:C}\n", hourlyEmployee1, "earned", hourlyEmployee1.Earnings()); Console.WriteLine("{0}\n{1}: {2:C}\n", commissionEmployee1, "earned", commissionEmployee1.Earnings()); Console.WriteLine("{0}\n{1}: {2:C}\n", basePlusCommissionEmployee1, "earned", basePlusCommissionEmployee1.Earnings()); Console.WriteLine("{0}\n{1}: {2:C}\n", salariedEmployee2, "earned", salariedEmployee2.Earnings()); Console.WriteLine("{0}\n{1}: {2:C}\n", hourlyEmployee2, "earned", hourlyEmployee2.Earnings()); Console.WriteLine("{0}\n{1}: {2:C}\n", commissionEmployee2, "earned", commissionEmployee2.Earnings()); Console.WriteLine("{0}\n{1}: {2:C}\n", basePlusCommissionEmployee2, "earned", basePlusCommissionEmployee2.Earnings()); Console.WriteLine("{0}\n{1}: {2:C}\n", salariedEmployee3, "earned", salariedEmployee3.Earnings()); Console.WriteLine("{0}\n{1}: {2:C}\n", hourlyEmployee3, "earned", hourlyEmployee3.Earnings()); Console.WriteLine("{0}\n{1}: {2:C}\n", commissionEmployee3, "earned", commissionEmployee3.Earnings()); Console.WriteLine("{0}\n{1}: {2:C}\n", basePlusCommissionEmployee3, "earned", basePlusCommissionEmployee3.Earnings()); // create four-element Employee array Employee[] employees = new Employee[12]; // initialize array with Employees employees[0] = salariedEmployee1; employees[1] = hourlyEmployee1; employees[2] = commissionEmployee1; employees[3] = basePlusCommissionEmployee1; employees[4] = salariedEmployee2; employees[5] = hourlyEmployee2; employees[6] = commissionEmployee2; employees[7] = basePlusCommissionEmployee2; employees[8] = salariedEmployee3; employees[9] = hourlyEmployee3; employees[10] = commissionEmployee3; employees[11] = basePlusCommissionEmployee3; int currentMonth; // get and validate current month do { Console.Write("Enter the current month (1 - 12): "); currentMonth = Convert.ToInt32(Console.ReadLine()); Console.WriteLine(); } while ((currentMonth < 1) || (currentMonth > 12)); Console.WriteLine("Employees processed polymorphically:\n"); // generically process each element in array employees foreach (var currentEmployee in employees) { Console.WriteLine(currentEmployee); // invokes ToString // determine whether element is a BasePlusCommissionEmployee if (currentEmployee is BasePlusCommissionEmployee) { // downcast Employee reference to // BasePlusCommissionEmployee reference BasePlusCommissionEmployee employee = (BasePlusCommissionEmployee)currentEmployee; employee.BaseSalary *= 1.1M; Console.WriteLine( "new base salary with 10% increase is: {0:C}", employee.BaseSalary); } // end if // if month of employee's birthday, add $100 to salary if (currentMonth == currentEmployee.BirthDate.Month) { Console.WriteLine( "earned {0:C} {1}\n", currentEmployee.Earnings(), "plus $100.00 birthday bonus"); } else { Console.WriteLine( "earned {0:C}\n", currentEmployee.Earnings()); } } // end for // get type name of each object in employees array for (int j = 0; j < employees.Length; j++) { Console.WriteLine("Employee {0} is a {1}", j, employees[j].GetType()); } // Pauses the console and showcase the results // prompt user any key to exit the console Console.WriteLine("\nPress any key to exit."); Console.ReadKey(); }