public static void Main(string[] args) { // create five-element Employee array Employee[] employees = new Employee[5]; // 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); employees[4] = new PieceWorker("Tony", "Rosella", "555-55-5555", 15.50M, 20M); 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.Earnings()); } // end foreach } // end Main
public static void Main( string[] args ) { // create five-element Employee array Employee[] employees = new Employee[ 5 ]; // 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 ); employees[4] = new PieceWorker("Tony", "Rosella", "555-55-5555", 15.50M, 20M); 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.Earnings() ); } // end foreach }
static void Main(string[] args) { //Declare Variables string firstName = "Carlos"; string lastName = "Varlack"; string ssn = "555-55-5555"; int sales = 6000; decimal rate = (decimal)0.4; decimal salary = 3000; //(string first, string last, string socialSec, decimal sales, //decimal rate, decimal salary) //create object BasePlusCommissionEmployee employee = new BasePlusCommissionEmployee(firstName, lastName, ssn, sales, rate, salary); //Explain the program to the user ExplainToUser(); //Display the results to the user DisplayResults(employee); // Wait until the user desides to close the program Console.ReadLine(); }
public static void Main(string[] args) { // instantiate BasePlusCommissionEmployee object BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee("Bob", "Lewis", "333-33-3333", 5000.00M, .04M, 300.00M); // display BasePlusCommissionEmployee's data Console.WriteLine( "Employee information obtained by properties and methods: \n"); Console.WriteLine("First name is {0}", basePlusCommissionEmployee.FirstName); Console.WriteLine("Last name is {0}", basePlusCommissionEmployee.LastName); Console.WriteLine("Social security number is {0}", basePlusCommissionEmployee.SocialSecurityNumber); Console.WriteLine("Gross sales are {0:C}", basePlusCommissionEmployee.GrossSales); Console.WriteLine("Commission rate is {0:F2}", basePlusCommissionEmployee.CommissionRate); Console.WriteLine("Earnings are {0:C}", basePlusCommissionEmployee.Earnings()); Console.WriteLine("Base salary is {0:C}", basePlusCommissionEmployee.BaseSalary); basePlusCommissionEmployee.BaseSalary = 1000.00M; // set base salary Console.WriteLine("\n{0}:\n\n{1}", "Updated employee information obtained by ToString", basePlusCommissionEmployee); Console.WriteLine("Earnings: {0:C}", basePlusCommissionEmployee.Earnings()); } // end Main
public void TestRate() { BasePlusCommissionEmployee myEmp = new BasePlusCommissionEmployee("AAA", "BB", "123", 540.00M, 0.3M, 30000.0M); myEmp.CommissionRate = -1; }
public void TestSalary() { BasePlusCommissionEmployee myEmp = new BasePlusCommissionEmployee("AAA", "BB", "123", 540.00M, 0.3M, 30000.0M); myEmp.Salary = -1; }
public static void Main( string[] args ) { // instantiate BasePlusCommissionEmployee object BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee( "Bob", "Lewis", "333-33-3333", 5000.00M, .04M, 300.00M ); // display BasePlusCommissionEmployee's data Console.WriteLine( "Employee information obtained by properties and methods: \n" ); Console.WriteLine( "First name is {0}", basePlusCommissionEmployee.FirstName ); Console.WriteLine( "Last name is {0}", basePlusCommissionEmployee.LastName ); Console.WriteLine( "Social security number is {0}", basePlusCommissionEmployee.SocialSecurityNumber ); Console.WriteLine( "Gross sales are {0:C}", basePlusCommissionEmployee.GrossSales ); Console.WriteLine( "Commission rate is {0:F2}", basePlusCommissionEmployee.CommissionRate ); Console.WriteLine( "Earnings are {0:C}", basePlusCommissionEmployee.Earnings() ); Console.WriteLine( "Base salary is {0:C}", basePlusCommissionEmployee.BaseSalary ); basePlusCommissionEmployee.BaseSalary = 1000.00M; // set base salary Console.WriteLine( "\n{0}:\n\n{1}", "Updated employee information obtained by ToString", basePlusCommissionEmployee ); Console.WriteLine( "earnings: {0:C}", basePlusCommissionEmployee.Earnings() ); }
static void Main() { // instantiate BasePlusCommissionEmployee object var employee = new BasePlusCommissionEmployee("Bob", "Lewis", "333-33-3333", 5000.00M, .04M, 300.00M); // display BasePlusCommissionEmployee's data Console.WriteLine( "Employee information obtained by properties and methods: \n"); Console.WriteLine($"First name is {employee.FirstName}"); Console.WriteLine($"Last name is {employee.LastName}"); Console.WriteLine( $"Social security number is {employee.SocialSecurityNumber}"); Console.WriteLine($"Gross sales are {employee.GrossSales:C}"); Console.WriteLine( $"Commission rate is {employee.CommissionRate:F2}"); Console.WriteLine($"Earnings are {employee.Earnings():C}"); Console.WriteLine($"Base salary is {employee.BaseSalary:C}"); employee.BaseSalary = 1000.00M; // set base salary Console.WriteLine( "\nUpdated employee information obtained by ToString:\n"); Console.WriteLine(employee); Console.WriteLine($"earnings: {employee.Earnings():C}"); }
static void Main() { // create derived-class objects var salariedEmployee = new SalariedEmployee("John", "Smith", "111-11-1111", 800.00M); var hourlyEmployee = new HourlyEmployee("Karen", "Price", "222-22-2222", 16.75M, 40.0M); var commissionEmployee = new CommissionEmployee("Sue", "Jones", "333-33-3333", 10000.00M, .06M); var basePlusCommissionEmployee = new BasePlusCommissionEmployee("Bob", "Lewis", "444-44-4444", 5000.00M, .04M, 300.00M); Console.WriteLine("Employees 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 }; Console.WriteLine("Employees processed polymorphically:\n"); // generically process each element in 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 var employee = (BasePlusCommissionEmployee)currentEmployee; employee.BaseSalary *= 1.10M; Console.WriteLine("new base salary with 10% increase is: " + $"{employee.BaseSalary:C}"); } Console.WriteLine($"earned: {currentEmployee.Earnings():C}\n"); } // get type name of each object in employees for (int j = 0; j < employees.Count; j++) { Console.WriteLine( $"Employee {j} is a {employees[j].GetType()}"); } }
public void Earnings() { BasePlusCommissionEmployee myEmp = new BasePlusCommissionEmployee("AAA", "BB", "123", 540.00M, 0.3M, 30000.0M); decimal expValue = 30162.0M; // NUnit Assert.AreEqual(expValue, myEmp.Earnings()); }
//Constructor public PayrollSystemTest() { payableObjects[0] = new SalariedEmployee("John", "Smith", "111-11-1111", 700M); payableObjects[1] = new SalariedEmployee("Antonio", "Smith", "555-55-5555", 800M); payableObjects[2] = new SalariedEmployee("Victor", "Smith", "444-44-4444", 600M); payableObjects[3] = new HourlyEmployee("Karen", "Price", "222-22-2222", 16.75M, 40M); payableObjects[4] = new HourlyEmployee("Ruben", "Zamora", "666-66-6666", 20.00M, 40M); payableObjects[5] = new CommissionEmployee("Sue", "Jones", "333-33-3333", 10000M, .06M); payableObjects[6] = new BasePlusCommissionEmployee("Bob", "Lewis", "777-77-7777", 5000M, .04M, 300M); payableObjects[7] = new BasePlusCommissionEmployee("Lee", "Duarte", "888-88-888", 5000M, .04M, 300M); }
//Show the parameters to the user public static void DisplayResults(BasePlusCommissionEmployee employee) { Console.WriteLine("\t First Name: " + employee.Commission1.FirstName); Console.WriteLine("\t Last Name: " + employee.Commission1.LastName); Console.WriteLine("\t Social Security: " + employee.Commission1.SocialSecurityNumber); Console.WriteLine("\t Gross Sales: " + employee.Commission1.GrossSales); Console.WriteLine("\t Commission Rate: " + employee.Commission1.CommissionRate); Console.WriteLine("\t Base Salary: " + employee.BaseSalary); Console.WriteLine("\t Earnings: " + employee.Earnings()); Console.WriteLine(); }
public void TestToString() { BasePlusCommissionEmployee myEmp = new BasePlusCommissionEmployee("AAA", "BB", "123", 540.00M, 0.3M, 30000.0M); string expResult = "commission employee: AAA BB\n" + "social security number: 123\n" + "gross sales: $540.00\n" + "commission rate: 0.30" + " Base Salary: $30,000.00"; Assert.AreEqual(expResult, myEmp.ToString()); }
public void TestObj1() { BasePlusCommissionEmployee myEmp = new BasePlusCommissionEmployee("AAA", "BB", "123", 540.00M, 0.3M, 30000.0M); // NUnit Assert.AreEqual("AAA", myEmp.FirstName); Assert.AreEqual("BB", myEmp.LastName); Assert.AreEqual("123", myEmp.SocialSecurityNumber); Assert.AreEqual(540.00M, myEmp.GrossSales); Assert.AreEqual(0.3M, myEmp.CommissionRate); Assert.AreEqual(30000.0M, myEmp.Salary); }
static void Main(string[] args) { SalariedEmployee salariedEmployee = new SalariedEmployee("Peter", "Parker", "SPIDER001", 10.0M); HourlyEmployee hourlyEmployee = new HourlyEmployee("Bruce", "Wayne", "BAT001", 15.0M, 45); CommissionEmployee commissionEmployee = new CommissionEmployee("David", "Banner", "HULK001"); BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee("Clark", "Kent", "SUPER001"); Console.WriteLine(hourlyEmployee.ToString()); Console.WriteLine(salariedEmployee.ToString()); Console.WriteLine(commissionEmployee.ToString()); Console.WriteLine(basePlusCommissionEmployee.ToString()); }
public 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", "Jonse", "333-33-333", 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; Console.WriteLine("Employees processed polymorphically:\n"); foreach (Employee currentEmployee in employees) { Console.WriteLine(currentEmployee); if (currentEmployee is BasePlusCommissionEmployee) { 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()); } }
public static void Main( string [] args) { bool loop = true; while(loop == true) { Console.WriteLine("Welcome to Employee Builder: "); Console.Read(); //instantiate Employee Array Employee[] Employees = new Employee[5]; //fill array with different 'Employee' Derived Types all functioning under their own version of the 'Employee' Methods. Employees[0] = new BasePlusCommissionEmployee("Jerry", "Redmond", "000-00-0000", 500.00M, .8M, 700M); Employees[1] = new CommissionEmployee("Carlos", "Reese", "111-11-1111", 999.00M, .45M); Employees[2] = new HourlyEmployee("Mike", "Luongo", "222-22-2222", 15.00M, 45M); Employees[3] = new PieceWorker("Marcus", "Shepherd", "333-33-3333", 3.50M, 100M); Employees[4] = new SalariedEmployee("Larry", "Peters", "444-44-4444", 75000M); //Employees[5] = new PieceWorker("Hakeem", "Rogers", "555-55-5555", 5.0M,1); Console.WriteLine("Employees Earnings & System Signature through ToString: \n\n{0}\t\t{1}", "Earnings:", "String:"); foreach(Employee employee in Employees) { Console.WriteLine( employee.GetPaymentAmount() ); }//end foreach for (int i = 0; i <= 4; i++) { if (Employees[i].GetType is BasePlusCommissionEmployee) { BasePlusCommissionEmployee employee = (BasePlusCommissionEmployee)Employees[i]; employee.BaseSalary = employee.BaseSalary * 1.1M; }//end if Console.WriteLine("\n" + Employees[i].GetPaymentAmount() /* + "\t\t" + Employees[i].Earnings() + Employees[i].*/); }//end for statement printing GetPaymentAmount Console.ReadLine(); }//End while }
static void Main() { // create derived-class objects var salariedEmployee = new SalariedEmployee("John", "Smith", "111-11-1111", 800.00M); var hourlyEmployee = new HourlyEmployee("Karen", "Price", "222-22-2222", 16.75M, 40.0M); var commissionEmployee = new CommissionEmployee("Sue", "Jones", "333-33-3333", 10000.00M, .06M); var basePlusCommissionEmployee = new BasePlusCommissionEmployee("Bob", "Lewis", "444-44-4444", 5000.00M, .04M, 300.00M); var pieceWorkEmployee = new PieceWorkEmployee("Penny", "Parker", "555-55-5555", 1.75M, 600); // create List<Employee> and initialize with employee objects var employees = new List <Employee>() { salariedEmployee, hourlyEmployee, commissionEmployee, basePlusCommissionEmployee, pieceWorkEmployee }; Console.WriteLine("Employees processed polymorphically:\n"); // generically process each element in 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 var employee = (BasePlusCommissionEmployee)currentEmployee; employee.BaseSalary *= 1.10M; Console.WriteLine("new base salary with 10% increase is: " + $"{employee.BaseSalary:C}"); } Console.WriteLine($"earned: {currentEmployee.Earnings():C}\n"); } }
public static void Main(string[] args) { // create six-element IPayable array IPayable[] payableObjects = new IPayable[6]; // populate array with objects that implement IPayable payableObjects[0] = new Invoice("01234", "seat", 2, 375.00M); payableObjects[1] = new Invoice("56789", "tire", 4, 79.95M); payableObjects[2] = new SalariedEmployee("John", "Smith", "111-11-1111", 800.00M); /***** Modified code for assignment *****/ // Add three new payable objects payableObjects[3] = new HourlyEmployee("Robert", "Millsaps", "222-22-2222", 12.5M, 20M); payableObjects[4] = new CommissionEmployee("Albert", "Einstein", "333-33-3333", 1000M, 0.314159M); payableObjects[5] = new BasePlusCommissionEmployee("Bill", "Gates", "444-44-4444", 1000M, 0.314159M, 1000000M); // Iterate over all payable objects foreach (IPayable currentPayable in payableObjects) { // Output string representation Console.WriteLine(currentPayable); // Increase base salary by 10% for Base Plus Commission Employee if (currentPayable is BasePlusCommissionEmployee) { BasePlusCommissionEmployee employee = (BasePlusCommissionEmployee)currentPayable; employee.BaseSalary *= 1.10M; // Output new base salary Console.WriteLine("new base salary with 10% increase is: {0:C}", employee.BaseSalary); } // Output payment for each payable object Console.WriteLine("payment due: {0:C}", currentPayable.GetPaymentAmount()); Console.WriteLine(); } // Freeze console window Console.ReadLine(); /***** End Modified code for assignment *****/ }
public static void Main(string[] args) { BasePlusCommissionEmployee employee = new BasePlusCommissionEmployee("Bob", "Lewis", "333-33-3333", 5000.00M, .04M, 300.00M); Console.WriteLine("Employee information obtained by properties and methods: \n"); Console.WriteLine("{0} {1}", "First name is", employee.FirstName); Console.WriteLine("{0} {1}", "Last name is", employee.LastName); Console.WriteLine("{0} {1}", "Social security number is", employee.SocialSecurityNumber); Console.WriteLine("{0} {1:C}", "Gross sales are", employee.GrossSales); Console.WriteLine("{0} {1:C}", "Commission rate is", employee.CommissionRate); Console.WriteLine("{0} {1:C}", "Earnings are", employee.Earnings()); Console.WriteLine("{0} {1:C}", "Base salary is", employee.BaseSalary); employee.BaseSalary = 1000.00M; Console.WriteLine("\n{0}:\n\n{1}", "Updated employee information obtained ToString", employee); Console.Write("earnings: {0:C}", employee.Earnings()); Console.ReadKey(); }
public static void Main() { BasePlusCommissionEmployee employee = new BasePlusCommissionEmployee("Nordine", "Sebkhi", "999-99-9999", 5000.00M, 0.06M, 300.00M); Console.WriteLine("Employee info obtained by properties and methods: \n"); Console.WriteLine("{0} {1}", "First name is", employee.FirstName); Console.WriteLine("{0} {1}", "Last name is", employee.LastName); Console.WriteLine("{0} {1}", "SSN is", employee.SocialSecurityNumber); Console.WriteLine("{0} {1:C}", "Gross sale is", employee.GrossSales); Console.WriteLine("{0} {1:F2}", "Commission rate is", employee.CommissionRate); Console.WriteLine("{0} {1:C}", "Earnings are", employee.Earnings()); Console.WriteLine("{0} {1:C}", "Base salary is", employee.BaseSalary); employee.BaseSalary = 100.00M; Console.WriteLine("\n{0}:\n\n{1}", "Updated employee info obtained by ToString", employee); Console.WriteLine("Earnings: {0:C}", employee.Earnings()); Console.ReadLine(); } // end class Main
static void Main(string[] args) { // Test each class by entering employee via constructor and printing details HourlyEmployee Hire1 = new HourlyEmployee("Joe","Dunn","070723889",1000,40); Console.WriteLine(Hire1); Console.WriteLine("{0}\t {1}\n","Total earnings:",Hire1.Earnings()); SalariedEmployee Hire2 = new SalariedEmployee("John", "Rex", "970723889", 80000); Console.WriteLine(Hire2); Console.WriteLine("{0}\t {1}\n", "Total earnings:", Hire2.Earnings()); BasePlusCommissionEmployee Hire3 = new BasePlusCommissionEmployee("Reg", "Smith", "370723889", 10000, 1/5, 40000); Console.WriteLine(Hire3); Console.WriteLine("{0}\t {1}\n", "Total earnings:", Hire3.Earnings()); CommissionEmployee Hire4 = new CommissionEmployee("Lisa", "Pepper", "170723889", 100000, 1 / 5); Console.WriteLine(Hire4); Console.WriteLine("{0}\t {1}\n", "Total earnings:", Hire4.Earnings()); Console.Read(); }
static void Main() { // create derived-class objects var salariedEmployee = new SalariedEmployee("John", "Smith", "111-11-1111", 800.00M); var hourlyEmployee = new HourlyEmployee("Karen", "Price", "222-22-2222", 16.75M, 40.0M); var commissionEmployee = new CommissionEmployee("Sue", "Jones", "333-33-3333", 10000.00M, .06M); var basePlusCommissionEmployee = new BasePlusCommissionEmployee("Bob", "Lewis", "444-44-4444", 5000.00M, .04M, 300.00M); var pieceWorkerEmployee = new PieceWorkerEmployee("Spencer", "Dee", "111-11-1111" , 300M, 5000.000M); new PieceWorkerEmployee("Andrew", "Wright", "222-22-2222", 400M, 3000M); // create List<Employee> and initialize with employee objects var employees = new List <Employee>() { salariedEmployee, hourlyEmployee, commissionEmployee, basePlusCommissionEmployee, pieceWorkerEmployee }; WriteLine("Employees processed polymorphically:\n"); // generically process each element in employees foreach (var currentEmployee in employees) { WriteLine(currentEmployee); // invokes ToString WriteLine($"earned: {currentEmployee.Earnings():C}\n"); } // get type name of each object in employees for (int j = 0; j < employees.Count; j++) { WriteLine($"Employee {j} is a {employees[j].GetType()}"); } }
} // end Main // generically process each element in array employees public static void processEmployees(IPayable[] employees) { foreach (IPayable 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.10M; Console.WriteLine( "new base salary with 10% increase is: {0:C}", employee.BaseSalary); } // end if Console.WriteLine( "earned {0:C}\n", currentEmployee.GetPaymentAmount()); } // end foreach } //end method
static void Main(string[] args) { Console.WriteLine("Payroll...."); //Employee ee = new Employee("Bruce", "Wayne", "BAT001"); //Console.WriteLine(ee); HourlyEmployee he = new HourlyEmployee("Diana", "Ross", "WW001"); Console.WriteLine(he); CommissionEmployee ce = new CommissionEmployee("Peter", "Parker", "SPIDER001"); Console.WriteLine(ce); SalariedEmployee se = new SalariedEmployee("Bruce", "Banner", "HULK001"); Console.WriteLine(se); BasePlusCommissionEmployee bce = new BasePlusCommissionEmployee("Tony", "Stark", "IRON001"); Console.WriteLine(bce); }
public static void Main(string [] args) { // create four-element IPayable array IPayable[] payableObjects = new IPayable[6]; // populate array with objects that implement IPayable payableObjects[0] = new Invoice("01234", "seat", 2, 375.00M); payableObjects[1] = new Invoice("56789", "tire", 4, 79.95M); payableObjects[2] = new SalariedEmployee("John", "Smith", "111-11-1111", 800.00M); payableObjects[3] = new HourlyEmployee("Bob", "Bobson", "222-22-2222", 16.75M, 40M ); payableObjects[4] = new CommissionEmployee("Sally", "Seashore", "333-33-3333", 10000M, .06M ); payableObjects[5] = new BasePlusCommissionEmployee("Bob", "Lewis", "444-44-4444", 5000M, .04M, 300M, .1M); Console.WriteLine("Invoices and Employees process polymorphically:\n"); // generically process each element in array payableObjects foreach (var currentPayable in payableObjects) { // output currentPayable and its appropriate payment amount Console.WriteLine("{0}\npayment due: {1:C}\n", currentPayable, currentPayable.GetPaymentAmount()); } // end foreach }
static void Main() { Console.WriteLine("(02) Empleado por Comision + Sueldo Base\n"); var employee = new BasePlusCommissionEmployee("Bob", "Lewis", "333-33-3333", 5000.00M, .04M, 300.00M); Console.WriteLine("Informacion del Empleado obtenido por Propiedades y Metodos: \n"); Console.WriteLine("Nombre : " + employee.FirstName); Console.WriteLine("Apellido : " + employee.LastName); Console.WriteLine("Numero de Seguro Social : " + employee.SocialSecurityNumber); Console.WriteLine("Ventas Brutas : " + employee.GrossSales); Console.WriteLine("Porcentaje de Comision : " + employee.CommissionRate); Console.WriteLine("Ganancias : " + employee.Earnings()); Console.WriteLine("Salario Base : " + employee.BaseSalary); employee.BaseSalary = 1000.00M; // Se actualiza el salario base Console.WriteLine("\nInformacion actualizada obtenida por ToString:\n"); Console.WriteLine(employee); Console.WriteLine("Ganancias : " + employee.Earnings() + "\n"); }
public static void Main(string[] args) { // assign base class reference to base class variable CommissionEmployee commissionEmployee = new CommissionEmployee( "Sue", "Jones", "222-22-2222", 10000.00M, .06M); // assign derived class reference to derived class variable BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee("Bob", "Lewis", "333-33-3333", 5000.00M, .04M, 300.00M); // invoke ToString and Earnings on base class object // using base class variable Console.WriteLine("{0} {1}:\n\n{2}\n{3}: {4:C}\n", "Call CommissionEmployee's ToString and Earnings methods", "with base class reference to base class object", commissionEmployee.ToString(), "Earnings", commissionEmployee.Earnings()); // invoke ToString and Earnings on derived class object // using derived class variable Console.WriteLine("{0} {1}:\n\n{2}\n{3}: {4:C}\n", "Call BasePlusCommissionEmployee's ToString and Earnings", "methods with derived class reference to derived class object", basePlusCommissionEmployee.ToString(), "Earnings", basePlusCommissionEmployee.Earnings()); // invoke ToString and Earnings on derived class object // using base class variable CommissionEmployee commissionEmployee2 = basePlusCommissionEmployee; Console.WriteLine("{0} {1}:\n\n{2}\n{3}: {4:C}", "Call BasePlusCommissionEmployee's ToString and Earnings", "methods with base class reference to derived class object", commissionEmployee2.ToString(), "Earnings", commissionEmployee2.Earnings()); } // end Main
static void Main() { // assign base-class reference to base-class variable var commissionEmployee = new CommissionEmployee( "Sue", "Jones", "222-22-2222", 10000.00M, .06M); // assign derived-class reference to derived-class variable var basePlusCommissionEmployee = new BasePlusCommissionEmployee( "Bob", "Lewis", "333-33-3333", 5000.00M, .04M, 300.00M); // invoke ToString and Earnings on base-class object // using base-class variable Console.WriteLine( "Call CommissionEmployee's ToString and Earnings methods " + "with base-class reference to base class object\n"); Console.WriteLine(commissionEmployee.ToString()); Console.WriteLine($"earnings: {commissionEmployee.Earnings()}\n"); // invoke ToString and Earnings on derived-class object // using derived-class variable Console.WriteLine("Call BasePlusCommissionEmployee's ToString and" + " Earnings methods with derived class reference to" + " derived-class object\n"); Console.WriteLine(basePlusCommissionEmployee.ToString()); Console.WriteLine( $"earnings: {basePlusCommissionEmployee.Earnings()}\n"); // invoke ToString and Earnings on derived-class object // using base-class variable CommissionEmployee commissionEmployee2 = basePlusCommissionEmployee; Console.WriteLine( "Call BasePlusCommissionEmployee's ToString and Earnings " + "methods with base class reference to derived-class object\n"); Console.WriteLine(commissionEmployee2.ToString()); Console.WriteLine( $"earnings: {basePlusCommissionEmployee.Earnings()}\n"); }
public static void Main(string[] args) { Console.WriteLine("Payroll......"); //Employee ee = new Employee("Bruce", "Wayne", "BAT001"); //Console.WriteLine(ee.ToString()); HourlyEmployee he = new HourlyEmployee("Diane", "Carter", "WW001"); Console.WriteLine(he.ToString()); SalariedEmployee se = new SalariedEmployee("Barry", "Allen", "FLASH001"); Console.WriteLine(se.ToString()); CommissionEmployee ce = new CommissionEmployee("Peter", "Parker", "SPIDER001"); Console.WriteLine(ce.ToString()); BasePlusCommissionEmployee bce = new BasePlusCommissionEmployee("Clark", "Kent", "SUPER001"); Console.WriteLine(bce.ToString()); }
//load xml file into an ObservableCollection private void InitEmployees(ObservableCollection <IPayable> EmployeeList, string path) { XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreWhitespace = true; settings.IgnoreComments = true; XmlReader xmlIn = XmlReader.Create(path, settings); if (xmlIn.ReadToDescendant("Employee")) { // xmlIn.ReadToDescendant("Employee"); do { string id = xmlIn.GetAttribute("id"); if (id.Equals("salaried")) { xmlIn.ReadStartElement("Employee"); string ssn = xmlIn.ReadElementContentAsString(); string first = xmlIn.ReadElementContentAsString(); string last = xmlIn.ReadElementContentAsString(); decimal salary = xmlIn.ReadElementContentAsDecimal(); Employee salaryEmp = new SalariedEmployee(first, last, ssn, salary); EmployeeList.Add(salaryEmp); } else if (id.Equals("commission")) { xmlIn.ReadStartElement("Employee"); string ssn = xmlIn.ReadElementContentAsString(); string first = xmlIn.ReadElementContentAsString(); string last = xmlIn.ReadElementContentAsString(); decimal sales = xmlIn.ReadElementContentAsDecimal(); decimal rate = xmlIn.ReadElementContentAsDecimal(); Employee salaryEmp = new CommissionEmployee(first, last, ssn, sales, rate); EmployeeList.Add(salaryEmp); } else if (id.Equals("hourly")) { xmlIn.ReadStartElement("Employee"); string ssn = xmlIn.ReadElementContentAsString(); string first = xmlIn.ReadElementContentAsString(); string last = xmlIn.ReadElementContentAsString(); decimal hourlyWage = xmlIn.ReadElementContentAsDecimal(); decimal hoursWorked = xmlIn.ReadElementContentAsDecimal(); Employee salaryEmp = new HourlyEmployee(first, last, ssn, hourlyWage, hoursWorked); EmployeeList.Add(salaryEmp); } else if (id.Equals("base")) { xmlIn.ReadStartElement("Employee"); string ssn = xmlIn.ReadElementContentAsString(); string first = xmlIn.ReadElementContentAsString(); string last = xmlIn.ReadElementContentAsString(); decimal sales = xmlIn.ReadElementContentAsDecimal(); decimal rate = xmlIn.ReadElementContentAsDecimal(); decimal salary = xmlIn.ReadElementContentAsDecimal(); Employee salaryEmp = new BasePlusCommissionEmployee(first, last, ssn, sales, rate, salary); EmployeeList.Add(salaryEmp); } }while (xmlIn.ReadToNextSibling("Employee")); } xmlIn.Close(); }
public static void Main(string[] args) { // create derived class objects 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); HourlyShiftEmployee hourlyShiftEmployee1 = new HourlyShiftEmployee("Nick", "Schuler", "111-22-3333", 10.00m, 30, 2); HourlyShiftEmployee hourlyShiftEmployee2 = new HourlyShiftEmployee("George", "Of The Jungle", "000-00-0000", 20.00m, 40, 3); 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()); Console.WriteLine("{0}\nEarned: {1:C}\n", hourlyShiftEmployee1, hourlyShiftEmployee1.Earnings()); Console.WriteLine("{0}\nEarned: {1:C}\n", hourlyShiftEmployee2, hourlyShiftEmployee2.Earnings()); // create four-element Employee array Employee[] employees = new Employee[6]; // initialize array with Employees of derived types employees[0] = salariedEmployee; employees[1] = hourlyEmployee; employees[2] = commissionEmployee; employees[3] = basePlusCommissionEmployee; employees [4] = hourlyShiftEmployee1; employees [5] = hourlyShiftEmployee2; Console.WriteLine("Employees processed polymorphically:\n"); // generically process each element in array employees foreach (Employee 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.10M; Console.WriteLine( "new base salary with 10% increase is: {0:C}", employee.BaseSalary); } // end if Console.WriteLine( "earned {0:C}\n", currentEmployee.Earnings()); } // end foreach // 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()); } } // end Main
public 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.GetPaymentAmount()); Console.WriteLine("{0}\nearned: {1:C}\n", hourlyEmployee, hourlyEmployee.GetPaymentAmount()); Console.WriteLine("{0}\nearned: {1:C}\n", commissionEmployee, commissionEmployee.GetPaymentAmount()); Console.WriteLine("{0}\nearned: {1:C}\n", basePlusCommissionEmployee, basePlusCommissionEmployee.GetPaymentAmount()); // create four-element Employee array Employee[] employees = new Employee[4]; // initialize array with Employees of derived types employees[0] = salariedEmployee; employees[1] = hourlyEmployee; employees[2] = commissionEmployee; employees[3] = basePlusCommissionEmployee; Console.WriteLine("Employees processed polymorphically:\n"); // generically process each element in array employees foreach (Employee 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.10M; Console.WriteLine( "new base salary with 10% increase is: {0:C}", employee.BaseSalary); } // end if Console.WriteLine( "earned {0:C}\n", currentEmployee.GetPaymentAmount()); } // end foreach //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()); } Console.WriteLine("-------------------------------------------------"); // create derived class objects IPayable[] payableObjects = new IPayable[8]; payableObjects[0] = new SalariedEmployee("John", "Smith", "111-11-1111", 700M); payableObjects[1] = new SalariedEmployee("Antonio", "Smith", "555-55-5555", 800M); payableObjects[2] = new SalariedEmployee("Victor", "Smith", "444-44-4444", 600M); payableObjects[3] = new HourlyEmployee("Karen", "Price", "222-22-2222", 16.75M, 40M); payableObjects[4] = new HourlyEmployee("Ruben", "Zamora", "666-66-6666", 20.00M, 40M); payableObjects[5] = new CommissionEmployee("Sue", "Jones", "333-33-3333", 10000M, .06M); payableObjects[6] = new BasePlusCommissionEmployee("Bob", "Lewis", "777-77-7777", 5000M, .04M, 300M); payableObjects[7] = new BasePlusCommissionEmployee("Lee", "Duarte", "888-88-888", 5000M, .04M, 300M); SortSSNDelegate sortDelegate = new SortSSNDelegate(Employee.CompareSSN); //delegate points to method. bool key = true; while (key) { Console.WriteLine(Environment.NewLine + "1.Sort last name in ascending order using IComparable"); Console.WriteLine("2.Sort pay amount in descending order using IComparer"); Console.WriteLine("3.Sort by social security number in asc order using selection sort & DELEGATE"); Console.WriteLine("Press 4 anytime to Exit." + Environment.NewLine); int value = Int32.Parse(Console.ReadLine()); switch (value) { case 1: Console.WriteLine("<Sorting Last Name using IComparABLE>"); Array.Sort(payableObjects); PrintArrayElements(payableObjects); break; case 2: Console.WriteLine("\n\n<Sort payment amount in desc using ICompaRER>"); Array.Sort(payableObjects, new PaymentAmountDesc()); PrintArrayElements(payableObjects); break; case 3: Console.WriteLine("\n\n<Sort based on SSN in Ascending order>"); SelectionSortMethod(payableObjects as object, sortDelegate, true); // Call method. 'true' for ascending. PrintArrayElements(payableObjects); break; default: key = false; break; } } // end Main }
public static void Main(string[] args) { // create derived class objects 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()); // create four-element Employee array Employee[] employees = new Employee[4]; // initialize array with Employees of derived types employees[0] = salariedEmployee; employees[1] = hourlyEmployee; employees[2] = commissionEmployee; employees[3] = basePlusCommissionEmployee; Console.WriteLine("Employees processed polymorphically:\n"); // generically process each element in array employees processEmployees(employees); // 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()); Console.WriteLine("\nEMPLOYEES PROCESSED BY IPAYABLE\n"); //Create IPayable array IPayable[] payableObjects = new IPayable[8]; payableObjects[0] = new SalariedEmployee( "John", "Smith", "111-11-1111", 700M); payableObjects[1] = new SalariedEmployee( "Antonio", "Smith", "555-55-5555", 800M); payableObjects[2] = new SalariedEmployee( "Victor", "Smith", "444-44-4444", 600M); payableObjects[3] = new HourlyEmployee( "Karen", "Price", "222-22-2222", 16.75M, 40M); payableObjects[4] = new HourlyEmployee( "Ruben", "Zamora", "666-66-6666", 20.00M, 40M); payableObjects[5] = new CommissionEmployee( "Sue", "Jones", "333-33-3333", 10000M, .06M); payableObjects[6] = new BasePlusCommissionEmployee( "Bob", "Lewis", "777-77-7777", 5000M, .04M, 300M); payableObjects[7] = new BasePlusCommissionEmployee( "Lee", "Duarte", "888-88-888", 5000M, .04M, 300M); // generically process each element in array employees processEmployees(payableObjects); //bubble sort and pointer Console.WriteLine("\nIPAYABLES SORTED BY SSN:\n"); BubbleSort(payableObjects, SSNAscending); foreach (Employee currentEmployee in payableObjects) { Console.WriteLine(currentEmployee); // invokes ToString Console.WriteLine( "earned {0:C}\n", currentEmployee.Earnings()); } // end foreach //IComparer Console.WriteLine("\nIPAYABLES SORTED BY LAST NAME ASCENDING\n"); ArrayList comparerSample = new ArrayList(); //populate ArrayList for IComparer and IComprable foreach (IPayable currentPayable in payableObjects) { comparerSample.Add(currentPayable); }//end foreach IComparer lastAscend = new SortLastNAscending(); comparerSample.Sort(lastAscend); foreach (Employee currentEmployee in comparerSample) { Console.WriteLine(currentEmployee); // invokes ToString Console.WriteLine( "earned {0:C}\n", currentEmployee.Earnings()); } // end foreach //IComparable implementation Console.WriteLine("\nIPAYABLES SORTED BY SALARY DESCEDNING\n"); comparerSample.Sort(); foreach (Employee currentEmployee in comparerSample) { Console.WriteLine(currentEmployee); // invokes ToString Console.WriteLine( "earned {0:C}\n", currentEmployee.Earnings()); } // end foreach }
public static void Main(string[] args) { IPayable[] payableObjects = new IPayable[8]; payableObjects[0] = new SalariedEmployee("John", "Smith", "111-11-1111", 700M); payableObjects[1] = new SalariedEmployee("Antonio", "Smith", "555-55-5555", 800M); payableObjects[2] = new SalariedEmployee("Victor", "Smith", "444-44-4444", 600M); payableObjects[3] = new HourlyEmployee("Karen", "Price", "222-22-2222", 16.75M, 40M); payableObjects[4] = new HourlyEmployee("Ruben", "Zamora", "666-66-6666", 20.00M, 40M); payableObjects[5] = new CommissionEmployee("Sue", "Jones", "333-33-3333", 10000M, .06M); payableObjects[6] = new BasePlusCommissionEmployee("Bob", "Lewis", "777-77-7777", 5000M, .04M, 300M); payableObjects[7] = new BasePlusCommissionEmployee("Lee", "Duarte", "888-88-888", 5000M, .04M, 300M); /* Console.WriteLine("Not sorted in any way yet \n"); for (int j = 0; j < payableObjects.Length; j++) { Console.WriteLine("Employee new {0} is a {1}", j, payableObjects[j]); } */ foreach (IPayable p in payableObjects) { Console.WriteLine(p + "\n"); } string menuOption; bool menuLogic = true; while (menuLogic) { Console.WriteLine("1: Sort last name in ascending order using IComparable"); Console.WriteLine("2: Sort pay amount in descending order using IComparer"); Console.WriteLine("3: Sort by social security number in ascending order using a selection sort and delegate"); Console.WriteLine("4: Exit"); menuOption = Console.ReadLine(); switch (menuOption) { case "1": Array.Sort(payableObjects); foreach (IPayable p in payableObjects) { Console.WriteLine(p + "\n"); } Console.WriteLine(); break; case "2": Array.Sort(payableObjects, Employee.payAmountSorter()); foreach (IPayable p in payableObjects) { Console.WriteLine(p + "\n"); } Console.WriteLine(); break; case "3": PayrollSystemTest payRoll = new PayrollSystemTest(); ComparerSSN ssnSort = new ComparerSSN(Employee.SortAsSSN); payRoll.SelectionSort(payableObjects, ssnSort); break; case "4": Console.WriteLine("Option 4 "); menuLogic = false; break; } } Console.ReadKey(); }
static void Main() { // create derived-class objects var salariedEmployee = new SalariedEmployee("John", "Smith", "111-11-1111", 800.00M); var hourlyEmployee = new HourlyEmployee("Karen", "Price", "222-22-2222", 16.75M, 40.0M); var commissionEmployee = new CommissionEmployee("Sue", "Jones", "333-33-3333", 10000.00M, .06M); var basePlusCommissionEmployee = new BasePlusCommissionEmployee("Bob", "Lewis", "444-44-4444", 5000.00M, .04M, 300.00M); Console.WriteLine("Employees 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 }; Console.WriteLine("Employees processed polymorphically:\n"); // generically process each element in 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 var employee = (BasePlusCommissionEmployee)currentEmployee; employee.BaseSalary *= 1.10M; Console.WriteLine("new base salary with 10% increase is: " + $"{employee.BaseSalary:C}"); } Console.WriteLine($"earned: {currentEmployee.Earnings():C}\n"); } // get type name of each object in employees for (int j = 0; j < employees.Count; j++) { Console.WriteLine( $"Employee {j} is a {employees[j].GetType()}"); } Console.WriteLine("-----------------Beginnig of Question 3 answer"); Console.WriteLine("-----------------by Jovane Marques - 300982100"); var employeesNewSalary = employees.Select(e => new { FullName = e.FirstName + " " + e.LastName, Salary = e is BasePlusCommissionEmployee ? ((e as BasePlusCommissionEmployee).BaseSalary * Decimal.Parse("1.1")) : e.Earnings() }); foreach (var emp in employeesNewSalary) { Console.WriteLine("Employee: " + emp.FullName + ", Salary = " + emp.Salary); } Console.WriteLine("-----------------End of Question 3 answer"); Console.ReadKey(); }
static void Main() { var date1 = new Date(1, 1, 1989); var birthday = new Date(2, 9, 1989); var salariedEmployee = new SalariedEmployee("John", "Smith", "111-11-1111", 800.00m, birthday); var hourlyEmployee = new HourlyEmployee("Karen", "Price", "222-22-2222", 16.75m, 40.0m, birthday); var commissionEmployee = new CommissionEmployee("Sue", "Jones", "333-33-3333", 10000.00m, .06m, birthday); var basePlusCommissionEmployee = new BasePlusCommissionEmployee("Bob", "Lewis", "444-44-4444", 5000.00m, .04m, 300.00m, birthday); var pieceWorker2 = new PieceWorker("Brian", "Briansen", "555-55-5555", .40m, 1000, birthday); birthday = new Date(4, 9, 1989); var pieceWorker = new PieceWorker("Brian", "Briansen", "555-55-5555", .40m, 1000, birthday); Console.WriteLine("Employees 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"); Console.WriteLine($"{pieceWorker}\nearned: " + $"{pieceWorker.Earnings():C}"); var employees = new List <Employee>() { salariedEmployee, hourlyEmployee, commissionEmployee, basePlusCommissionEmployee, pieceWorker2, pieceWorker }; Console.WriteLine("\nEmployees processed polymorphically: \n"); for (int month = 1; month < 12; month++) { foreach (var currentEmployee in employees) { Console.WriteLine(currentEmployee); if (currentEmployee is BasePlusCommissionEmployee) { var employee = (BasePlusCommissionEmployee)currentEmployee; employee.BaseSalary *= 1.10m; Console.WriteLine("new base salary with 10% increase is: " + $"{employee.BaseSalary:C}"); } if (month == currentEmployee.Birthday.Month) { Console.WriteLine("Birthday Bonus: 100.00"); Console.WriteLine($"earned {currentEmployee.Earnings() + 100:C}\n"); } else { Console.WriteLine($"earned {currentEmployee.Earnings():C}\n"); } } } for (int j = 0; j < employees.Count; j++) { Console.WriteLine($"Employee {j} is a {employees[j].GetType()}"); } Console.ReadLine(); }
public static void Main(string[] args) { // create derived class objects 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()); // create four-element Employee array Employee[] employees = new Employee[4]; // initialize array with Employees of derived types employees[0] = salariedEmployee; employees[1] = hourlyEmployee; employees[2] = commissionEmployee; employees[3] = basePlusCommissionEmployee; Console.WriteLine("Employees processed polymorphically:\n"); // generically process each element in array employees processEmployees(employees); // 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()); } Console.WriteLine("\nEMPLOYEES PROCESSED BY IPAYABLE\n"); //Create IPayable array IPayable[] payableObjects = new IPayable[8]; payableObjects[0] = new SalariedEmployee( "John", "Smith", "111-11-1111", 700M); payableObjects[1] = new SalariedEmployee( "Antonio", "Smith", "555-55-5555", 800M); payableObjects[2] = new SalariedEmployee( "Victor", "Smith", "444-44-4444", 600M); payableObjects[3] = new HourlyEmployee( "Karen", "Price", "222-22-2222", 16.75M, 40M); payableObjects[4] = new HourlyEmployee( "Ruben", "Zamora", "666-66-6666", 20.00M, 40M); payableObjects[5] = new CommissionEmployee( "Sue", "Jones", "333-33-3333", 10000M, .06M); payableObjects[6] = new BasePlusCommissionEmployee( "Bob", "Lewis", "777-77-7777", 5000M, .04M, 300M); payableObjects[7] = new BasePlusCommissionEmployee( "Lee", "Duarte", "888-88-888", 5000M, .04M, 300M); // generically process each element in array employees processEmployees(payableObjects); //bubble sort and pointer Console.WriteLine("\nIPAYABLES SORTED BY SSN:\n"); BubbleSort(payableObjects, SSNAscending); foreach (Employee currentEmployee in payableObjects) { Console.WriteLine(currentEmployee); // invokes ToString Console.WriteLine( "earned {0:C}\n", currentEmployee.Earnings()); } // end foreach //IComparer Console.WriteLine("\nIPAYABLES SORTED BY LAST NAME ASCENDING\n"); ArrayList comparerSample = new ArrayList(); //populate ArrayList for IComparer and IComprable foreach (IPayable currentPayable in payableObjects) { comparerSample.Add(currentPayable); }//end foreach IComparer lastAscend = new SortLastNAscending(); comparerSample.Sort(lastAscend); foreach (Employee currentEmployee in comparerSample) { Console.WriteLine(currentEmployee); // invokes ToString Console.WriteLine( "earned {0:C}\n", currentEmployee.Earnings()); } // end foreach //IComparable implementation Console.WriteLine("\nIPAYABLES SORTED BY SALARY DESCEDNING\n"); comparerSample.Sort(); foreach (Employee currentEmployee in comparerSample) { Console.WriteLine(currentEmployee); // invokes ToString Console.WriteLine( "earned {0:C}\n", currentEmployee.Earnings()); } // end foreach } // end Main