您的位置:首页 > 编程语言 > Java开发

数学公式计算 结合 JavaBean

2015-12-09 09:42 253 查看
先上测试代码:


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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: