-4

我有一个 Bean 的 ArrayList。bean 内部有两个属性“String monthYear”和“double bill”,我需要添加并根据 monthYear 获取总帐单。

例如,基于下面的代码,我需要得到输出

 JAN-2013 = 210
 FEB-2013 = 20 
 NOV-2012 = 130 
 DEC-2012 = 40

public static void main(String[] args) {
        List<BillsBean> billList = new ArrayList<BillsBean>();

        BillsBean bills1 = new BillsBean();
        bills1.setMonthYear("JAN-2013");
        bills1.setBill(10);
        billList.add(bills1);

        BillsBean bills2 = new BillsBean();     
        bills2.setMonthYear("FEB-2013");
        bills2.setBill(20);
        billList.add(bills2);

        BillsBean bills3 = new BillsBean();
        bills3.setMonthYear("NOV-2012");
        bills3.setBill(30);
        billList.add(bills3);

        BillsBean bills4 = new BillsBean();
        bills4.setMonthYear("DEC-2012");
        bills4.setBill(40);
        billList.add(bills4);

        BillsBean bills5 = new BillsBean();
        bills5.setMonthYear("NOV-2012");
        bills5.setBill(100);
        billList.add(bills5);

        BillsBean bills6 = new BillsBean();
        bills6.setMonthYear("JAN-2013");
        bills6.setBill(200);
        billList.add(bills6);
}
4

3 回答 3

1

在您的类中添加以下方法并从 main 调用它。

private static void calculateSum(List<BillsBean> billList) {
    Map<String,Double> sumMap=new HashMap<>();
    for(BillsBean bean:billList)
        if(sumMap.containsKey(bean.getMonthYear()))
            sumMap.put(bean.getMonthYear(), bean.getBill()+sumMap.get(bean.getMonthYear()));
        else
            sumMap.put(bean.getMonthYear(),bean.getBill());
    Iterator iter = sumMap.keySet().iterator();
    while(iter.hasNext()){
        String key = iter.next().toString();
        System.out.println("Year: "+key+" Bill: "+sumMap.get(key));
    }
 }
于 2013-05-14T05:03:27.920 回答
1

看看这个,它可能对你有帮助,通过扩展 ArrayList 和创建自定义类

public class CustomList extends ArrayList<BillsBean>{

    public double addAllBill(){
        double sum=0;
        Iterator<BillsBean> it = this.iterator();
        while(it.hasNext()){
            sum += it.next().getBill(); 
        }

        return sum;
    }

    public double addAllBill(String s1, String s2) throws ParseException{
        double sum=0;
        SimpleDateFormat monthYearFormat = new SimpleDateFormat("MMM-yyyy");
        Date date1=null;
        Date date2=null;
        Date date3= null;
        if(s1 == null){
            date1 = monthYearFormat.parse("JAN-1900");
        }else{
            date1 = monthYearFormat.parse(s1);
        }
        if(s2 == null){
            date2 = monthYearFormat.parse("JAN-1900");
        }else{
            date2 = monthYearFormat.parse(s2);
        }
        Iterator<BillsBean> it = this.iterator();
        BillsBean bean=null;
        while(it.hasNext()){
            bean= it.next();
            date3= monthYearFormat.parse(bean.getMonthYear());
            if(date3.compareTo(date1)>=0 && date3.compareTo(date2)<=0)
                sum += bean.getBill();
        }

        return sum;
    }
}

在主要检查中使用以下行

System.out.println("日期之间:" + billList.addAllBill("DEC-2012","FEB-2013"));

于 2013-05-14T05:35:54.187 回答
1

您需要简单的迭代来获得总数。你可以有两种方法。检查以下代码。

 public static  double getTotal(List<BillsBean> billsBeans,String monthYear) {
        double total=0.0d;;
        for(BillsBean bb : billsBeans) {
            if(bb.getMonthYear().equals(monthYear)) {
                total += bb.getBill();
            }
        }
        return total;
    }

    public static HashMap<String,Double> getTotals(List<BillsBean> billsBeans) {
        HashMap<String,Double> totalMap = new HashMap();
        for(BillsBean bb : billsBeans) {
            Double billAmount = totalMap.get(bb.getMonthYear());
            if( billAmount == null) {
                billAmount = bb.getBill();
            } else {
                billAmount = billAmount.doubleValue() + bb.getBill();
            }
            totalMap.put(bb.getMonthYear(),billAmount);
        }
        return totalMap;
    }
于 2013-05-14T04:52:49.313 回答