數據庫裡面只有時間字段(包括年月日),資產名稱,資產數量,資產金額,學校名稱,怎樣
按年度統計統計每年學校的資產總數,總金額,會的能提點下麼?,求指點,能告訴大概
的方法,思路都好,在線等,謝謝 (java)
class Record{
private String dateStr = "";
private String schoolName = "";
private int money = 0;
public Record(String dateStr,String schoolName,int money){
this.dateStr = dateStr;
this.schoolName = schoolName;
this.money = money;
}
public String getYear() {
return dateStr.split("-")[0];
}
public String getSchoolName() {
return schoolName;
}
public int getMoney() {
return money;
}
}
public class Test {
public List<Record> init(){
List<Record> list = new ArrayList();
list.add(new Record("2015-01-01","學校A",200));
list.add(new Record("2015-02-01","學校A",300));
list.add(new Record("2015-03-01","學校A",400));
list.add(new Record("2015-01-01","學校B",200));
list.add(new Record("2015-01-01","學校B",600));
list.add(new Record("2015-02-01","學校B",800));
return list;
}
public HashMap<String,HashMap<String,Integer>> tongJi(List<Record> list){
HashMap<String,HashMap<String,Integer>> rstMap = new HashMap();
for(int i=0;i<list.size();i++){
Record rd = list.get(i);
if(rstMap.get(rd.getYear())==null){
rstMap.put(rd.getYear(),new HashMap());
}
HashMap<String,Integer> schoolMap= rstMap.get(rd.getYear());
if(schoolMap.get(rd.getSchoolName())==null){
schoolMap.put(rd.getSchoolName(),0);
}
Integer value = schoolMap.get(rd.getSchoolName());
value = value + rd.getMoney();
schoolMap.put(rd.getSchoolName(),value);
}
return rstMap;
}
public void print(HashMap<String,HashMap<String,Integer>> rstMap){
Iterator iter1 = rstMap.keySet().iterator();
while(iter1.hasNext()){
String year = (String)iter1.next();
HashMap<String,Integer> schoolMap= rstMap.get(year);
Iterator iter2 = schoolMap.keySet().iterator();
while(iter2.hasNext()){
String schoolName = (String)iter2.next();
int value = schoolMap.get(schoolName);
System.out.println("year="+year+",school="+schoolName + ",金額="+value);
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
Test t = new Test();
List<Record> list = t.init();
HashMap map = t.tongJi(list);//統計
t.print(map);
}
}