Advanced Databases
Advanced Databases
unit PropertyManagement;
interface
uses
SysUtils, Classes;
type
TAgent = class
private
FName: string;
FProperties: Integer;
public
constructor Create(const AName:
string);
property Name: string read FName;
property Properties: Integer read
FProperties;
end;
TProperty = class
private
FAgent: TAgent;
public
constructor Create(const AAgent:
TAgent);
property Agent: TAgent read FAgent;
end;
TPropertyTable = class
private
FProperties: TList;
FAgents: TList;
public
constructor Create;
destructor Destroy; override;
procedure AddProperty(const AAgent:
TAgent);
function GetAgentWithLeastProperties:
TAgent;
end;
implementation
constructor TAgent.Create(const AName:
string);
begin
FName := AName;
FProperties := 0;
end;
constructor TProperty.Create(const
AAgent: TAgent);
begin
FAgent := AAgent;
AAgent.FProperties :=
AAgent.FProperties + 1;
end;
constructor TPropertyTable.Create;
begin
FProperties := TList.Create;
FAgents := TList.Create;
end;
destructor TPropertyTable.Destroy;
var
I: Integer;
begin
for I := 0 to FProperties.Count - 1 do
TObject(FProperties[I]).Free;
FProperties.Free;
FAgents.Free;
inherited Destroy;
end;
procedure
TPropertyTable.AddProperty(const
AAgent: TAgent);
var
NewProperty: TProperty;
begin
NewProperty :=
TProperty.Create(AAgent);
FProperties.Add(NewProperty);
end;
function
TPropertyTable.GetAgentWithLeastPropert
ies: TAgent;
var
I: Integer;
MinProperties: Integer;
MinAgent: TAgent;
begin
MinProperties := MaxInt;
MinAgent := nil;
for I := 0 to FAgents.Count - 1 do
begin
if TAgent(FAgents[I]).Properties <
MinProperties then
begin
MinProperties :=
TAgent(FAgents[I]).Properties;
MinAgent := TAgent(FAgents[I]);
end;
end;
Result := MinAgent;
end;
end.
```
In this code, we have three classes:
`TAgent`, `TProperty`, and
`TPropertyTable`. The `TAgent` class
represents an agent with a name and a
number of properties. The `TProperty`
class represents a property assigned to an
agent. The `TPropertyTable` class
manages the properties and agents.
To add a property to the property table and
automatically assign it to the agent with the
least number of properties, you can use the
following code:
```delphi
var
PropertyTable: TPropertyTable;
Agent: TAgent;
begin
PropertyTable := TPropertyTable.Create;
// Create agents
Agent := TAgent.Create('Agent A');
PropertyTable.AddProperty(Agent);
Agent := TAgent.Create('Agent B');
PropertyTable.AddProperty(Agent);
Agent := TAgent.Create('Agent C');
PropertyTable.AddProperty(Agent);
// Add property and assign it to the agent
with the least number of properties
Agent :=
PropertyTable.GetAgentWithLeastProperti
es;
PropertyTable.Add