더북(TheBook)

유일한 데이터 멤버 service_t는 고객이 쇼핑한 상품을 계산하기까지 걸린 시간을 분 단위로 기록한다. 이 값은 고객에 따라 다르게 지정한다. time_decrement() 함수는 1분이 지날 때마다 호출되고, 고객이 계산할 때까지의 시간을 반영하는 service_t 값을 감소시킨다. service_t 값이 0일 때 done() 멤버는 true를 반환한다.

각 마트 계산대는 계산을 기다리는 고객 queue를 갖는다. 계산대 위치를 정의하는 클래스는 Checkout.h에 정의한다.

// 마트 계산대 - 대기열에 있는 고객을 관리하고 처리한다
#ifndef CHECKOUT_H
#define CHECKOUT_H
#include <queue>                                     // queue 컨테이너
#include “Customer.h”
 
class Checkout
{
private:
  std::queue<Customer> customers;                   // 계산을 기다리는 대기열(큐)
 
public:
  void add(const Customer& customer) { customers.push(customer); }
  size_t qlength() const { return customers.size(); }
 
  // 1분씩 대기 시간을 증가시킨다
  void time_increment()
  { // 기다리는 고객이 있다면…
    if (!customers.empty())
    {
      if (customers.front().time_decrement().done()) // 고객이 계산을 끝냈으면…
        customers.pop(); // …대기열에서 제거한다
    }
  };
 
  bool operator<(const Checkout& other) const { return qlength() < other.qlength(); }
  bool operator>(const Checkout& other) const { return qlength() > other.qlength(); }
};
#endif
 

신간 소식 구독하기
뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.