发布于2021-03-10 19:03 阅读(1001) 评论(0) 点赞(22) 收藏(4)
简单的逻辑完全可以用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也是很难的。
逻辑表建成数据库的一张表(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();
逻辑表列数是有限并且稳定的,行数会随着用户的个数变化
现在只需要把列的逻辑写好,
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; }
行数完全可以使用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);
写一个作为接口的方法
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; }
执行以下,看看效果
//用户数据 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; } }
可以将用户信息导入excel
通过excel函数实现
是专门处理业务逻辑的软件,使用决策表能可是实现上面逻辑表的功能
最后打成jar包,直接调用即可
原文链接:https://www.cnblogs.com/aihuadung/p/14489652.html
作者:木得事
链接:http://www.javaheidong.com/blog/article/112222/9b0dd5a10face2f2c130/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!