Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

java代码 分数段统计如何用 表驱动方式来编写

image.png
这是我自己写的代码 我觉得很差 希望能优化下 能用表驱动吗/

输出的结果是

统计年龄分布数据[ApproveCustomerEntity(area=null, fqzScore=null, ageGroup=[18,25], peopleNum=4),
[ApproveCustomerEntity(area=null, fqzScore=null, ageGroup=[26,30], peopleNum=4)......]
List<ApproveCustomerEntity> list = statisticsService.findApproveCustomerList(approveCustomer);
Integer sumNum = list.size();
List<ApproveCustomerEntity> list1 = new ArrayList<>();
String ageGroup1 = "[18,25]";
String ageGroup2 = "[26,30]";
String ageGroup3 = "[31,35]";
String ageGroup4 = "[36,40]";
String ageGroup5 = "[41,45]";
String ageGroup6 = "[46,50]";
String ageGroup7 = "[51,65]";
Integer age1 = 0;
Integer passAge1 = 0;
Integer rejectAge1 = 0;
Integer age2 = 0;
Integer passAge2 = 0;
Integer rejectAge2 = 0;
Integer age3 = 0;
Integer passAge3 = 0;
Integer rejectAge3 = 0;
Integer age4 = 0;
Integer passAge4 = 0;
Integer rejectAge4 = 0;
Integer age5 = 0;
Integer passAge5 = 0;
Integer rejectAge5 = 0;
Integer age6 = 0;
Integer passAge6 = 0;
Integer rejectAge6 = 0;
Integer age7 = 0;
Integer passAge7 = 0;
Integer rejectAge7 = 0;
Integer otherAge = 0;
Integer otherPassAge = 0;
Integer otherRejectAge = 0;
for (int i = 0; i < list.size(); i++) {
    Integer age = list.get(i).getAge();
    String rejectCode = list.get(i).getRejectCode();
    if(age >= 18 && age <= 25){
        age1 ++;
        if(JdConstans.REJECT_APPROVAL.equals(rejectCode)){
            passAge1++;
        }else {
            rejectAge1++;
        }
    }
}
ApproveCustomerEntity approveCustomerEntity = new ApproveCustomerEntity();
approveCustomerEntity.setAgeGroup(ageGroup1);
approveCustomerEntity.setPeopleNum(age1);
approveCustomerEntity.setPassNum(passAge1);
approveCustomerEntity.setRejectNum(rejectAge1);
for (int i = 0; i < list.size(); i++) {
    Integer age = list.get(i).getAge();
    String rejectCode = list.get(i).getRejectCode();
    if(age >= 26 && age <= 30){
        age2 ++;
        if(JdConstans.REJECT_APPROVAL.equals(rejectCode)){
            passAge2++;
        }else {
            rejectAge2++;
        }
    }
}
ApproveCustomerEntity approveCustomerEntity1 = new ApproveCustomerEntity();
approveCustomerEntity1.setAgeGroup(ageGroup2);
approveCustomerEntity1.setPeopleNum(age2);
approveCustomerEntity1.setPassNum(passAge2);
approveCustomerEntity1.setRejectNum(rejectAge2);
for (int i = 0; i < list.size(); i++) {
    Integer age = list.get(i).getAge();
    String rejectCode = list.get(i).getRejectCode();
    if(age >= 31 && age <= 35){
        age3 ++;
        if(JdConstans.REJECT_APPROVAL.equals(rejectCode)){
            passAge3++;
        }else {
            rejectAge3++;
        }
    }
}
ApproveCustomerEntity approveCustomerEntity2 = new ApproveCustomerEntity();
approveCustomerEntity2.setAgeGroup(ageGroup3);
approveCustomerEntity2.setPeopleNum(age3);
approveCustomerEntity2.setPassNum(passAge3);
approveCustomerEntity2.setRejectNum(rejectAge3);
for (int i = 0; i < list.size(); i++) {
    Integer age = list.get(i).getAge();
    String rejectCode = list.get(i).getRejectCode();
    if(age >= 36 && age <= 40){
        age4 ++;
        if(JdConstans.REJECT_APPROVAL.equals(rejectCode)){
            passAge4 ++;
        } else {
            rejectAge4 ++;
        }
    }
}
ApproveCustomerEntity approveCustomerEntity3 = new ApproveCustomerEntity();
approveCustomerEntity3.setAgeGroup(ageGroup4);
approveCustomerEntity3.setPeopleNum(age4);
approveCustomerEntity3.setPassNum(passAge4);
approveCustomerEntity3.setRejectNum(rejectAge4);
for (int i = 0; i < list.size(); i++) {
    Integer age = list.get(i).getAge();
    String rejectCode = list.get(i).getRejectCode();
    if(age >= 41 && age <= 45){
        age5 ++;
        if(JdConstans.REJECT_APPROVAL.equals(rejectCode)){
            passAge5 ++;
        } else {
            rejectAge5 ++;
        }
    }
}
ApproveCustomerEntity approveCustomerEntity4 = new ApproveCustomerEntity();
approveCustomerEntity4.setAgeGroup(ageGroup5);
approveCustomerEntity4.setPeopleNum(age5);
approveCustomerEntity4.setPassNum(passAge5);
approveCustomerEntity4.setRejectNum(rejectAge5);
for (int i = 0; i < list.size(); i++) {
    String rejectCode = list.get(i).getRejectCode();
    Integer age = list.get(i).getAge();
    if(age >= 46 && age <= 50){
        age6 ++;
        if(JdConstans.REJECT_APPROVAL.equals(rejectCode)){
            passAge6 ++;
        } else {
            rejectAge6 ++;
        }
    }
}
ApproveCustomerEntity approveCustomerEntity5 = new ApproveCustomerEntity();
approveCustomerEntity5.setAgeGroup(ageGroup6);
approveCustomerEntity5.setPeopleNum(age6);
approveCustomerEntity5.setPassNum(passAge6);
approveCustomerEntity5.setRejectNum(rejectAge6);
for (int i = 0; i < list.size(); i++) {
    String rejectCode = list.get(i).getRejectCode();
    Integer age = list.get(i).getAge();
    if(age >= 51 && age <= 65){
        age7 ++;
        if(JdConstans.REJECT_APPROVAL.equals(rejectCode)){
            passAge7 ++;
        } else {
            rejectAge7 ++;
        }
    }
}
ApproveCustomerEntity approveCustomerEntity6 = new ApproveCustomerEntity();
approveCustomerEntity6.setAgeGroup(ageGroup7);
approveCustomerEntity6.setPeopleNum(age7);
approveCustomerEntity6.setPassNum(passAge7);
approveCustomerEntity6.setRejectNum(rejectAge7);
list1.add(approveCustomerEntity);
list1.add(approveCustomerEntity1);
list1.add(approveCustomerEntity2);
list1.add(approveCustomerEntity3);
list1.add(approveCustomerEntity4);
list1.add(approveCustomerEntity5);
list1.add(approveCustomerEntity6);
System.out.println("统计年龄分布数据"+list1);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

如果用表驱动的方式,肯定是可以的,不过其实你用表驱动也是希望处理一些if else的问题,但是代码里去处理也是可以的哈。我写了一个例子,你可以简单参考一下,不过由于我写的类稍微多一点,所以我放在了git上,你可以去看看

先这里说明一下

  • 题主你开始查出来的是ApproveCustomerEntity,最后转换成的也是ApproveCustomerEntity,也就是说你的ApproveCustomerEntity既有agerejectCode属性,也有ageGrouppeopleNumpassNumrejectNum属性,我感觉很奇怪,所以我为了区分,最开始的list里装的是CustomerEntity,它有有agerejectCode属性,最后目标list1中装的是ApproveCustomerEntity,它有ageGrouppeopleNumpassNumrejectNum属性
  • 题主写的常量类JdConstans名字错了,我改成了JdConstants
  • JdConstans.REJECT_APPROVAL应该表示拒绝的意思吧,所以这个if块里我改为RejectNum+1

思路简单介绍

你可以从这个main类入口去看
image.png

思路上其实跟题主写的差不多,只是换了一种比较方便拓展的形式

  • 在做年龄分类,并且需要额外统计通过拒绝人数时,我采取了定制java.util.stream.Collector的方式,新的Collector实现为CustomerCollectorImpl,这样方便年龄分类并且做统计的处理可以单独抽象出来,以后修改容易,替换也容易,不会跟你现有业务代码强耦合。这里一定要采用定制Collector主要就是做了年龄分类后,还要做其他业务操作,如果只是做分类,Collectors.groupBy其实就已经足够了
  • 在年龄区间设置上,它其实也是一个强业务的点,所以可变性非常大,为了方便后续无论是增加区间,或者减少区间,或者修改区间数字,更或者是修改区间排序等操作更方便,我采用了枚举AgeGroup的方式进行整理,一目了然

image.png

  • .由于年龄区间区间二字具有更强的抽象性,跟业务可以是较小的耦合,并且也不局限于只是使用Integer的区间,其他诸如Long或者Double,更或者是只要可比的类,就应该满足区间这个需求,因为可比,就意味着有大小,有大小就可以设置区间,结合在java中可比的意思就是实现了Comparable接口,那我们可以根据当前区间所涉及到的一些业务处理,做了一个顶层的抽象,然后让我们的年龄区间满足这个抽象Range,并且这个区间抽象可以涉及更广的其他业务

image.png

简单解释一下上面的几个方法,Range就代表一个区间,所以它必须表示出最小值,最大的值,并且由于是区间,所以有开闭性,默认我给予的是左闭右开(题主的需求上应该是左闭右闭),还有一个isMatch的方法用于判断某一个值是否在这个区间里

同时在工厂类Ranges中我提供了一些默认构造方式,并且用一个基类Ranges.BaseRange来做通用的处理这样方便使用。

  • 同时还注意到,题主中有一个区间是"其他",因此我也在Range的基础上扩展了一个实现类Ranges.AlwaysMatchRange,它的isMatch实现永远返回true,方便做收尾处理
  • 这里为啥BaseRangeAlwaysMatchRange都放在了Ranges中作为内部类,就是希望使用者关注接口Range本身,实现方式通过Ranges的静态方法来做选择即可。类似TemporalAdjusterTemporalAdjusters的关系

可能回答和代码有点没有太结合起来,不过题主可以跑跑我的代码,试一下就知道了~拜了个拜?(^?^*)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...