Ну и, наконец, класс 'Процессор':
| Код | TProcessor=class private FMaxClaims: Integer; FTurn: TTurn; procedure SetMaxClaims(const Value: Integer); procedure ExecStep(Claim: TClaim); public constructor Create(MaxClaims: Integer=20); destructor Destroy; override; procedure AddClaim(Claim: TClaim); procedure Execute;
property MaxClaims: Integer read FMaxClaims write SetMaxClaims; end;
{ TProcessor }
procedure TProcessor.AddClaim(Claim: TClaim); begin if FTurn.Count=FMaxClaims then raise Exception.Create('Нельзя добавить новую заявку, превышено максимальное число заявок - '+IntToStr(FMaxClaims)); FTurn.Add(Claim); end;
constructor TProcessor.Create(MaxClaims: Integer); begin FMaxClaims := MaxClaims; FTurn := TTurn.Create; end;
destructor TProcessor.Destroy; begin FTurn.Free; inherited; end;
procedure TProcessor.ExecStep(Claim: TClaim); begin Claim.DecCount; end;
procedure TProcessor.Execute; var i: Integer; begin while FTurn.Count>0 do begin for i := 0 to MaxSteps-1 do begin ExecStep(FTurn[0]); if FTurn[0].Count=0 then break; end; if FTurn[0].Count=0 then FTurn.Delete(0) else FTurn.MoveToLast; end; end;
procedure TProcessor.SetMaxClaims(const Value: Integer); begin if Value=FMaxClaims then Exit; if (FTurn.Count=FMaxClaims) and (Value<FMaxClaims) then raise Exception.Create('Нельзя установить максимальное число заявок в '+IntToStr(Value)+ ', число текущих заявок в обработке - '+IntToStr(FTurn.Count)); FMaxClaims := Value; end;
|
Добавлено @ 00:43 Вот тебе весь получившийся модуль:
| Код | unit Unit1;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
const MaxSteps=5; type
TClaim=class private FCount: Integer; public constructor Create; function DecCount: Integer;
property Count: Integer read FCount; end;
TTurn=class(TList) private function GetItems(Index: Integer): TClaim; procedure SetItems(Index: Integer; const Value: TClaim); public destructor Destroy; override; function Add(Item: TClaim): Integer; procedure Delete(Index: Integer); procedure MoveToLast;
property Items[Index: Integer]: TClaim read GetItems write SetItems; default; end;
TProcessor=class private FMaxClaims: Integer; FTurn: TTurn; procedure SetMaxClaims(const Value: Integer); procedure ExecStep(Claim: TClaim); public constructor Create(MaxClaims: Integer=20); destructor Destroy; override; procedure AddClaim(Claim: TClaim); procedure Execute;
property MaxClaims: Integer read FMaxClaims write SetMaxClaims; end;
TForm1 = class(TForm) end;
var Form1: TForm1;
implementation
{$R *.dfm}
{ TClaim }
constructor TClaim.Create; begin FCount := Random(7)+1; end;
function TClaim.DecCount: Integer; begin if FCount>0 then Dec(FCount); Result := FCount; end;
{ TTurn }
function TTurn.Add(Item: TClaim): Integer; begin Result := Add(Item); end;
procedure TTurn.Delete(Index: Integer); begin TClaim(Items[Index]).Free; inherited Delete(Index); end;
destructor TTurn.Destroy; begin while Count>0 do Delete(0); inherited; end;
function TTurn.GetItems(Index: Integer): TClaim; begin Result := Items[Index]; end;
procedure TTurn.MoveToLast; begin Add(Items[0]); inherited Delete(0); end;
procedure TTurn.SetItems(Index: Integer; const Value: TClaim); begin Items[Index] := Value; end;
{ TProcessor }
procedure TProcessor.AddClaim(Claim: TClaim); begin if FTurn.Count=FMaxClaims then raise Exception.Create('Нельзя добавить новую заявку, превышено максимальное число заявок - '+IntToStr(FMaxClaims)); FTurn.Add(Claim); end;
constructor TProcessor.Create(MaxClaims: Integer); begin FMaxClaims := MaxClaims; FTurn := TTurn.Create; end;
destructor TProcessor.Destroy; begin FTurn.Free; inherited; end;
procedure TProcessor.ExecStep(Claim: TClaim); begin Claim.DecCount; end;
procedure TProcessor.Execute; var i: Integer; begin while FTurn.Count>0 do begin for i := 0 to MaxSteps-1 do begin ExecStep(FTurn[0]); if FTurn[0].Count=0 then break; end; if FTurn[0].Count=0 then FTurn.Delete(0) else FTurn.MoveToLast; end; end;
procedure TProcessor.SetMaxClaims(const Value: Integer); begin if Value=FMaxClaims then Exit; if (FTurn.Count=FMaxClaims) and (Value<FMaxClaims) then raise Exception.Create('Нельзя установить максимальное число заявок в '+IntToStr(Value)+ ', число текущих заявок в обработке - '+IntToStr(FTurn.Count)); FMaxClaims := Value; end;
initialization Randomize; end.
|
А в качестве тренировки предлагаю самому добавить в TProcessor установку разиера кванта (вместо константы MaxSteps), а также отладить и исправить все это дело как нужно. |