home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol9n04.zip / BANK.PAS next >
Pascal/Delphi Source File  |  1990-01-22  |  3KB  |  123 lines

  1. BANK.PAS
  2.  
  3. PROGRAM bankdemo;
  4. (* Turbo Pascal 5.5 program *)
  5. USES queues,crt;
  6. TYPE
  7.   PatronPt = ^PatronItem;
  8.   PatronItem = OBJECT (GenericItem)
  9.     TaskTime : Word;
  10.     CONSTRUCTOR Init(min,max : Byte);
  11.     FUNCTION GetTime : Word;
  12.     DESTRUCTOR Done; virtual;
  13.   END;
  14.  
  15.   ActionType = (NewPatron, Serving, Idle);
  16.  
  17.   Bank = Object
  18.     Q : queue;
  19.     time, timeleft, idletime : Word;
  20.     AvgMins, Shortest, Longest : Byte;
  21.     CONSTRUCTOR Init(AMBP, ST, LT : Byte);
  22.     DESTRUCTOR  Done;
  23.     FUNCTION    GetTime : Word;
  24.     PROCEDURE   Tick;
  25.   END;
  26.  
  27.   CONSTRUCTOR PatronItem.Init(min,max : Byte);
  28.   BEGIN
  29.     TaskTime := 10*(min+random(succ(max-min)));
  30.   END;
  31.  
  32.   DESTRUCTOR PatronItem.Done;
  33.   BEGIN  END;
  34.  
  35.   FUNCTION PatronItem.GetTime : Word;
  36.   BEGIN  GetTime := TaskTime;  END;
  37.  
  38.   CONSTRUCTOR Bank.Init(AMBP, ST, LT : Byte);
  39.   BEGIN
  40.     Randomize;
  41.     Q.Init;
  42.     time := 0; timeLeft := 0; idletime := 0;
  43.     AvgMins := AMBP;
  44.     Shortest := ST;
  45.     Longest := LT;
  46.     Write('Now beginning bank simulation.  ');
  47.     WriteLn('New patrons arrive on the average of');
  48.     Write('every ',AvgMins,' minutes.  Their business ');
  49.     WriteLn('takes from ',shortest,' to ',longest,' minutes.');
  50.   END;
  51.  
  52.   DESTRUCTOR Bank.Done;
  53.   BEGIN
  54.     WriteLn;
  55.     Write('The simulation ran for ',time DIV 10,'.',time MOD 10);
  56.     WriteLn(' simulated minutes.  Of that time, the clerk');
  57.     Write('was idle for ',idletime DIV 10,'.',idletime MOD 10);
  58.     Write(' simulated minutes.  Maximum length of ');
  59.     WriteLn('queue was ',Q.GetMax);
  60.     Q.done;
  61.   END;
  62.  
  63.   FUNCTION Bank.GetTime : Word;
  64.   BEGIN  GetTime := Time;  END;
  65.  
  66.   PROCEDURE Bank.Tick;
  67.   VAR
  68.     A : PatronPt;
  69.     G : ItemPtr;
  70.  
  71.     PROCEDURE Event(Act : ActionType; T, L : Word);
  72.     BEGIN
  73.       Write(T DIV 10:4,'.',T MOD 10,' minutes   ');
  74.       CASE Act OF
  75.         NewPatron : Write('New patron joined queue.  ');
  76.         Serving   : Write('Clerk is serving a patron.');
  77.         Idle      : Write('Clerk is idle.            ');
  78.       END;
  79.       WriteLn('  ',L:4,' in queue.');
  80.     END;
  81.  
  82.   BEGIN
  83.     Write(time DIV 10:4,'.',time MOD 10,^M);
  84.     IF random(10*AvgMins) = 0 THEN
  85.       BEGIN
  86.         Q.enqueue(new(PatronPt,Init(Shortest, Longest)));
  87.         Event(NewPatron, time, Q.length);
  88.       END;
  89.     IF timeleft <= 1 THEN
  90.       BEGIN
  91.         A := PatronPt(Q.dequeue);
  92.         IF A <> NIL THEN
  93.           BEGIN
  94.             timeleft := A^.GetTime;
  95.             Dispose(A,done);
  96.             Event(Serving, time, Q.length);
  97.           END
  98.         ELSE
  99.           BEGIN
  100.             Inc(IdleTime);
  101.             IF timeleft = 1 THEN Event(idle, time, Q.length);
  102.           END;
  103.       END;
  104.     Inc(Time);
  105.     IF timeleft > 0 THEN dec(timeleft);
  106.     delay(100);
  107.   END;
  108.  
  109.  
  110. VAR FirstNational : Bank;
  111.  
  112. BEGIN
  113.   Randomize;
  114.   FirstNational.Init(4,3,5);
  115.   REPEAT
  116.     FirstNational.tick;
  117.   UNTIL KeyPressed OR (FirstNational.GetTime >= 600);
  118.   FirstNational.done;
  119. END.
  120.  
  121.  
  122.  
  123.