// // ice cream store simulation -- test of event class // // Described in Chapter 7 of // Data Structures in C++ using the STL // Published by Addison-Wesley, 1997 // Written by Tim Budd, budd@cs.orst.edu // Oregon State University // # include # include # include # include "event.h" class randomInteger { public: unsigned int operator () (unsigned int); } randomizer; unsigned int randomInteger::operator () (unsigned int max) { // rand return random integer // convert to unsigned to make positive // take remainder to put in range unsigned int rval = rand(); return rval % max; } unsigned int randBetween(int low, int high) { return low + randomizer(high - low); } class IceCreamStore { public: IceCreamStore() : freeChairs(35), profit(0.0) { } bool canSeat (unsigned int numberOfPeople); void order(unsigned int numberOfScoops); void leave(unsigned int numberOfPeople); unsigned int freeChairs; double profit; }; simulation theSimulation; IceCreamStore theStore; bool IceCreamStore::canSeat (unsigned int numberOfPeople) // if sufficient room, then seat customers { cout << "Time: " << theSimulation.currentTime; cout << " group of " << numberOfPeople << " customers arrives"; if (numberOfPeople < freeChairs) { cout << " is seated" << endl; freeChairs -= numberOfPeople; return true; } else { cout << " no room, they leave" << endl; return false; } } void IceCreamStore::order (unsigned int numberOfScoops) // serve ice-cream, compute profits { cout << "Time: " << theSimulation.currentTime; cout << " serviced order for " << numberOfScoops << endl; profit += 0.35 * numberOfScoops; } void IceCreamStore::leave (unsigned int numberOfPeople) // people leave, free up chairs { cout << "Time: " << theSimulation.currentTime; cout << " group of size " << numberOfPeople << " leaves" << endl; freeChairs += numberOfPeople; } class arriveEvent : public event { public: arriveEvent (unsigned int time, unsigned int gs) : event(time), groupSize(gs) { } virtual void processEvent (); protected: unsigned int groupSize; }; class orderEvent : public event { public: orderEvent (unsigned int time, unsigned int gs) : event(time), groupSize(gs) { } virtual void processEvent (); protected: unsigned int groupSize; }; void arriveEvent::processEvent() { if (theStore.canSeat(groupSize)) theSimulation.scheduleEvent (new orderEvent(theSimulation.currentTime + randBetween(2,10), groupSize)); } class leaveEvent : public event { public: leaveEvent (unsigned int time, unsigned int gs) : event(time), groupSize(gs) { } virtual void processEvent (); protected: unsigned int groupSize; }; void orderEvent::processEvent() { // each person orders some number of scoops for (int i = 0; i < groupSize; i++) theStore.order(1 + randomizer(3)); int t = theSimulation.currentTime + randBetween(15,35); event * ne = new leaveEvent(t, groupSize); theSimulation.scheduleEvent (ne); }; void leaveEvent::processEvent () { theStore.leave(groupSize); } void main() { // load queue with some number of initial events unsigned int t = 0; while (t < 120) { t += randBetween(2,5); theSimulation.scheduleEvent(new arriveEvent(t, randBetween(1,5))); } // then run simulation and print profits theSimulation.run(); cout << "Total profits " << theStore.profit << endl; }