Esempio n. 1
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));
  }
 public static void initialiseBillingSystem() {
   // Clear all items from the previous fixtures:
   printer.clear();
   customerDatabase.clear();
   // BillGenerator can stay as it is.
   // Make sure no old times remain in the clock buffer:
   clock.reset();
   // Because the CustomerCallLog class has no clearing method,
   // a new object has to be created in here:
   callLog = new CustomerCallLog(clock);
   // The new callLog and changes to the strategy can't be set to the old BillingSystem,
   // so new one has to be created
   billingSystem =
       new BillingSystem(
           customerDatabase,
           CentralTariffDatabase.getInstance(),
           callLog,
           currentStrategy,
           billGenerator);
 }
 public Tariff getCustomerTariff(Customer customer) {
   return CentralTariffDatabase.getInstance().tarriffFor(customer);
 }