DailyTotalCostConverter { public static List<DailyTotalCost> convertCostsToDailyCosts(List<Cost> costList) { List<DailyTotalCost> result = new ArrayList<>(); if (costList == null || costList.size() == 0) { return result; } List<Cost> dailyCosts = new ArrayList<>(); Date currentDate = costList.get(0).getDate(); Cost cost; for (int idx = 0; idx < costList.size(); idx++) { cost = costList.get(idx); if (idx == 0) { dailyCosts = new ArrayList<>(); currentDate = cost.getDate(); } dailyCosts.add(cost); if (idx == costList.size() - 1) { DailyTotalCost dailyTotalCost = new DailyTotalCost(dailyCosts, currentDate); result.add(dailyTotalCost); continue; } Cost nextCost = costList.get(idx + 1); if (!nextCost.getDate().equals(currentDate)) { DailyTotalCost dailyTotalCost = new DailyTotalCost(dailyCosts, currentDate); result.add(dailyTotalCost); dailyCosts = new ArrayList<>(); currentDate = nextCost.getDate(); } } return result; }  static List<DailyTotalCost> convertCostsToDailyCosts(List<Cost> costList);  }
@Test public void testDailyCostConversion() throws Exception { mCosts = new ArrayList<>(); mCosts.add(new Cost("Transportation", "ZET", TestDateUtil.getDate(2016, Calendar.JANUARY, 4), 100.0)); mCosts.add(new Cost("Groceries", "ZET", TestDateUtil.getDate(2016, Calendar.JANUARY, 4), 200.0)); mCosts.add(new Cost("Entertainment", "ZET", TestDateUtil.getDate(2016, Calendar.JANUARY, 4), 300.0)); mCosts.add(new Cost("Bills", "HEP struja", TestDateUtil.getDate(2016, Calendar.JANUARY, 4), 400.0)); mCosts.add(new Cost("Transportation", "Description", TestDateUtil.getDate(2016, Calendar.JANUARY, 2), 150.0)); mCosts.add(new Cost("Transportation", "Description", TestDateUtil.getDate(2016, Calendar.JANUARY, 2), 110.0)); mCosts.add(new Cost("Transportation", "Description", TestDateUtil.getDate(2016, Calendar.JANUARY, 2), 240.0)); mCosts.add(new Cost("Transportation", "Description", TestDateUtil.getDate(2016, Calendar.JANUARY, 1), 130.0)); mCosts.add(new Cost("Transportation", "Description", TestDateUtil.getDate(2016, Calendar.JANUARY, 1), 230.0)); List<DailyTotalCost> dailyTotalCosts = DailyTotalCostConverter.convertCostsToDailyCosts(mCosts); assertEquals(3, dailyTotalCosts.size()); assertEquals(4, dailyTotalCosts.get(0).getCostList().size()); assertEquals(1000.0, dailyTotalCosts.get(0).getTotalCost(), 0.00001); assertEquals(3, dailyTotalCosts.get(1).getCostList().size()); assertEquals(500.0, dailyTotalCosts.get(1).getTotalCost(), 0.00001); assertEquals(2, dailyTotalCosts.get(2).getCostList().size()); assertEquals(360.0, dailyTotalCosts.get(2).getTotalCost(), 0.00001); }
@Test public void testDailyCostConversion2() throws Exception { mCosts = new ArrayList<>(); mCosts.add(new Cost("Transportation", "ZET", TestDateUtil.getDate(2016, Calendar.JANUARY, 4), 100.0)); mCosts.add(new Cost("Transportation", "Description", TestDateUtil.getDate(2016, Calendar.JANUARY, 2), 150.0)); mCosts.add(new Cost("Transportation", "Description", TestDateUtil.getDate(2016, Calendar.JANUARY, 2), 110.0)); mCosts.add(new Cost("Transportation", "Description", TestDateUtil.getDate(2016, Calendar.JANUARY, 2), 240.0)); mCosts.add(new Cost("Transportation", "Description", TestDateUtil.getDate(2016, Calendar.JANUARY, 1), 130.0)); mCosts.add(new Cost("Transportation", "Description", TestDateUtil.getDate(2016, Calendar.JANUARY, 1), 230.0)); List<DailyTotalCost> dailyTotalCosts = DailyTotalCostConverter.convertCostsToDailyCosts(mCosts); assertEquals(3, dailyTotalCosts.size()); assertEquals(1, dailyTotalCosts.get(0).getCostList().size()); assertEquals(100.0, dailyTotalCosts.get(0).getTotalCost(), 0.00001); assertEquals(3, dailyTotalCosts.get(1).getCostList().size()); assertEquals(500.0, dailyTotalCosts.get(1).getTotalCost(), 0.00001); assertEquals(2, dailyTotalCosts.get(2).getCostList().size()); assertEquals(360.0, dailyTotalCosts.get(2).getTotalCost(), 0.00001); }