統計java類的屬性的完成率,像個人資料完成率統計之類
阿新 • • 發佈:2018-11-08
public String count(PopulationVo populationVo){ Field[] fields = populationVo.getClass().getDeclaredFields(); if (null == fields) { return "0"; } int count=0; int finishcount=0; String wcl = "0"; for (Field s : fields) { String name = s.getName();//獲取屬性名 String type = s.getGenericType().toString();//獲取屬性型別 if (type.equals("class java.lang.String")) { if (!"hourceAddress".equals(name)||!"houseCode".equals(name)){ count++; try { Method m = populationVo.getClass().getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1)); String value = (String) m.invoke(populationVo); if (null != value) { // log.info("---屬性名---" + name); // log.info("---屬性值---" + value); finishcount++; } else { //log.info("---屬性名---" + name); } } catch (Exception e) { e.printStackTrace(); } } } /* if (type.equals("class java.lang.Integer")) { try { Method m = populationVo.getClass().getMethod("get" + name.substring(0, 1).toUpperCase() + name.substring(1)); int value = (String) m.invoke(populationVo); if (null != value) { Log.d("---屬性名---",name); Log.d("---屬性值---",value); } } catch (Exception e) { e.printStackTrace(); } }*/ } if (count != 0) { NumberFormat numberFormat = NumberFormat.getInstance(); // 設定精確到小數點後2位 numberFormat.setMaximumFractionDigits(2); wcl = numberFormat.format((float) finishcount / (float) count * 100); System.out.println(finishcount+"count"+count+"wcl"+wcl); } return wcl; }