Answer to Question #287782 in C++ for Meh

Question #287782

Fast food custom simulation training application. The trainees encounter a wide array of situations, from working with customers and handling customer complaints, to inputting order into the system. Simulation allow new employees to experience all these scenarios without any disruption to the day-to-day restaurant affairs


1
Expert's answer
2022-01-16T09:02:45-0500
class event {
public:
  // Construct sets time of event.
  event (unsigned int t) : time (t)
    { }

  // Execute event by invoking this method.
  virtual void processEvent () = 0;

  const unsigned int time;
};
  struct eventComparator {
  bool operator() (const event * left, const event * right) const {
    return left->time > right->time;
  }
};
class simulation {
public:
  simulation () : time (0), eventQueue () 
    {}
  void run ();
  void  scheduleEvent (event * newEvent) {
    eventQueue.push (newEvent);
  }
  unsigned int time;
  protected:
  std::priority_queue<event*,
                      std::vector<event *, std::allocator<event*> >,
                      eventComparator> eventQueue;
};
  void simulation::run () {

  while (! eventQueue.empty ()) {

    event * nextEvent = eventQueue.top ();
    eventQueue.pop ();
    time = nextEvent->time;
    nextEvent->processEvent ();
    delete nextEvent;
  }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment