static void Main(string[] args) { SalaryEmployee salaryEmployee = new SalaryEmployee("Ringo", "DeathStarr", "IT", 15000m); Console.WriteLine(salaryEmployee.ToString()); Console.WriteLine(salaryEmployee.CalculateWeeklyGross()); Console.WriteLine(salaryEmployee.GetDepartmentTwice()); Console.WriteLine("*********************"); HourlyEmployee hourlyEmployee = new HourlyEmployee("Funny", "Trips", "IT", 8.37m); hourlyEmployee.HoursWorked = 40; Console.WriteLine(hourlyEmployee.ToString()); Console.WriteLine(hourlyEmployee.CalculateWeeklyGross()); Console.WriteLine(hourlyEmployee.GetDepartmentTwice()); Console.WriteLine("*************NEW SHIT***************"); HourlyEmployee joe = new HourlyEmployee("Joe", "Dirtbag", "HR", 9.37m); joe.HoursWorked = 87; SalaryEmployee dick = new SalaryEmployee("Dick", "Head", "PR", 15000m); Employee ronald = new SalaryEmployee("Mrs.", "Ronfonbob", "IT", 42369m); IEmployee newPerson = new HourlyEmployee("Some", "OneElse", "WK", 12.43m); //downcast from type iemployee to type hourly employee HourlyEmployee myStack; myStack = (HourlyEmployee)newPerson; dick.CompareTo(joe); IEmployee[] employeeList = { joe, dick, new SalaryEmployee("Ben", "Dover", "ST", 99075m), ronald }; foreach(IEmployee employee in employeeList) { Console.WriteLine(employee.ToString()); Console.WriteLine(employee.CalculateWeeklyGross()); Console.WriteLine(); } Console.ReadKey(); }
static void Main(string[] args) { SalaryEmployee salaryEmployee = new SalaryEmployee("David", "Barnes", "IT", 150000); Console.WriteLine(salaryEmployee.ToString()); Console.WriteLine(salaryEmployee.CalculateWeeklyGross()); HourlyEmployee hourlyEmployee = new HourlyEmployee("Joe", "Somebody", "HR", 8.37M); // Decimalic literals inheriantly assume double, so use M to assume decimal (money) hourlyEmployee.HoursWorked = 40; Console.WriteLine(hourlyEmployee.ToString()); Console.WriteLine(hourlyEmployee.CalculateWeeklyGross()); }
static void Main(string[] args) { SalaryEmployee salaryEmployee = new SalaryEmployee("Trash", "Boat", "Park Employee", 5); Console.WriteLine(salaryEmployee.ToString()); Console.WriteLine(salaryEmployee.CalculateWeeklyGross()); Console.WriteLine(salaryEmployee.GetDepartmentTwice()); Console.WriteLine("*********************************************************************"); HourlyEmployee hourlyEmployee = new HourlyEmployee("Peter", "Parker", "Avenger", 8.37m); hourlyEmployee.HoursWorked = 40; Console.WriteLine(hourlyEmployee.ToString()); Console.WriteLine((hourlyEmployee.CalculateWeeklyGross()).ToString("C")); Console.WriteLine(hourlyEmployee.GetDepartmentTwice()); Console.WriteLine("**********************************************************************"); HourlyEmployee joe = new HourlyEmployee("Joeseph", "Joestar", "Stand User", 250m); joe.HoursWorked = 87; SalaryEmployee dave = new SalaryEmployee("Dave", "Barnes", "IT", 150000m); Employee spiff = new SalaryEmployee("Spiff", "Lightspace", "Professional Space Guy", 0m); IEmployee newPerson = new HourlyEmployee("Silent", "Hill", "City", 100000.00m); HourlyEmployee myStack; myStack = (HourlyEmployee)newPerson; //Create an array of type IEmployee, and pass in the two instances we created. //IEmployee can contain instances that are lower on the inheritance chain. //(More specific implementations) IEmployee[] employeeList = { joe, dave, new SalaryEmployee("Jack", "Ramen", "Pit Lord", 750000m), spiff }; //Now we can loop through the list of employees and call teh CalculateWeeklyGross //On each of the employees. The porgram will be smart enough to know which method to call depending on the current instance it is working with. foreach(IEmployee employee in employeeList) { Console.WriteLine(employee.ToString()); Console.WriteLine(employee.CalculateWeeklyGross()); Console.WriteLine(); } }
static void Main(string[] args) { SalaryEmployee salaryEmployee = new SalaryEmployee("David", "Barnes", "IT", 150000); Console.WriteLine(salaryEmployee.ToString()); Console.WriteLine(salaryEmployee.CalculateWeeklyGross()); Console.WriteLine(salaryEmployee.GetDepartmentTwice()); Console.WriteLine("**********************************************************"); HourlyEmployee hourlyEmployee = new HourlyEmployee("Joe", "Somebody", "HR", 8.37m); hourlyEmployee.HoursWorked = 40; Console.WriteLine(hourlyEmployee.ToString()); Console.WriteLine(hourlyEmployee.CalculateWeeklyGross()); Console.WriteLine(hourlyEmployee.GetDepartmentTwice()); Console.WriteLine("**********************************************************"); //declare and instantiate one of each type of employee HourlyEmployee joe = new HourlyEmployee("Joe", "Somebody", "HR", 9.37m); joe.HoursWorked = 87; SalaryEmployee dave = new SalaryEmployee("Dave", "Barnes", "IT", 150000m); Employee spock = new SalaryEmployee("Mr.", "Spock", "ST", 0m); //Create an array of type IEmployee, and pass in the two instances we created. IEmployee can contain instances that are lower on the inheritance chain (more specific implementations). IEmployee[] employeeList = { joe, dave, new SalaryEmployee("James", "Kirk", "ST", 0m), spock }; //Now we can loop through the list of employees and call the CalculateWeeklyGross on each of the employees. The program will be smart enough to know which method to call depending on the current instance it is working with. foreach(IEmployee employee in employeeList) { Console.WriteLine(employee.ToString()); Console.WriteLine(employee.CalculateWeeklyGross()); Console.WriteLine(); } }
static void Main(string[] args) { SalaryEmployee salaryEmployee = new SalaryEmployee("David", "Barnes", "IT", 150000); Console.WriteLine(salaryEmployee.ToString()); Console.WriteLine(salaryEmployee.CalculateWeeklyGross()); Console.WriteLine(salaryEmployee.GetDepartmentTwice()); Console.WriteLine("\n"); Console.WriteLine("*****************************"); Console.WriteLine("\n"); HourlyEmployee hourlyEmployee = new HourlyEmployee("Joe", "Somebody", "HR", 8.37m); hourlyEmployee.HoursWorked = 40; Console.WriteLine(hourlyEmployee.ToString()); Console.WriteLine(hourlyEmployee.CalculateWeeklyGross()); // can't make instance of abstract class // Employee employee = new Employee("D", "B", "d"); Console.WriteLine(hourlyEmployee.GetDepartmentTwice()); Console.WriteLine("**************"); //Declare and instanciate one of each type of employee HourlyEmployee joe = new HourlyEmployee("Joe", "Somebody", "HR", 9.37m); joe.HourlyRate = 87; SalaryEmployee dave = new SalaryEmployee("Dave", "Barnes", "IT", 150000m); Employee spock = new SalaryEmployee("Mr.", "Spock", "ST", 0m); IEmployee newPerson = new HourlyEmployee("Some", "OneElse", "WK", 12.34m); ((HourlyEmployee)newPerson).HoursWorked = 21; //Create an array of type IEmployee, and pass in the two instances we created. //IEmployee can contain instances that are lower on the inheritance chain. //(More specific imploementations) //Add another on the fly. No need to assign it to a seperate variable first. IEmployee[] employeeList = { joe, dave, new SalaryEmployee("James","Kirk", "ST", 0m), spock }; //Now we can loop through the list of employees and call the CalculateWeeklyGross //On each of the employees. The program will be smart enough to know which //method to call depending on the current instance it is working with. foreach (IEmployee employee in employeeList) { Console.WriteLine(employee.ToString()); Console.WriteLine(employee.CalculateWeeklyGross()); Console.WriteLine(); } IEmployee[] anotherEmployeeList; anotherEmployeeList = new IEmployee[20]; anotherEmployeeList[0] = joe; anotherEmployeeList[1] = dave; foreach (IEmployee employee in anotherEmployeeList) { Console.WriteLine(employee.ToString()); Console.WriteLine(employee.CalculateWeeklyGross()); Console.WriteLine(); } }
static void Main(string[] args) { SalaryEmployee salaryEmployee = new SalaryEmployee("David", "Barnes", "IT", 150000); Console.WriteLine(salaryEmployee.ToString()); Console.WriteLine(salaryEmployee.CalculateWeeklyGross()); Console.WriteLine(salaryEmployee.GetDepartmentTwice()); Console.WriteLine("**************************************"); HourlyEmployee hourlyEmployee = new HourlyEmployee("Joe", "Somebody", "HR", 8.37m); hourlyEmployee.HoursWorked = 40; Console.WriteLine(hourlyEmployee.ToString()); Console.WriteLine(hourlyEmployee.CalculateWeeklyGross()); Console.WriteLine(hourlyEmployee.GetDepartmentTwice()); Console.WriteLine("*************************************"); //Declare and instanciate one of each type of employee HourlyEmployee joe = new HourlyEmployee("Joe", "Somebody", "HR", 9.37m); joe.HoursWorked = 87; SalaryEmployee dave = new SalaryEmployee("Dave", "Barnes", "IT", 150000m); Employee spock = new SalaryEmployee("Mr.", "Spock", "ST", 0m); IEmployee newPerson = new HourlyEmployee("Some", "OneElse", "WK", 12.34m); ((HourlyEmployee)newPerson).HoursWorked = 21; //Create an array of type IEmployee, and pass in the two instances we created. //IEmployee can contain instances that are lower on the inheritance chain. //(More specific implementations) //Add another on the fly. No need to assign it to a seperate variable first. IEmployee[] employeeList = { joe, dave, new SalaryEmployee("James", "Kirk", "ST", 0m), spock }; //Now we can loop through the list of employees and call the CalculateWeeklyGross //On each of the employees. The program will be smart enough to know which //method to call depending on the current instance it is working with. foreach(IEmployee employee in employeeList) { Console.WriteLine(employee.ToString()); Console.WriteLine(employee.CalculateWeeklyGross()); Console.WriteLine(); } Console.WriteLine("************************************"); IEmployee[] anotherEmployeeList; anotherEmployeeList = new IEmployee[20]; anotherEmployeeList[0] = joe; anotherEmployeeList[1] = dave; foreach (IEmployee employee in anotherEmployeeList) { if (employee != null) { Console.WriteLine(employee.ToString()); Console.WriteLine(employee.CalculateWeeklyGross()); Console.WriteLine(); } } }
/// <summary> /// Main. /// </summary> /// <param name="args">Main-Line args.</param> static void Main(string[] args) { #region Intial Inheritance Work Console.WriteLine(); Console.WriteLine("".PadRight(20, '*')); Console.WriteLine("Initial Inheritance Work"); Console.WriteLine("".PadRight(20, '*')); // Creates Salary Employee (inheriting from the abstract class and interface) and displays information. SalaryEmployee salaryEmployee = new SalaryEmployee("Aa", "Aaa", "IT", 150000); Console.WriteLine(salaryEmployee.ToString()); Console.WriteLine(salaryEmployee.CalculateWeeklyGross().ToString("C")); Console.WriteLine("Department: " + salaryEmployee.GetDepartmentTwice()); Console.WriteLine(Environment.NewLine + "".PadLeft(15, '~') + Environment.NewLine); // Creates Hourly Employee (inheriting from the abstract class and interface) and displays information. HourlyEmployee hourlyEmployee = new HourlyEmployee("Bb", "Bbb", "HR", 8.37m); hourlyEmployee.HoursWorked = 40; Console.WriteLine(hourlyEmployee.ToString()); Console.WriteLine(hourlyEmployee.CalculateWeeklyGross().ToString("C")); Console.WriteLine("Department: " + hourlyEmployee.GetDepartmentTwice()); Console.WriteLine(); Console.WriteLine("".PadRight(20, '*')); Console.WriteLine("End Inheritance Work"); Console.WriteLine("".PadRight(20, '*')); #endregion #region Polymorphism Work Console.WriteLine(); Console.WriteLine("".PadRight(20, '*')); Console.WriteLine("Polymorphism Work"); Console.WriteLine("".PadRight(20, '*')); // Declares instances of employees. SalaryEmployee aa = new SalaryEmployee("Aa", "Aaa", "IT", 150000); HourlyEmployee bb = new HourlyEmployee("Bb", "Bbb", "HR", 8.37m); HourlyEmployee cc = new HourlyEmployee("Cc", "Ccc", "No", 50m); cc.HoursWorked = 40; // Puts all created employees into a list. Because they are of different types, // must use polymorphism to create a compatible container. IEmployee[] employeeList = { aa, bb, cc, new SalaryEmployee("Dd", "ddd", "Yus", 500), new HourlyEmployee("Ee", "Eee", "Uh", 12m) }; IEmployee Ee = new HourlyEmployee("Ee", "Eee", "Uh", 12m); ((HourlyEmployee)employeeList[4]).HoursWorked = 40; ((HourlyEmployee)Ee).HoursWorked = 40; /* Loops through list to call CalculateWeeklyGross on each employee. Due to properties of polymorphism, * program knows which version of each method to call based on theactual employee instance version. This * only works if the methods are a part of the top classand then is overridden/inherited by the lower classes. * Anything which is only in the lowerclasses CANNOT be called/used/changed via the polymorphized top class. * To do this you must cast class first, as in two lines of code directly above. * NOTE: By casting like the above two lines of code, you have potential to crash your program if done incorrectly. * */ foreach (IEmployee employee in employeeList) { Console.WriteLine(employee.ToString()); Console.WriteLine(employee.CalculateWeeklyGross().ToString("C")); Console.WriteLine(); } // You can also simply create an array of the interface/abstract class and then add lower level objects afterwards. Console.WriteLine(Environment.NewLine + "".PadRight(15, '~') + Environment.NewLine); IEmployee[] anotherEmployeeList; anotherEmployeeList = new IEmployee[5]; anotherEmployeeList[0] = aa; anotherEmployeeList[1] = bb; anotherEmployeeList[2] = cc; foreach (IEmployee employee in anotherEmployeeList) { if (employee != null) { Console.WriteLine(employee.ToString()); Console.WriteLine(employee.CalculateWeeklyGross().ToString("C")); Console.WriteLine(); } } #endregion }