Saturday, April 16, 2011

Delphi 7 : Memanfaatkan ItemIndex dalam seleksi Case-Of

Semester baru, mata kuliah baru. Dan kali ini saya mengambil matkul Delphi 7. Well, sebenarnya saya sudah pernah pake Delphi sekitar 9 tahun yang lalu, enak sih pake bahasa Pascal, tapi karena pada masa itu GUI Delphi sangat jelek dan VB6 lebih menggiurkan, so i had to say goodbye to Delphi. And now i have to learn it from the scratch :-)

Oke, mockup latihan serta rule yang diberikan dosen sangat simpel seperti ini :

Oke source code-nya serta bentuk form-nya di Delphi seperti ini :



unit Latihan3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    txtNama: TEdit;
    txtGapok: TEdit;
    txtTunjJab: TEdit;
    GroupBox3: TGroupBox;
    Label6: TLabel;
    Label7: TLabel;
    txtPPH: TEdit;
    txtTotal: TEdit;
    btnHitung: TButton;
    btnEnd: TButton;
    txtGolongan: TEdit;
    txtJabatan: TEdit;
    procedure btnHitungClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnHitungClick(Sender: TObject);
var
  gapok, tunj, pph, total : real;

begin
   IF UPPERCASE(txtJabatan.Text) = 'STAFF' THEN
      begin
        if UPPERCASE(txtGolongan.Text) = 'A' then
            begin
              gapok := 1000000;
              tunj  := 700000;
            end
        else
        if UPPERCASE(txtGolongan.Text) = 'B' then
            begin
              gapok := 1300000;
              tunj  := 900000;
            end
        else
        if UPPERCASE(txtGolongan.Text) = 'C' then
            begin
              gapok := 1600000;
              tunj  := 1200000;
            end
        else
            begin
              gapok := 2000000;
              tunj  := 1500000;
            end;
      end
   ELSE
   if UPPERCASE(txtJabatan.Text) = 'MANAGER' then
      begin
        if UPPERCASE(txtGolongan.Text) = 'A' then
            begin
              gapok := 2500000;
              tunj  := 1700000;
            end
        else
        if UPPERCASE(txtGolongan.Text) = 'B' then
            begin
              gapok := 3000000;
              tunj  := 1900000;
            end
        else
        if UPPERCASE(txtGolongan.Text) = 'C' then
            begin
              gapok := 3500000;
              tunj  := 2000000;
            end
        else
            begin
              gapok := 4000000;
              tunj  := 3000000;
            end;
      end;
      total := gapok + tunj;
      pph := 0.05 * total;
      txtgapok.Text := floattostr(gapok);
      txttunjjab.Text := floattostr(tunj);
      txtpph.Text := floattostr(pph);
      txttotal.Text := floattostr(total-pph);
    end;
end.

Dari code beserta form diatas, kita bisa melihat beberapa kondisi IF-THEN yang tidak optimized dan UI yang rentan akan kesalahan entry. Misalnya bagaimana jika user memasukkan Jabatan selain Staff & Manager, atau Golongan selain A, B, C, D.

Dengan sedikit modifikasi, kita bisa memanfaatkan komponen RadioGroup untuk  meminimalisir kesalahan user :



Source code-nya :

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    rgGolongan: TRadioGroup;
    rgJabatan: TRadioGroup;
    txtNama: TEdit;
    txtGapok: TEdit;
    txtTunj: TEdit;
    GroupBox3: TGroupBox;
    Label4: TLabel;
    txtPPH: TEdit;
    Label5: TLabel;
    txtTotal: TEdit;
    btnClose: TButton;
    procedure btnCloseClick(Sender: TObject);
    procedure rgGolonganClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.btnCloseClick(Sender: TObject);
begin
  close;
end;

procedure TForm1.rgGolonganClick(Sender: TObject);
var
    gapok, tunj, pph, total : real;
begin
    IF rgjabatan.ItemIndex = 0 THEN
    begin
        case rggolongan.ItemIndex of
        0 : begin
              gapok := 1000000;
              tunj  := 700000;
            end;
        1 : begin
              gapok := 1300000;
              tunj  := 900000;
            end;
        2 : begin
              gapok := 1600000;
              tunj  := 1200000;
            end;
        3 : begin
              gapok := 2000000;
              tunj  := 1500000;
            end;
        end;
    end
    ELSE
    begin
        case rggolongan.ItemIndex of
        0 : begin
              gapok := 2500000;
              tunj  := 1700000;
            end;
        1 : begin
              gapok := 3000000;
              tunj  := 1900000;
            end;
        2 : begin
              gapok := 3500000;
              tunj  := 2000000;
            end;
        3 : begin
              gapok := 4000000;
              tunj  := 3000000;
            end;
        end;
    end;
    
    total := gapok + tunj;
    pph   := 0.05 * total;
    txtgapok.Text := formatcurr('###,###',gapok);
    txttunj.Text  := formatcurr('###,###',tunj);
    txtpph.Text   := formatcurr('###,###',pph);
    txttotal.Text := formatcurr('###,###',total-pph);

end;

end.

Untuk code diatas, saya mengganti IF-THEN dengan CASE-OF yang lebih simpel dan mudah dipahami. Karena limitasi CASE-OF di Delphi hanya mengenal integer, kita bisa memanfaatkan ItemIndex dari control RadioGroup (rgGolongan)

Oya, kita perlu mengaitkan event rgGolonganClick ke rgJabatan.OnClick supaya melakukan prosedur yang sama ketika di-klik dan Form1.OnActivate supaya pada saat pertama kali aplikasi load, perhitungan akan dilakukan.


See? Ternyata Delphi tidak sulit. Thanks to Eko Wahyudiharto for the help :-)

---

4 comments:

  1. Ho ho ho.. U're welcome :)

    Once u familiar with Delphi, it's a "real" bridge to C++ Builder.
    Once u familiar with C++, it's a "real" bridge to PHP (OOP).

    * ... And it's all about a "real" true story :)

    ReplyDelete
  2. i still can't get why peoples said PHP is OOP. I don't see any there. cmiiw

    ReplyDelete
  3. Once you've seeing a pointer over the codes, that's OOP will look-a-like...

    * Remember about pointer lessons from your programming colleges?

    ReplyDelete
  4. Honestly,i have more interests in vb6.net than others,

    ReplyDelete

Leave your comments with attitude please. This blog does DOFOLLOW :-)