Thursday, February 3, 2011

Java Complex Enum

So this is a very complex Enum that has another Enum inside of itself. This could be a common pattern where the Main set of Objects is dependant on the input to the Main object. This nice because Enums have the same benefits of a Class.


private enum PayrollDay {

  MONDAY(PayType.WEEKDAY);
  

  private final PayType type;

  private PayrollDay(PayType type) {
    this.type = type;
  }

  double pay(double hoursRate, double payRate) {
    return type.pay(hoursRate, payRate);
  }

  enum PayType {

    WEEKDAY {

      double overtimePay(double hoursRate, double payRate) {
        return hoursRate <= 8 ? 0 : hoursRate * payRate / 2; 

      }
  }; 

  abstract double overtimePay(double hoursRate, double payRate); 

  private double pay(double hoursRate, double payRate) { 
    double basePay = hoursRate * payRate; 
    double overtime = overtimePay(hoursRate, payRate); 
    return basePay + overtime; 
  }

No comments:

Post a Comment