public double getAverageCost() { List<Cost> costs = costPersistence.getCosts(); if (isNullOrEmpty(costs)) { return 0.; } double price = 0.0; for (Cost cost : costs) { price += cost.getPrice(); } return price / costs.size(); }
public double getAveragePerPerson() { List<Cost> costs = costPersistence.getCosts(); if (isNullOrEmpty(costs)) { return 0.; } double price = 0.0; int n = 0; for (Cost cost : costs) { price += cost.getPrice(); n += cost.getAffectedPeople() <= 0 ? 1 : cost.getAffectedPeople(); } return price / n; }
public double getAverageForType(CostType costType) { List<Cost> costs = costPersistence.getCosts(); if (isNullOrEmpty(costs)) { return 0.; } double price = 0.0; int n = 0; for (Cost cost : costs) { if (cost.getCostType() != null && costType != null && cost.getCostType().getId() == costType.getId()) { price += cost.getPrice(); n++; } } System.out.println( String.format( "Average cost for %s is %s / %s = %s.", costType == null ? "null" : costType.getName(), price, n, price / n)); return price == 0 ? 0 : price / n; }