表格逻辑的几个想法

简单的逻辑完全可以用if else解决掉;

现在有这样的一个逻辑

username

password

isBlackUser

age

分数

aaa

123456

TRUE

0

20

60

aaa

123456

FALSE

20

40

80

ccc

123456

TRUE

40

100

100

 

用户的数据如果满足逻辑表中的某一条会得到为一的分数。

 

 

一.if-else

如果逻辑表有成百上千的数据,直接使用if else也是很难的。

 

二.DB

逻辑表建成数据库的一张表(temp_table),直接通过sql搜索来得到分数

 

username

password

isBlackUser

age_l

age_r

score

aaa

123456

TRUE

0

20

60

bbb

123456

FALSE

20

40

80

ccc

123456

TRUE

40

100

100

 

Select score from temp_table where username = user.getUsername() and password = user.getPassword() and isBlackUser = user.getIsBlackUser() and age_r >= user.getAge() and age_l <= user.getAge();

 

三.Java

逻辑表列数是有限并且稳定的,行数会随着用户的个数变化

 

现在只需要把列的逻辑写好,

 

public static Object exe(List<Map> tableList, HashMap<String, Object> data1){
    for (Map map : tableList) {
        if(
                usernameMatch(map.get(username).toString(),data1.get(username).toString())
                &&passwordMatch(map.get(password).toString(),data1.get(password).toString())
                &&isBlackUserMatch((Boolean)map.get(isBlackUser),(Boolean)data1.get(isBlackUser))
                &&ageMatch(map.get(age).toString(),(Integer)data1.get(age))
        )
            return map.get(result);
    }
    return null;
}


private static Boolean usernameMatch(String var0, String var1) {
    if (var0 != null)
        return var0.equals(var1);
    return false;
}

private static Boolean passwordMatch(String var0, String var1) {
    if (var0 != null)
        return var0.equals(var1);
    return false;
}

private static Boolean isBlackUserMatch(Boolean var0, Boolean var1) {
    if (var0 != null)
        return var0.equals(var1);
    return false;
}

private static Boolean ageMatch(String var0, Integer var1) {
    if (var0 != null && var1 != null) {
        String[] split = var0.split(",");
        if (split.length > 1)
            return Integer.parseInt(split[0]) <= var1 && Integer.parseInt(split[1]) >= var1;
    }
    return false;
}
java example

 

 

行数完全可以使用POI读取excel,或其它方式读取其它存储的文件,动态封装大数据量的行数;但是这里仅仅是模拟。

//逻辑表数据
HashMap<String, Object> param1 = new HashMap<>();
param1.put(username, "aaa");
param1.put(password, "123456");
param1.put(isBlackUser, true);
param1.put(age, "0,20");
param1.put(result, "40");
tableList.add(param1);

HashMap<String, Object> param2 = new HashMap<>();
param2.put(username, "aaa");
param2.put(password, "123456");
param2.put(isBlackUser, false);
param2.put(age, "20,40");
param2.put(result, "60");
tableList.add(param2);


HashMap<String, Object> param3 = new HashMap<>();
param3.put(username, "ccc");
param3.put(password, "123456");
param3.put(isBlackUser, true);
param3.put(age, "40,100");
param3.put(result, "100");
tableList.add(param3);
java example

 

 

写一个作为接口的方法

public static Object exe(List<Map> tableList, HashMap<String, Object> data1){
    for (Map map : tableList) {
        if(
                usernameMatch(map.get(username).toString(),data1.get(username).toString())
                &&passwordMatch(map.get(password).toString(),data1.get(password).toString())
                &&isBlackUserMatch((Boolean)map.get(isBlackUser),(Boolean)data1.get(isBlackUser))
                &&ageMatch(map.get(age).toString(),(Integer)data1.get(age))
        )
            return map.get(result);
    }
    return null;
}
java 执行

 

 

执行以下,看看效果

//用户数据
User user = new User("ccc","123456",true,55);

