コード例 #1
0
        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!");
                }
            }
        }
コード例 #2
0
        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!");
                }
            }
        }
コード例 #3
0
ファイル: PersonTests.cs プロジェクト: sevcox/CSharpLibrary
        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!");
                }
            }
        }
コード例 #4
0
        public void EmployeeTests()
        {
            Employee       jarvis = new Employee();
            HourlyEmployee tony   = new HourlyEmployee();
            SalaryEmployee pepper = new SalaryEmployee();

            jarvis.SetFirstName("Jarvis");
            tony.HoursWorked = 55;
            tony.HourlyWage  = 3000;
            tony.SetFirstName("Tony");
            pepper.Salary = 200000;
            pepper.SetFirstName("Pepper");
            pepper.PhoneNumber = "317-730-2728";
            HourlyEmployee peter = new HourlyEmployee();
            SalaryEmployee happy = new SalaryEmployee();

            happy.Salary = 150000;
            happy.SetFirstName("Happy");
            peter.SetFirstName("Peter");

            List <Employee> allEmployees = new List <Employee>();

            allEmployees.Add(jarvis);
            allEmployees.Add(tony);
            allEmployees.Add(pepper);
            allEmployees.Add(peter);
            allEmployees.Add(happy);

            foreach (Employee worker in allEmployees)
            {
                Console.WriteLine($"{worker.Name}'s phone number is {worker.PhoneNumber}");

                if (worker.GetType() == typeof(SalaryEmployee))
                {
                    SalaryEmployee sEmployee = (SalaryEmployee)worker;
                    Console.WriteLine($"{worker.Name} is a salary employee that makes {sEmployee.Salary}");
                }
                else if (worker is HourlyEmployee hourlyWorker)
                {
                    Console.WriteLine($"{worker.Name} has worked {hourlyWorker.HoursWorked} hours!");
                }
            }
        }
コード例 #5
0
        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!");
                }
            }
        }
コード例 #6
0
        public void EmployeeTest()
        {
            Employee       jarvis = new Employee();
            HourlyEmployee tony   = new HourlyEmployee();
            SalaryEmployee pepper = new SalaryEmployee();

            tony.HoursWorked = 55;
            tony.HourlyWage  = 303;
            pepper.Salary    = 122333;
            HourlyEmployee peter = new HourlyEmployee();
            SalaryEmployee happy = new SalaryEmployee();

            happy.Salary = 88123;

            List <Employee> allEmployees = new List <Employee>();

            jarvis.SetFirstName("Jarvis");
            tony.SetFirstName("Tony");
            pepper.SetFirstName("Pepper");
            peter.SetFirstName("Peter");
            happy.SetFirstName("Happy");

            allEmployees.Add(jarvis);
            allEmployees.Add(tony);
            allEmployees.Add(pepper);
            allEmployees.Add(peter);
            allEmployees.Add(happy);

            foreach (Employee worker in allEmployees)
            {
                if (worker.GetType() == typeof(SalaryEmployee))
                {
                    SalaryEmployee sEmloyee = (SalaryEmployee)worker; // This is called: Casting.
                    Console.WriteLine($"This is a Salary Employee that makes {sEmloyee.Salary}.");
                }
                else if (worker is HourlyEmployee hourlyWorker)
                {
                    Console.WriteLine($"{worker.Name} has worked {hourlyWorker.HoursWorked} hours.");
                }
            }
        }
コード例 #7
0
        public void EmployeeTests()
        {
            Employee       jarvis = new Employee();
            HourlyEmployee tony   = new HourlyEmployee();
            SalaryEmployee pepper = new SalaryEmployee();

            tony.HourlyWage = 3000;
            pepper.Salary   = 2000000;
            HourlyEmployee peter = new HourlyEmployee();
            SalaryEmployee happy = new SalaryEmployee();

            happy.Salary = 150000;
            List <Employee> allEmployess = new List <Employee>();

            jarvis.SetFirstName("jarvis");
            tony.SetFirstName("tony");
            pepper.SetFirstName("pepper");
            peter.SetFirstName("peter");
            happy.SetFirstName("happy");
            allEmployess.Add(jarvis);
            allEmployess.Add(tony);
            allEmployess.Add(pepper);
            allEmployess.Add(peter);
            allEmployess.Add(happy);

            foreach (Employee worker in allEmployess)
            {
                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)
                {
                    Console.WriteLine($"{worker.Name} has worked {hourlyWorker.HoursWorked} hours!");
                }
            }
        }
コード例 #8
0
        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!");
                }
            }
        }