public void increaseSales(int index, double amount) { // Sets CommissionEmployee reference of Employee equal to the commission employee casted to the // employee at index location CommissionEmployee Employee = (CommissionEmployee) employees[index]; // Calls increaseSales method sending sales to the corresponding method and class Employee.increaseSales(amount); }
public static void main(String[] args) throws Exception { ArrayList<Employee> emplyoeesList = new ArrayList(); BasePlusCommissionEmployee basePlusCommEmp = new BasePlusCommissionEmployee("BasePlusCommission", "Employee", "11111", 1000, 0.5, 5000); basePlusCommEmp.setBirthDate(01, 1, 1991); // You can check date validation // basePlusCommEmp.setBirthDate(01, 1, 1991); emplyoeesList.add(basePlusCommEmp); CommissionEmployee commEmp = new BasePlusCommissionEmployee("Excellent", "commEmp", "22222", 2000, 0.7, 7000); commEmp.setBirthDate(02, 02, 1992); emplyoeesList.add(commEmp); HourlyEmployee hourEmp = new HourlyEmployee("Good", "hourEmp", "33333", 25, 160); emplyoeesList.add(hourEmp); hourEmp.setBirthDate(03, 03, 1993); SalariedEmployee salEmp = new SalariedEmployee("best", "salEmp", "44444", 10000); salEmp.setBirthDate(04, 4, 1994); emplyoeesList.add(salEmp); PieceWorker pwEmp = new PieceWorker("nice", "pwEmp", "55555", 1.5, 1000); pwEmp.setBirthDate(05, 11, 55); emplyoeesList.add(pwEmp); for (Employee emp : emplyoeesList) { if (emp.hasBirthdayThisMonth()) { System.out.println( "\nEmployee: " + "'" + emp.getFirstName() + " ," + emp.getLastName() + "'" + " has birthday this month!\n" + emp.getBirthDate() + "\nTherefore additional 200$ were added to earnings.\n"); System.out.println(emp + "\n" + "earnings: $" + (emp.earnings() + 200) + "\n"); } else System.out.println(emp + "\n" + "earnings: $" + emp.earnings() + "\n"); } }
public static void main(String[] args) { // create subclass objects SalariedEmployee salariedEmployee = new SalariedEmployee("John", "Smith", "111-11-1111", new BirthDate(1, 1, 2000), 800.00); HourlyEmployee hourlyEmployee = new HourlyEmployee("Karen", "Price", "222-22-2222", new BirthDate(24, 12, 1989), 16.75, 40); CommissionEmployee commissionEmployee = new CommissionEmployee( "Sue", "Jones", "333-33-3333", new BirthDate(4, 4, 1880), 10000, .06); BasePlusCommissionEmployee basePlusCommissionEmployee = new BasePlusCommissionEmployee( "Bob", "Lewis", "444-44-4444", new BirthDate(12, 11, 2070), 5000, .04, 300); PieceWorker pieceWorker = new PieceWorker("K", "P", "555-55-5555", new BirthDate(2, 2, 1999), 100, 3); System.out.println("Employees processed individually:"); System.out.printf( "%n%s%n%s: $%,.2f%n%n", salariedEmployee, "earned", salariedEmployee.earnings()); System.out.printf("%s%n%s: $%,.2f%n%n", hourlyEmployee, "earned", hourlyEmployee.earnings()); System.out.printf( "%s%n%s: $%,.2f%n%n", commissionEmployee, "earned", commissionEmployee.earnings()); System.out.printf( "%s%n%s: $%,.2f%n%n", basePlusCommissionEmployee, "earned", basePlusCommissionEmployee.earnings()); System.out.printf("%s%n%s: $%,.2f%n%n", pieceWorker, "earned", pieceWorker.earnings()); // create four-element Employee array Employee[] employees = new Employee[5]; // initialize array with Employees employees[0] = salariedEmployee; employees[1] = hourlyEmployee; employees[2] = commissionEmployee; employees[3] = basePlusCommissionEmployee; employees[4] = pieceWorker; System.out.printf("Employees processed polymorphically:%n%n"); // generically process each element in array employees for (Employee currentEmployee : employees) { System.out.println(currentEmployee); // invokes toString // determine whether element is a BasePlusCommissionEmployee if (currentEmployee instanceof BasePlusCommissionEmployee) { // downcast Employee reference to // BasePlusCommissionEmployee reference BasePlusCommissionEmployee employee = (BasePlusCommissionEmployee) currentEmployee; employee.setBaseSalary(1.10 * employee.getBaseSalary()); System.out.printf( "new base salary with 10%% increase is: $%,.2f%n", employee.getBaseSalary()); } System.out.printf("earned $%,.2f%n%n", currentEmployee.earnings()); } // get type name of each object in employees array for (int j = 0; j < employees.length; j++) System.out.printf("Employee %d is a %s%n", j, employees[j].getClass().getName()); } // end main