private void createBillFor(Customer customer) {
    List<CallEvent> customerEvents = new ArrayList<CallEvent>();
    for (CallEvent callEvent : callLog) {
      if (callEvent.getCaller().equals(customer.getPhoneNumber())) {
        customerEvents.add(callEvent);
      }
    }

    List<Call> calls = new ArrayList<Call>();

    CallEvent start = null;
    for (CallEvent event : customerEvents) {
      if (event instanceof CallStart) {
        start = event;
      }
      if (event instanceof CallEnd && start != null) {
        calls.add(new Call(start, event));
        start = null;
      }
    }

    BigDecimal totalBill = new BigDecimal(0);
    List<LineItem> items = new ArrayList<LineItem>();

    for (Call call : calls) {

      Tariff tariff = getCustomerTariff(customer);

      BigDecimal cost = calculateCost(call, tariff);
      totalBill = totalBill.add(cost);
      items.add(new LineItem(call, cost));
    }

    new BillGenerator(printer).send(customer, items, MoneyFormatter.penceToPounds(totalBill));
  }
Пример #2
0
  private void createBillFor(Customer customer) {
    List<CallEvent> customerEvents = new ArrayList<CallEvent>();
    for (CallEvent callEvent : callLog) {
      if (callEvent.getCaller().equals(customer.getPhoneNumber())) {
        customerEvents.add(callEvent);
      }
    }

    List<Call> calls = new ArrayList<Call>();

    CallEvent start = null;
    for (CallEvent event : customerEvents) {
      if (event instanceof CallStart) {
        start = event;
      }
      if (event instanceof CallEnd && start != null) {
        calls.add(new Call(start, event));
        start = null;
      }
    }

    BigDecimal totalBill = new BigDecimal(0);
    List<LineItem> items = new ArrayList<LineItem>();

    for (Call call : calls) {

      Tariff tariff = CentralTariffDatabase.getInstance().tarriffFor(customer);

      BigDecimal cost;

      DaytimePeakPeriod peakPeriod = new DaytimePeakPeriod();
      if (peakPeriod.offPeak(call.startTime())
          && peakPeriod.offPeak(call.endTime())
          && call.durationSeconds() < 12 * 60 * 60) {
        cost = new BigDecimal(call.durationSeconds()).multiply(tariff.offPeakRate());
      } else {
        cost = new BigDecimal(call.durationSeconds()).multiply(tariff.peakRate());
      }

      cost = cost.setScale(0, RoundingMode.HALF_UP);
      BigDecimal callCost = cost;
      totalBill = totalBill.add(callCost);
      items.add(new LineItem(call, callCost));
    }

    new BillGenerator().send(customer, items, MoneyFormatter.penceToPounds(totalBill));
  }