コード例 #1
0
 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();
 }
コード例 #2
0
 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;
 }
コード例 #3
0
 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;
 }