Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#6

AW: Record im Array nutzen? - Sinnvollere Wege?

  Alt 10. Jul 2015, 19:33
Ich hätte das mit Klassen so aufgebaut
Delphi-Quellcode:
unit Unit1;

interface

uses
  System.Classes,
  System.DateUtils,
  System.Generics.Collections,
  System.TimeSpan;

type
{$SCOPEDENUMS ON}
  TWeekDay = ( Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday );
{$SCOPEDENUMS OFF}

  TAvailibility = class( TPersistent )
  private
    FFrom: TTime;
    FDuration: TTimeSpan;
    FIsAvailable: Boolean;
    procedure SetFrom( const Value: TTime );
    procedure AssignToAvailibility( Dest: TAvailibility );
    procedure SetIsAvailable( const Value: Boolean );
  protected
    procedure AssignTo( Dest: TPersistent ); override;
  public
    function Equals( Obj: TObject ): Boolean; override;

    property IsAvailable: Boolean read FIsAvailable write SetIsAvailable;
    property From: TTime read FFrom write SetFrom;
    property Duration: TTimeSpan read FDuration write FDuration;
  end;

  TEmployee = class( TPersistent )
  private
    FAvailibility: TObjectDictionary<TWeekDay, TAvailibility>;
    FCardID: Integer;
    FPrename: string;
    FSurname: string;
    FWorkstation: string;
    FPosition: string;
    FBirthday: TDate;
    function GetAvailibility( Weekday: TWeekDay ): TAvailibility;
    procedure SetBirthday( const Value: TDate );
    procedure AssignToEmployee( Dest: TEmployee );
    procedure SetAvailibility( Weekday: TWeekDay; const Value: TAvailibility );
  protected
    procedure AssignTo( Dest: TPersistent ); override;
  public
    constructor Create;
    destructor Destroy; override;

    function Equals( Obj: TObject ): Boolean; override;

    property CardID: Integer read FCardID write FCardID;
    property Prename: string read FPrename write FPrename;
    property Surname: string read FSurname write FSurname;
    property Workstation: string read FWorkstation write FWorkstation;
    property Position: string read FPosition write FPosition;
    property Birthday: TDate read FBirthday write SetBirthday;
    property Availibility[ Weekday: TWeekDay ]: TAvailibility read GetAvailibility write SetAvailibility;
  end;

implementation

{ TEmployee }

procedure TEmployee.AssignTo( Dest: TPersistent );
begin
  if ( Dest is TEmployee ) then
    AssignToEmployee( Dest as TEmployee )
  else
    inherited;
end;

procedure TEmployee.AssignToEmployee( Dest: TEmployee );
var
  LWeekDay: TWeekDay;
begin
  Dest.FCardID := Self.FCardID;
  Dest.FPrename := Self.FPrename;
  Dest.FSurname := Self.FSurname;
  Dest.FWorkstation := Self.FWorkstation;
  Dest.FPosition := Self.FPosition;
  Dest.FBirthday := Self.FBirthday;
  for LWeekDay := low( TWeekDay ) to high( TWeekDay ) do
    Dest.SetAvailibility( LWeekDay, Self.GetAvailibility( LWeekDay ) );
end;

constructor TEmployee.Create;
var
  LWeekDay: TWeekDay;
begin
  inherited Create;
  FAvailibility := TObjectDictionary<TWeekDay, TAvailibility>.Create( [ doOwnsValues ] );
  for LWeekDay := low( TWeekDay ) to high( TWeekDay ) do
  begin
    FAvailibility.Add( LWeekDay, TAvailibility.Create );
  end;
end;

destructor TEmployee.Destroy;
begin
  FAvailibility.Free;
  inherited;
end;

function TEmployee.Equals( Obj: TObject ): Boolean;
begin
  Result := ( Self = Obj ) or Assigned( Obj ) and ( Obj is TEmployee ) and ( TEmployee( Obj ).FCardID = Self.FCardID );
end;

function TEmployee.GetAvailibility( Weekday: TWeekDay ): TAvailibility;
begin
  Result := FAvailibility[ Weekday ];
end;

procedure TEmployee.SetAvailibility( Weekday: TWeekDay; const Value: TAvailibility );
begin
  FAvailibility[ Weekday ].Assign( Value );
end;

procedure TEmployee.SetBirthday( const Value: TDate );
begin
  FBirthday := DateOf( Value );
end;

{ TAvailibility }

procedure TAvailibility.AssignTo( Dest: TPersistent );
begin
  if ( Dest is TAvailibility ) then
    AssignToAvailibility( Dest as TAvailibility )
  else
    inherited;
end;

procedure TAvailibility.AssignToAvailibility( Dest: TAvailibility );
begin
  Dest.FFrom := Self.FFrom;
  Dest.FDuration := Self.FDuration;
end;

function TAvailibility.Equals( Obj: TObject ): Boolean;
begin
  Result := ( Self = Obj ) or Assigned( Obj ) and ( Obj is TAvailibility ) and (
    {} ( TAvailibility( Obj ).FIsAvailable = Self.FIsAvailable ) or
    {} ( ( TAvailibility( Obj ).FFrom = Self.FFrom ) and ( TAvailibility( Obj ).FDuration = Self.FDuration ) ) );

end;

procedure TAvailibility.SetFrom( const Value: TTime );
begin
  FFrom := TimeOf( Value );
end;

procedure TAvailibility.SetIsAvailable( const Value: Boolean );
begin
  FIsAvailable := Value;
end;

end.
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat