public void EmployeeTests() { Employee jarvis = new Employee(); HourlyEmployee tony = new HourlyEmployee(); SalaryEmployee pepper = new SalaryEmployee(); tony.HoursWorked = 55; tony.HourlyWage = 9003; pepper.Salary = 20000; List <Employee> allEmployees = new List <Employee>(); allEmployees.Add(jarvis); allEmployees.Add(tony); allEmployees.Add(pepper); //tony.name = "tony stark"; tony.SetFirstName("Tony"); tony.SetLastName("Stark"); foreach (Employee worker in allEmployees) { if (worker.GetType() == typeof(SalaryEmployee)) { SalaryEmployee sEmployee = (SalaryEmployee)worker; Console.WriteLine($"This is a salary employee that makes {sEmployee.Salary}"); } else if (worker is HourlyEmployee hourlyWorker) // Pattern Matching { //HourlyEmployee hEmployee = (HourlyEmployee)hourlyWorker; Console.WriteLine($"{worker.Name} has worked {hourlyWorker.HoursWorked} hours!"); } } }
public void EmployeeTests() { Employee jarvis = new Employee(); HourlyEmployee tony = new HourlyEmployee(); SalaryEmployee pepper = new SalaryEmployee(); tony.HoursWorked = 55; tony.HourlyWage = 9003; pepper.Salary = 200000; List <Employee> allEmployees = new List <Employee>(); allEmployees.Add(jarvis); //we never added any info so in the foreach loop it will be blank allEmployees.Add(tony); // we never set his name so in the foreach loop nothing will appear for worker.Name allEmployees.Add(pepper); //tony.Name = "Tony Stark"; tony.SetFirstName("Tony"); tony.SetLastName("Stark"); foreach (Employee worker in allEmployees) { if (worker.GetType() == typeof(SalaryEmployee)) { SalaryEmployee sEmployee = (SalaryEmployee)worker; //casting because worker is of type List<Employee> and not SalaryEmployee Console.WriteLine($"This is a salary employee that makes {sEmployee.Salary}"); } else if (worker is HourlyEmployee hourlyWorker) //pattern matching-- so we don't have to cast like the below { //HourlyEmployee hEmployee = (HourlyEmployee)hourlyWorker; -- casting Console.WriteLine($"{worker.Name} has worked {hourlyWorker.HoursWorked} hours!"); } } }
public void EmployeeTests() { Employee jarvis = new Employee(); // newing up new instance of the employee class named jarvis HourlyEmployee tony = new HourlyEmployee(); // new instance of the hourly employee class named tony SalaryEmployee pepper = new SalaryEmployee(); //new instance of salary employee class named pepper tony.HoursWorked = 55; tony.HourlyWage = 9003; pepper.Salary = 200000; List <Employee> allEmployees = new List <Employee>(); allEmployees.Add(jarvis); allEmployees.Add(tony); allEmployees.Add(pepper); //adds all these employess to the list of employees //tony.Name = "Tony Stark" tony.SetFirstName("Tony"); // sets the property of the first name and the field _firstName tony.SetLastName("Stark"); // sets the property of the last name and the field _LastName foreach (Employee worker in allEmployees) //iterates through the list loops through this list and reassigns the variable worker each time the loop reaches the start of the list the list so first time through it is assigned the variable of jarvis, second time through the list it is assigned the values of tony, last time through the list it is assigned the value of pepper. Once it has run through the all the employees in the allemployee list the program stops. { if (worker.GetType() == typeof(SalaryEmployee)) // if worker is a salaryEmployee //will run through jarvis, tony, and pepper because they are all within the list of allemployees { SalaryEmployee sEmployee = (SalaryEmployee)worker; Console.WriteLine($"this is a salary employee that makes {sEmployee.Salary}"); // ? } else if (worker is HourlyEmployee hourlyWorker) // pattern matching it is taking worker turning into hourly worker then creating new variable within the if statement { //HourlyEmployee hEmployee = (HourlyEmployee)hourlyWorker; Console.WriteLine($"{worker.Name} has worked {hourlyWorker.HoursWorked} hours!"); } } }
public void EmployeeTests() { Employee jarvis = new Employee(); HourlyEmployee tony = new HourlyEmployee(); SalaryEmployee pepper = new SalaryEmployee(); tony.HoursWorked = 55; tony.HourlyWage = 9003; pepper.Salary = 2000000; // Since all above types inherit from type Employee, they can be treated as Employee and added to the List of Employees. However, we can only access List <Employee> allEmployees = new List <Employee>(); allEmployees.Add(jarvis); allEmployees.Add(tony); allEmployees.Add(pepper); // because of the set methods, I can't assign a string directly to Name //tony.Name = "Tony Stark"; tony.SetFirstName("Tony"); tony.SetLastName("Stark"); foreach (Employee worker in allEmployees) { if (worker.GetType() == typeof(SalaryEmployee)) { // cast the employees in the Employee List as SalaryEmployee SalaryEmployee sEmployee = (SalaryEmployee)worker; // this won't work because Employee List is only Employee (no Salary exists) // Console.WriteLine($"This is a salary employee that makes {worker.Salary}"); Console.WriteLine($"This is a salary employee that makes {sEmployee.Salary}"); } // Pattern Matching // turn worker into Hourly worker and create a new variable in the if statement. // hourlyWorker is HourlyEmployee type else if (worker is HourlyEmployee hourlyWorker) // pattern matching { // HourlyEmployee hEmployee = (HourlyEmployee)hourlyWorker; // Console.WriteLine($"{worker.Name} has worked {hEmployee.HoursWorked} hours!"); Console.WriteLine($"{worker.Name} has worked {hourlyWorker.HoursWorked} hours!"); } } }
public void EmployeeTests() { Employee javis = new Employee(); HourlyEmployee tony = new HourlyEmployee(); SalaryEmployee pepper = new SalaryEmployee(); tony.HoursWorked = 55; tony.HourlyWage = 9003; pepper.Salary = 200000; List <Employee> allEmployees = new List <Employee>(); allEmployees.Add(javis); allEmployees.Add(tony); allEmployees.Add(pepper); tony.SetFirstName("Tristan"); tony.SetLastName("Oshier"); Console.WriteLine(tony.Name); foreach (Employee worker in allEmployees) { if (worker.GetType() == typeof(SalaryEmployee)) { //Casts worker into a Salary Employee to give access to Salary SalaryEmployee sEmployee = (SalaryEmployee)worker; Console.WriteLine($"This is a salary employee that makes {sEmployee.Salary} a year"); } //This is the same things as the if above, this casts the worker to a HourlyEmployee for you else if (worker is HourlyEmployee hourlyWorker) // pattern matching { //HourlyEmployee hEmployee = (HourlyEmployee)hourlyWorker; Console.WriteLine($"{worker.Name} has worked {hourlyWorker.HoursWorked} hours!"); } } }