使用 Java 实现枚举工资单

发布于 2024-11-07 16:51:43 字数 966 浏览 0 评论 0原文

抱歉,只是尝试做一道过去的考试题来准备 Java 考试,希望有人能告诉我我的解决方案是否正确。问题:

使用 Java 枚举,实现一个用于工资计算的类。类中的常量对应于一周中的正常日子:星期一、星期二...星期五,周末日:星期六和星期日以及银行假日,这是一周中的特殊节日。

该类应该提供一个实例方法double pay (double time, double payrate),它返回当天工作的员工的总工资。计算规则如下: 给

定日期的工资由下式给出

base pay = pay rate * hours worked.

这里,基本工资由下式给出:

base pay = pay rate * hours worked

当天的加班工资由下式给出

overtime pay = pay rate * overtime hours/2

加班时间取决于日期的种类。

正常工作日:对于正常工作日,加班时间是指当天工作时间超过 8 小时的时间。

周末:周末的加班时间为当天的工作时间。

银行假日:银行假日加班时间为当天工作时间的 1.5 倍。

确保你的类是可维护的。应该可以在不破坏现有代码的情况下添加和删除。提示:使用策略枚举模式实现您的类。

public class Payroll {
   public enum Day {
      MONDAY, TUESDAY, WEDNESDAY,THURSDAY, FRIDAY, SATURDAY, SUNDAY, BANK HOLIDAY;
   }

   public double pay( double time, double payrate ) {
      if ( day
   }



}

我真的不知道接下来该去哪里,有人可以帮助我吗?

Sorry, just attempting to do a past exam question in preparation for a Java exam and was hoping someone could tell me whether or not my solution is correct. The question :

Using Java enums, implement a class for payroll computation. The constants in the class correspond to the normal days of the week : Monday, Tuesday...Friday, weekend days : Saturday and Sunday and a Bank Holiday which is a special fay of the week.

The class should provide an instance method double pay ( double time, double payrate ) which returns the total pay of an employee who has worked on the current day. The rules for computation are as follows :

The pay for the given day given by

base pay = pay rate * hours worked.

Here, base pay is given by :

base pay = pay rate * hours worked

The overtime pay of that day is given by

overtime pay = pay rate * overtime hours/2

The overtime hours depend on the kind of day.

Normal weekday: For a normal weekday, overtime hours are the hours worked on that day in excess of 8 hours.

Weekend: For weekend days, the overtime hours are the hours worked on that day.

Bank holiday: For a bank holiday, the over time hours are 1.5 times the hours worked on that day.

Make sure your class is maintainable. It should be possible to add and remove without breaking existing code. Hint: implement your class with the strategy enum pattern.

public class Payroll {
   public enum Day {
      MONDAY, TUESDAY, WEDNESDAY,THURSDAY, FRIDAY, SATURDAY, SUNDAY, BANK HOLIDAY;
   }

   public double pay( double time, double payrate ) {
      if ( day
   }



}

I can't really figure out where to go with this next, would anyone be able to help me with this please?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长伴 2024-11-14 16:51:43

有一个提示要求策略枚举模式。这是一个起点:

 public enum Day {
    MONDAY(Type.NORMAL), TUESDAY(Type.NORMAL), WEDNESDAY(Type.NORMAL), 
    THURSDAY(Type.NORMAL), FRIDAY(Type.NORMAL), SATURDAY(Type.WEEKEND),
    SUNDAY(Type.WEEKEND), BANK_HOLIDAY(Type.BANK_HOLIDAY);

    private Type type;                         // <- the strategy
    private Day(Type type) {this.type=type;}

    public double pay(double time, double payRate) {
       // calculate the pay using the strategy: type.overtimePay(hours, payRate)
    }

    private enum Type {
      NORMAL {
         @Override
         double overtimePay(double hours, double payRate) {
            // TODO: implement
         }
      }
      // same for WEEKEND and BANK_HOLIDAY

      abstract double overtimePay(double hours, double payRate);
    }
 }

所以现在,如果你想计算工资,应该可以使用

 double pay = Day.MONDAY.pay(10.5, 16.50);

或使用一个方便的(尚未实现的方法):

 double pay = getDay(aDate).pay(10.5, 16.50);  // getDay returns a Day enum

There is a hint asking for the strategy enum pattern. Here's a starting point:

 public enum Day {
    MONDAY(Type.NORMAL), TUESDAY(Type.NORMAL), WEDNESDAY(Type.NORMAL), 
    THURSDAY(Type.NORMAL), FRIDAY(Type.NORMAL), SATURDAY(Type.WEEKEND),
    SUNDAY(Type.WEEKEND), BANK_HOLIDAY(Type.BANK_HOLIDAY);

    private Type type;                         // <- the strategy
    private Day(Type type) {this.type=type;}

    public double pay(double time, double payRate) {
       // calculate the pay using the strategy: type.overtimePay(hours, payRate)
    }

    private enum Type {
      NORMAL {
         @Override
         double overtimePay(double hours, double payRate) {
            // TODO: implement
         }
      }
      // same for WEEKEND and BANK_HOLIDAY

      abstract double overtimePay(double hours, double payRate);
    }
 }

So now, if you want to calculate the pay, it should be possible with

 double pay = Day.MONDAY.pay(10.5, 16.50);

or with a convenient (yet to be implemented method):

 double pay = getDay(aDate).pay(10.5, 16.50);  // getDay returns a Day enum
墨落画卷 2024-11-14 16:51:43

正如 Andreas_D 的建议,您应该查看 策略枚举模式;例如,请参阅这篇文章

这是一些快速破解的代码:

public enum PayRoll {

    MONDAY {
        public double pay( double time, double payrate ) {
            if (time <= 8)
                return payrate * time;
            else
                return payrate * 8 + payrate * (time - 8) / 2;
        }
    },

    // implement TUESDAY, WEDNESDAY,THURSDAY, FRIDAY

    SATURDAY {
        public double pay( double time, double payrate ) {
            return payrate * time / 2;
        }
    },

    // implement SUNDAY

    BANK_HOLIDAY {
        public double pay( double time, double payrate ) {
            return payrate * 1.5 * time / 2;
        }
    };


    public abstract double pay(double time, double payrate);
}

有了这些代码,您可以执行以下操作:

public static void main(String[] args) {
    PayRoll christmasDay = PayRoll.BANK_HOLIDAY;
    System.out.println(christmasDay.pay(5, 3));
}

As suggest by Andreas_D, you should look at strategy enum pattern; see for example this article.

Here's some quickly hacked code:

public enum PayRoll {

    MONDAY {
        public double pay( double time, double payrate ) {
            if (time <= 8)
                return payrate * time;
            else
                return payrate * 8 + payrate * (time - 8) / 2;
        }
    },

    // implement TUESDAY, WEDNESDAY,THURSDAY, FRIDAY

    SATURDAY {
        public double pay( double time, double payrate ) {
            return payrate * time / 2;
        }
    },

    // implement SUNDAY

    BANK_HOLIDAY {
        public double pay( double time, double payrate ) {
            return payrate * 1.5 * time / 2;
        }
    };


    public abstract double pay(double time, double payrate);
}

With that in place, you can do things like:

public static void main(String[] args) {
    PayRoll christmasDay = PayRoll.BANK_HOLIDAY;
    System.out.println(christmasDay.pay(5, 3));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文