Ejemplo n.º 1
0
  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");
    }
  }
Ejemplo n.º 2
0
  public static void main(String[] args) {
    Employee[] empList = new Employee[6]; // 2 salray, 2 hourly, 2 bonus

    // populate salary emps
    SalariedEmployee s1 = new SalariedEmployee("Helena", "Handbasket", "Theatre Manager");
    s1.setEmpType('S');
    s1.setPayFrequency('S');
    s1.setAnnualSalary(75000.00);
    empList[0] = s1;
    SalariedEmployee s2 = new SalariedEmployee("Rayne", "Bow", "Lighting Manager");
    s2.setEmpType('S');
    s2.setPayFrequency('S');
    s2.setAnnualSalary(100000.00);
    empList[1] = s2;

    // populate hourly emps
    HourlyEmployee h1 = new HourlyEmployee("Leah", "Tard", "Dancer");
    h1.setHourlyRate(20.00);
    h1.setHoursWorked(100.0);
    empList[2] = h1;
    HourlyEmployee h2 = new HourlyEmployee("Cat", "Arwall", "Singer");
    h2.setHourlyRate(22.00);
    h2.setHoursWorked(80.0);
    empList[3] = h2;

    // populate salary plus bonus emps
    SalaryPlusBonusEmployee b1 = new SalaryPlusBonusEmployee("Bob", "Fosse", "Choreographer");
    b1.setEmpType('B');
    b1.setPayFrequency('S');
    b1.setAnnualSalary(200000.00);
    b1.setPayoutBonus(true);
    b1.setBonusAmt(3000.00);
    empList[4] = b1;
    SalaryPlusBonusEmployee b2 = new SalaryPlusBonusEmployee("Ima", "Good", "Artistic Director");
    b2.setEmpType('B');
    b2.setPayFrequency('M');
    b2.setAnnualSalary(150000.00);
    b2.setPayoutBonus(false);
    empList[5] = b2;

    DecimalFormat dollar = new DecimalFormat("#,##0.00");
    char freqInd;
    String payFreq;
    for (Employee emps : empList) {
      System.out.println("--------------------------------------");
      System.out.println("Name: " + emps.getLastName() + ", " + emps.getFirstName());
      System.out.println("Job Title: " + emps.getJobTitle());
      freqInd = emps.getPayFrequency();
      switch (freqInd) {
        case 'S':
          payFreq = "SEMI-MONTHLY";
          break;
        case 'M':
          payFreq = "MONTHLY";
          break;
        case 'B':
        default:
          payFreq = "BIWEEKLY";
      }
      System.out.println("Employee is paid " + payFreq);

      if (emps instanceof SalaryPlusBonusEmployee) {
        System.out.println("Employee is SALARY + BONUS");
        SalaryPlusBonusEmployee empBon = (SalaryPlusBonusEmployee) emps;
        System.out.println("Annual Salary: $" + dollar.format(empBon.getAnnualSalary()));
        System.out.println("Bonus Amount: $" + dollar.format(empBon.getBonusAmt()));
        System.out.println("Pay Period Amount: $" + dollar.format(empBon.calcPeriodPay()));
      } else if (emps instanceof SalariedEmployee) {
        System.out.println("Employee is SALARIED");
        SalariedEmployee empSal = (SalariedEmployee) emps;
        System.out.println("Annual Salary: $" + dollar.format(empSal.getAnnualSalary()));
        System.out.println("Pay Period Amount: $" + dollar.format(empSal.calcPeriodPay()));
      } else if (emps instanceof HourlyEmployee) {
        System.out.println("Employee is HOURLY");
        HourlyEmployee empHour = (HourlyEmployee) emps;
        System.out.println("Hourly Rate: " + dollar.format(empHour.getHourlyRate()));
        System.out.println("Hours Worked: " + empHour.getHoursWorked());
        System.out.println("Pay Period Amount: $" + dollar.format(empHour.calcPeriodPay()));
      }
    }
  }
Ejemplo n.º 3
0
  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