# Java如何优雅的通过表格中的规则计算结果

• 298

Java程序需要根据下面表格中的规则计算得到物品编号：

 红色 黑色 高<10 20<高<50 ... ... 高>1000 宽<20 FA VF ... ... QT 20<=宽<30 TA MF ... ... ZA 30<=宽<80 YN VF ... ... OP 宽>=80 UI SR ... ... OH

``````Box box = boxService.getBoxById(10);
String boxCode = "";

if(box.getWidth()<20){
if(box.getColor()=="红色"){
if(box.getHeight()<10){
boxCode = "FA";
}else if(box.getHieght() > 20 && box.getHieght() <50){
boxCode = "VF";
}
//省略...
}else if(box.getColor()=="黑色"){
//省略...
}
}else if(box.getWidth()>=20){
//省略...
}``````

6 个回答

• 54.9k
✓ 已被采纳

``````package playground.sf.q1010000042100219;

import java.util.List;

public class Solution {
private static List<Row> rows = initRows();
private static List<Column> columns = initColumns();

static String getCode(int width, int height, String color) {
var row = rows.stream()
.filter(it -> it.inRange(width))
.findFirst()
.orElse(null);
if (row == null) { return null; }
var column = columns.stream()
.filter(it -> it.getColor().equals(color) && it.inRange(height))
.findFirst()
.orElse(null);
if (column == null) { return null; }

return row.getCodes()[column.getIndex()];
}

public static void main(String[] args) {
System.out.println("Code: " + getCode(5, 5, "红色"));
System.out.println("Code: " + getCode(15, 25, "红色"));
System.out.println("Code: " + getCode(5, 1024, "黑色"));
System.out.println("Code: " + getCode(115, 115, "红色"));
}

private static List<Row> initRows() {
return List.of(
new Row(-1, 20, "FA", "VF", "QT"),
new Row(20, 30, "TA,MF,ZA".split(",")),
new Row(30, 80, "YN,VF,OP".split(",")),
new Row(80, -1, "UI,SR,OH".split(","))
);
}

private static List<Column> initColumns() {
int i = 0;
return List.of(
new Column(i++, "红色", -1, 10),
new Column(i++, "红色", 20, 50),
new Column(i++, "黑色", 1000, -1)
);
}

public static class Row {
private final int[] range;
private final String[] codes;

public Row(int min, int max, String... codes) {
range = new int[] { min, max };
this.codes = codes;
}

public int[] getRange() {
return range;
}

public String[] getCodes() {
return codes;
}

public boolean inRange(int value) {
return (range[0] < 0 || range[0] <= value)
&& (range[1] < 0 || range[1] > value);
}
}

public static class Column {
private final int index;
private final String color;
private final int[] range;

public Column(int index, String color, int min, int max) {
this.index = index;
this.color = color;
this.range = new int[] { min, max };
}

public boolean inRange(int value) {
return (range[0] < 0 || range[0] <= value)
&& (range[1] < 0 || range[1] > value);
}

public int getIndex() {
return index;
}

public String getColor() {
return color;
}

public int[] getRange() {
return range;
}
}
}``````

• 3.5k
``````let goodsConfig = new Map([
[(color='',width=0,height=0)=>(color==='红色'&&width<20&&height<10),['FA','TA']],
[(color='',width=0,height=0)=>(color==='红色'&&width>=20&&height<10),['YN','UI']],
[(color='',width=0,height=0)=>(color==='红色'&&width<20&&20<height<50),['VF','MF']],
[(color='',width=0,height=0)=>(color==='红色'&&width>=20&&20<height<50),['UI','SR']],
]);
let getCode=(color='',width=0,height=0)=>{
let res=[];
for(let [fun,val] of goodsConfig.entries()){
if(fun(color,width,height)){
res = val;
break;
}
}
return res;
}
getCode('红色',10,30);
(2) ['VF', 'MF']``````

https://segmentfault.com/a/11...

• 15

``````    public static void main(String[] args) throws ScriptException {
String code = "if(color==='红色'&&width<20&&height<10) return 'NA'; else return 'FA';";
HashMap map = new HashMap();
map.put("color","红色");
map.put("width",10);
map.put("height",3);
System.out.println(eval(code,map));
}
static String eval(String code, HashMap<String,String> param_map) throws ScriptException {
code = "function f () {" + code + "} f()"; // Or otherwise
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
param_map.entrySet().forEach(stringStringEntry -> {
engine.put(stringStringEntry.getKey(),stringStringEntry.getValue());
});
String result =  (String)engine.eval(code);
return result;
}``````

``````//规则一：所购图书总价在100元以下的没有优惠
rule "book_discount_1"
when
\$order:Order(originalPrice < 100)
then
\$order.setRealPrice(\$order.getOriginalPrice());
print("所购图书总价在100元以下的没有优惠");
end

//规则二：所购图书总价在100到200元的优惠20元
rule "book_discount_2"
when
\$order:Order(originalPrice < 200 && originalPrice >= 100)
then
\$order.setRealPrice(\$order.getOriginalPrice() - 20);
print("所购图书总价在100到200元的优惠20元");
end

//规则三：所购图书总价在200到300元的优惠50元
rule "book_discount_3"
when
\$order:Order(originalPrice <= 300 && originalPrice >= 200)
then
\$order.setRealPrice(\$order.getOriginalPrice() - 50);
print("所购图书总价在200到300元的优惠50元");
end

//规则四：所购图书总价在300元以上的优惠100元
rule "book_discount_4"
when
\$order:Order(originalPrice >= 300)
then
\$order.setRealPrice(\$order.getOriginalPrice() - 100);
print("所购图书总价在300元以上的优惠100元");
end

function void print(String message){
System.out.printf("成功匹配到规则:%s",message);
}``````

• 4

`res[i][j]`

###### 你尚未登录，登录后可以
• 和开发者交流问题的细节
• 关注并接收问题和回答的更新提醒
• 参与内容的编辑和改进，让解决方法与时俱进