数学公式计算 结合 JavaBean

先上测试代码:
public static void main(String[] args) throws Exception {
        //实际操作对象    money, count, people, cat 
        Think think = new Think();
        think.setMoney("2640");
        think.setCount("50");
        think.setPeople("25");
        think.setCat("1");
        //用户输入的公式
        String exp = "((money+count)*people/100)+50-88+cat*10"; //644.5  

        //调用方法
        Object result = MathFormulaUtil.getResultAdvanced(think, exp);

        //打印结果
        System.out.println(result);

    }

Think实体类:


public class Think {
    private String money;
    private String count;
    private String people;
    private String cat;

    public String getMoney() {
        return money;
    }
    public void setMoney(String money) {
        this.money = money;
    }
    public String getCount() {
        return count;
    }
    public void setCount(String count) {
        this.count = count;
    }
    public String getPeople() {
        return people;
    }
    public void setPeople(String people) {
        this.people = people;
    }
    public Think(String money, String count, String people, String cat) {
        super();
        this.money = money;
        this.count = count;
        this.people = people;
        this.cat = cat;
    }
    public Think() {
        super();
    }
    public String getCat() {
        return cat;
    }
    public void setCat(String cat) {
        this.cat = cat;
    }
}

程序源码:

/*
    说明:
        源码中需要jep-java-3.4-trial.jar包,该包从官网
            http://www.singularsys.com/jep/
        下载
*/

public class MathFormulaUtil {

    /**
     * GET 新技能
     * @param obj 目标对象 里面封装了数据
     * @param exp 表达式
     * @return 计算结果
     * @throws Exception 异常 
     */
    @SuppressWarnings({"rawtypes"})
    public static Object getResultAdvanced(Object obj,String exp) throws Exception{
        Object result = null; //算术结果
        try {
            //核心对象
            Jep jep = new Jep();
            //获取目标对象的 属性名 和 属性值组成的Map对象,然后Map对象组成List
            List fieldsLists = getFiledsInfo(obj);
            //循环一次取出 List 中的 Map 中的 key - value
            for (Object listObj : fieldsLists) {
                //获取属性名称
                String fieldName = String.valueOf(((Map) listObj).get("name"));
                //获取属性的值
                Object fieldValue = getFieldValueByName(fieldName, obj);
                if(fieldValue != null && !fieldValue.toString().isEmpty()){ //保证非空
                    //添加变量 动态赋值
                    jep.addVariable(fieldName, Double.parseDouble(String.valueOf(fieldValue)));
                }
            }
            //解析
            jep.parse(exp);
            //计算结果
            result = jep.evaluate();
        } catch (Exception e) {
            System.err.println("表达式中所含的字段 在实体类中没有赋予相应的值");
        }
        //返回结果
        return result;
    }

    /**
     * 根据属性名获取属性值
     * @param fieldName 变量名
     * @param o 对象
     * @throws Exception 异常
     * 
     * @author 杨润康
     * */
    private static Object getFieldValueByName(String fieldName, Object o) throws Exception {
            //构造get方法名
            String getter = "get" + firstAlpUpCase(fieldName); 
            //获取方法
            Method method = o.getClass().getMethod(getter, new Class[] {});  
            //取值
            Object value = method.invoke(o, new Object[] {});

            return value;  
    } 


    /**
     * 获取 属性名(name),属性值(value)的map组成的list
     * @param 目标对象
     * @return 封装了 属性-值 的Map 组成的 List 集合
     * @throws Exception 异常
     * 
     * @author 杨润康
     * */
    @SuppressWarnings({ "unused", "rawtypes", "unchecked" })
    private static List getFiledsInfo(Object o) throws Exception{
        //获取定义的变量
        Field[] fields=o.getClass().getDeclaredFields();
        //变量名数组
        String[] fieldNames=new String[fields.length];
        List list = new ArrayList();
        Map infoMap=null;
        for(int i=0;i<fields.length;i++){
            infoMap = new HashMap();
            //获取变量名
            infoMap.put("name", fields[i].getName());
            //变量名对应的值
            infoMap.put("value", getFieldValueByName(fields[i].getName(), o));
            //存入集合
            list.add(infoMap);
        }
        return list;
    }

    /**
     * 字符串首字母大写
     * @param nodeName 节点名称
     * @return 首字母大写的字符串
     * 
     * @author 杨润康
     */
     public static String firstAlpUpCase(String nodeName) {
            String s = nodeName.substring(0,1);
            Pattern p = Pattern.compile("[a-z]");
            Matcher m = p.matcher(s);
            if(m.matches()){
              char[] cs=nodeName.toCharArray();
              cs[0]-=32;
              return String.valueOf(cs);
            }
            return nodeName;
     }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值