HashMap<String, Object> data1 = new HashMap<>();
data1.put(username, user.getUsername());
data1.put(password, user.getPassword());
data1.put(isBlackUser, user.getIsBlackUser());
data1.put(age, user.getAge());

Object exe = exe(tableList, data1);
System.out.println(exe);

 

 

 

 

package com.ahd.excel_04.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TableTest {
    private static String username = "username";
    private static String password = "password";
    private static String isBlackUser = "isBlackUser";
    private static String age = "age";
    private static String result = "result";

    //决策表的 实现方案
    public static void main(String[] args) {

        List<Map> tableList = new ArrayList();

        //逻辑表数据
        HashMap<String, Object> param1 = new HashMap<>();
        param1.put(username, "aaa");
        param1.put(password, "123456");
        param1.put(isBlackUser, true);
        param1.put(age, "0,20");
        param1.put(result, "40");
        tableList.add(param1);

        HashMap<String, Object> param2 = new HashMap<>();
        param2.put(username, "aaa");
        param2.put(password, "123456");
        param2.put(isBlackUser, false);
        param2.put(age, "20,40");
        param2.put(result, "60");
        tableList.add(param2);


        HashMap<String, Object> param3 = new HashMap<>();
        param3.put(username, "ccc");
        param3.put(password, "123456");
        param3.put(isBlackUser, true);
        param3.put(age, "40,100");
        param3.put(result, "100");
        tableList.add(param3);

        //用户数据
        User user = new User("ccc","123456",true,55);

        HashMap<String, Object> data1 = new HashMap<>();
        data1.put(username, user.getUsername());
        data1.put(password, user.getPassword());
        data1.put(isBlackUser, user.getIsBlackUser());
        data1.put(age, user.getAge());

        Object exe = exe(tableList, data1);
        System.out.println(exe);

    }
    public static Object exe(List<Map> tableList, HashMap<String, Object> data1){
        for (Map map : tableList) {
            if(
                    usernameMatch(map.get(username).toString(),data1.get(username).toString())
                    &&passwordMatch(map.get(password).toString(),data1.get(password).toString())
                    &&isBlackUserMatch((Boolean)map.get(isBlackUser),(Boolean)data1.get(isBlackUser))
                    &&ageMatch(map.get(age).toString(),(Integer)data1.get(age))
            )
                return map.get(result);
        }
        return null;
    }


    private static Boolean usernameMatch(String var0, String var1) {
        if (var0 != null)
            return var0.equals(var1);
        return false;
    }

    private static Boolean passwordMatch(String var0, String var1) {
        if (var0 != null)
            return var0.equals(var1);
        return false;
    }

    private static Boolean isBlackUserMatch(Boolean var0, Boolean var1) {
        if (var0 != null)
            return var0.equals(var1);
        return false;
    }

    private static Boolean ageMatch(String var0, Integer var1) {
        if (var0 != null && var1 != null) {
            String[] split = var0.split(",");
            if (split.length > 1)
                return Integer.parseInt(split[0]) <= var1 && Integer.parseInt(split[1]) >= var1;
        }
        return false;
    }
}
完整代码-核心

 

 

/***
 * 实体类 User
 *  1. username,password,是否黑名单isBlackUser,age  四个值通过对区间的比较能唯一定位一个分数
 */
@Data
public class User {
    private String username;
    private String password;
    private Boolean isBlackUser;
    private Integer age;

    public User(String username, String password, Boolean isBlackUser, Integer age) {
        this.username = username;
        this.password = password;
        this.isBlackUser = isBlackUser;
        this.age = age;
    }
}
完整代码-User

 

 

四.Excel自身函数实现

 

可以将用户信息导入excel

通过excel函数实现

 

 

五.规则引擎

 

是专门处理业务逻辑的软件,使用决策表能可是实现上面逻辑表的功能

 

最后打成jar包,直接调用即可
 

 

六. 存储过程实现列的逻辑

 

posted @ 2021-03-06 10:05  爱华顿g  阅读(161)  评论(0编辑  收藏  举报