Разработка программы "Генеалогическое дерево", страница 4

  i: Integer;

  List: TObjectList;

begin

  Result:= TObjectList.Create(false);

  List:= getChildrensNotParentOfSuprug();

  for i:= 0 to Childrens.Count - 1 do

    if (List.IndexOf(Childrens.Items[i]) = -1) and

       ((sex and (Result.IndexOf((Childrens.Items[i] as TPeople).Mother) = -1)) or

       (not sex and (Result.IndexOf((Childrens.Items[i] as TPeople).Father) = - 1))) then

      if Sex then

        Result.Add((Childrens.Items[i] as TPeople).Mother)

      else

        Result.Add((Childrens.Items[i] as TPeople).Father);

end;

function TPeople.getProchieMarriage(): TObjectList;

var

  i: Integer;

  list: TObjectList;

begin

  Result:= TObjectList.Create(false);

  List:= getChildrensOfMarriage();

  for i:= 0 to Marriages.Count - 1 do

    if (((Marriages.Items[i] as TMarriage).Husband = self) and (List.IndexOf((Marriages.Items[i] as TMarriage).Wife) = -1)) or

       (((Marriages.Items[i] as TMarriage).Wife = self) and (List.IndexOf((Marriages.Items[i] as TMarriage).Husband) = -1)) then

      if (Marriages.Items[i] as TMarriage).Husband = self then

        Result.Add((Marriages.Items[i] as TMarriage).Wife)

      else

        Result.Add((Marriages.Items[i] as TMarriage).Husband);

end;

function TPeople.ValidateBrother(People: TPeople): Boolean;

var

  i: Integer;

begin

  Result:= false;

  if (Father <> nil) and (Father.Childrens.Count > 1) then

    for i:= 0 to Father.Childrens.Count - 1 do

      if (Father.Childrens.Items[i] as TPeople) = People then

      begin

        Result:= true;

        Break;

      end;

  if (Mother <> nil) and (Mother.Childrens.Count > 1) then

    for i:= 0 to Mother.Childrens.Count - 1 do

      if (Mother.Childrens.Items[i] as TPeople) = People then

      begin

        Result:= true;

        Break;

      end;

end;

function TPeople.ValidatePara(People: TPeople): Boolean;

var

  i: Integer;

begin

  Result:= false;

  for i:= 0 to Marriages.Count - 1 do

    if ((Marriages.Items[i] as TMarriage).Husband = People) or

       ((Marriages.Items[i] as TMarriage).Wife = People) then

    begin

      Result:= true;

      Break;

    end;   

end;

{ TManager }

function TManager.AddMarriage(RMarriage: TRMarriage): TMarriage;

begin

  Result:= TMarriage.Create(RMarriage);

  Marriages.Add(Result);

end;

function TManager.AddPeople(RPeople: TRPeople): TPeople;

begin

  Result:= TPeople.Create(RPeople);

  Peoples.Add(Result);

end;

procedure TManager.Clear;

begin

  Peoples.Clear;

  Marriages.Clear;

end;

constructor TManager.Create;

begin

  inherited Create();

  Peoples:= TObjectList.Create(true);

  Marriages:= TObjectList.Create(true);

end;

procedure TManager.DeleteMarriage(Marriage: TMarriage);

begin

  Marriages.Delete(Marriages.IndexOf(Marriage));

end;

procedure TManager.DeletePeople(People: TPeople);

var

  i, Item: Integer;

begin

  Item:= Peoples.IndexOf(People);

  if Item > -1 then

  begin

    Peoples.Delete(Item);

    //Очищаем все плохие браки

    for i:= 0 to Marriages.Count - 1 do

      if ((Marriages.First as TMarriage).Husband = nil) or ((Marriages.First as TMarriage).Wife = nil) then

        DeleteMarriage((Marriages.First as TMarriage));

  end;

end;

destructor TManager.Destroy;

begin

  Clear;

  inherited Destroy();

end;

procedure TManager.Save(PFiles: string);

var

  RSLPeople: TRSLPeople;

  RSLMarriage: TRSLMarriage;

  RManager: TRManager;

  F: file;

  i: Integer;

begin

  AssignFile(F, PFiles + '.gf');

  Rewrite(F, 1);

  RManager.CountPeople:= Peoples.Count;

  RManager.CountMarriage:= Marriages.Count;

  BlockWrite(F, RManager, sizeof(RManager));

  for i:= 0 to Peoples.Count - 1 do

  begin

    with (Peoples.Items[i] as TPeople) do

    begin

      RSLPeople.Family:= Family;

      RSLPeople.FamilyFirst:= FamilyFirst;

      RSLPeople.Name:= Name;

      RSLPeople.Patronymic:= Patronymic;

      RSLPeople.Sex:= Sex;

      RSLPeople.Father:= Peoples.IndexOf(Father);

      RSLPeople.Mother:= Peoples.IndexOf(Mother);

      RSLPeople.PlaceOfBirth:= PlaceOfBirth;

      RSLPeople.PlaceOfDeath:= PlaceOfDeath;

      RSLPeople.DateOfBirth:= DateOfBirth;

      RSLPeople.DateOfDeath:= DateOfDeath;

      RSLPeople.Commentary:= Commentary;

    end;

    BlockWrite(F, RSLPeople, sizeof(RSLPeople));

  end;

  for i:= 0 to Marriages.Count - 1 do

  begin

    with (Marriages.Items[i] as TMarriage) do

    begin

      RSLMarriage.Family:= Family;

      RSLMarriage.Husband:= Peoples.IndexOf(Husband);

      RSLMarriage.Wife:= Peoples.IndexOf(Wife);

      RSLMarriage.DateOfStart:= DateOfStart;

      RSLMarriage.DateOfEnd:= DateOfEnd;

      RSLMarriage.Commentary:= Commentary;

    end;

    BlockWrite(F, RSLMarriage, sizeof(RSLMarriage));

  end;

  CloseFile(F);

end;

procedure TManager.Load(PFiles: string);

var

  RPeople: TRPeople;

  LPeoples: array of TRSLPeople;

  RMarriage: TRMarriage;

  RSLMarriage: TRSLMarriage;

  RManager: TRManager;

  F: file;

  i: Integer;

begin

  Clear;

  AssignFile(F, PFiles);

  Reset(F, 1);

  BlockRead(F, RManager, sizeof(RManager));

  RPeople.Father:= nil;

  RPeople.Mother:= nil;

  for i:= 0 to RManager.CountPeople - 1 do

  begin

    SetLength(LPeoples, i + 1);

    BlockRead(F, LPeoples[i], sizeof(LPeoples[i]));

    with LPeoples[i] do

    begin

      RPeople.Family:= Family;

      RPeople.FamilyFirst:= FamilyFirst;

      RPeople.Name:= Name;

      RPeople.Patronymic:= Patronymic;

      RPeople.Sex:= Sex;

      RPeople.PlaceOfBirth:= PlaceOfBirth;

      RPeople.PlaceOfDeath:= PlaceOfDeath;

      RPeople.DateOfBirth:= DateOfBirth;

      RPeople.DateOfDeath:= DateOfDeath;

      RPeople.Commentary:= Commentary;

      AddPeople(RPeople);

    end;

  end;

  for i:= 0 to RManager.CountPeople - 1 do

  begin

    if LPeoples[i].Father > -1 then