RSS
热门关键字:  java  Ajax  JSP  JSF  Struts
当前位置 : 首页>Java>列表

用匿名类处理分类汇总的方法

来源: 作者: 时间:2007-09-28 点击:
  分类汇总是统计中常用,举例来说如统计学生成绩,及格不及格的归类,分优良中差等级归类等,每个单项代码很好写,但是如果分类汇总的项目多了,能一种汇总写一个函数吗? 比如说有些科目60分才算及格,有些科目50分就算; 有些老师喜欢分优良中差四等,有些老师却喜欢分ABCD; 不一而足,如果每个都写一个函数无疑是个编写和维护恶梦. 如果我们用匿名类把分类汇总的规则和分类汇总的过程分别抽象出来,代码就清晰灵活多了,以下代码讲述了这个过程,代码比较简单,这里就不赘述了,相信大家都能看明白.

  首先是数据的基本类Student:

public class Student{
private String name;
private int score;

public Student(String name,int score){
this.name=name;
this.score=score;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
然后是用于分类汇总的类,它强制子类实现getKey和getvalue两个方法:
public abstract class ClassifyRule {
public Student student;

public ClassifyRule(){
}

public void setStudent(Student student) {
this.student = student;
}

abstract public String getKey();
abstract public int getValue();
}
接下来是对Student进行CRUD处理的StudentService类,注意getSum方法,它保留了筛选过程,筛选规则则不在其中:
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;

public class StudentService {
private List students;

public StudentService() {
students = new ArrayList();
}

public void add(Student student) {
students.add(student);
}

public Hashtable getSum(ClassifyRule rule) {
Hashtable ht = new Hashtable();

for (Student student : students) {
rule.setStudent(student);
String key = rule.getKey();
int value = rule.getValue();

if (ht.containsKey(key)) {
Integer oldValue = ht.remove(key);
oldValue += value;
ht.put(key, oldValue);
} else {
ht.put(key, value);
}
}

return ht;
}
}
最后是测试代码,注意其中筛选规则的创建:
import java.util.Hashtable;
import java.util.Iterator;

public class Test {
public static void main(String[] args) {
// 初始化
StudentService service = new StudentService();
service.add(new Student("Andy", 90));
service.add(new Student("Bill", 95));
service.add(new Student("Cindy", 70));
service.add(new Student("Dural", 85));
service.add(new Student("Edin", 60));
service.add(new Student("Felix", 55));
service.add(new Student("Green", 15));

// 60分及格筛选
ClassifyRule rule60 = new ClassifyRule() {
public String getKey() {
return student.getScore() >= 60 ? "及格" : "不及格";
}

public int getValue() {
return 1;
}
};

System.out.println("60分及格筛选");
printHt(service.getSum(rule60));

// 50分及格筛选
ClassifyRule rule50 = new ClassifyRule() {
public String getKey() {
return student.getScore() >= 50 ? "及格" : "不及格";
}

public int getValue() {
return 1;
}
};

System.out.println("\n50分及格筛选");
printHt(service.getSum(rule50));

// 分"优良中差"等级
ClassifyRule ruleCn = new ClassifyRule() {
public String getKey() {
String retval = "";

int score = student.getScore();
if (score >= 90) {
retval = "优";
} else if (score >= 80) {
retval = "良";
} else if (score >= 60) {
retval = "中";
} else if (score > 0) {
retval = "差";
}

return retval;

共2页: 上一页 1 [2] 下一页
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册