unit Un_network;

interface
  uses
  Un_company, Un_Abonent, Un_list1;
  type TNetwork=class
                private
                FNamenetwork:string;
                FL:TList;
                public
                constructor Create (Anamenetwork:string);
                destructor Destroy; override;
                procedure Print;
                function Add_network(Anamecompany:string;Aabonent:TAbonent):boolean;
                function del(Anamecompany:string;Anumberphone:TAbonent):Boolean;
                property namenetwork:string read Fnamenetwork;
                property L:Tlist read FL;
                end;

implementation

  constructor TNetwork.Create;
  begin
    inherited Create;
    FNamenetwork:=Anamenetwork;
    InitLL(FL);
  end;

  procedure TNetwork.Print;
  begin
    Writeln(#10,FNamenetwork,'.');
    Printll(FL);
  end;

  destructor TNetwork.Destroy;
  begin
    ClearLL(FL);
    inherited Destroy
  end;

 function TNetwork.del(Anamecompany:string;Anumberphone:TAbonent):Boolean;
 var p:TList;
 begin
    p:=Found_beforeLL(FL,Anamecompany);
    if(p^.next = nil) or  (P^.next^.company.comparecompany(Anamecompany)>0) then result:=false
                                                                            else // компания есть,ищем абонента по номеру телефона
                                                                            begin
                                                                              result:=true;
                                                                              p^.next^.Company.Del_ab(Anumberphone);
                                                                            end;
   if P^.next^.Company.Empty then delete_afterLL(p)
 end;

 function TNetwork.Add_network(Anamecompany:string;Aabonent:TAbonent):boolean;
 var p:TList;
 begin
   p:=Found_beforeLL(fl,Anamecompany);
   if(p^.next = nil) or  (P^.next^.company.comparecompany(Anamecompany)>0) then//такой компании нет,создаем новую компанию
   Add_afterLL(p,anamecompany);
   p:=p^.next; // переходим к найденной компании
   if p^.Company.Found(Aabonent)then Aabonent.destroy else p^.Company.Add_ab(Aabonent);
 end;

end.
