【发布时间】:2016-10-15 20:52:54
【问题描述】:
我应该清理一个 java 代码,去掉很多东西,但是应该还有什么要清理的,也许以某种方式去掉多个 if 语句,而不完全重写这段代码?似乎无法弄清楚,因为它们是如此不同,以至于将它们堆叠在一个“如果”中。有什么想法吗?
public class Calc {
// employee types
public static final int SELLER;
public static final int COOK;
public static final int CHIEF;
public static void main(final String[] args) {
Calc c = new Calc();
System.err.println(c.pay(CHIEF) + " should be 66");
}
private int pay(final int type, final int h) {
int Sum = 0;
if (type == SELLER) {
if (h > 8) {
Sum = 20 * (h - 8);
Sum += 80;
} else {
Sum += 10 * h;
}
}
if (type == COOK) {
if (h > 8) {
Sum = 30 * (h - 8);
Sum += 15 * 8;
} else {
Sum += 15 * h;
}
}
if (type == CHIEF) {
if (h > 8) {
Sum = 66 * (h - 8);
Sum += 22 * 8;
} else {
Sum += 22 * h;
}
}
if (h > 20) {
if (type == SELLER) {
Sum += 10;
}
if (type == COOK) {
Sum += 20;
}
if (type == CHIEF) {
Sum += 30;
}
}
return Sum;
}
}
【问题讨论】:
标签: java code-cleanup