Программирование - это просто
Advertisement
arrow arrow , , arrow 17. Delphi: TESBAccount (, , ).
07.02.2023 .
1:
C#
Delphi
Web-
Excel-
.
17. Delphi: TESBAccount (, , ). E-mail
megabax   
25.03.2010 .
New Page 1

: TESBAccount (, , ).

.

.

" Delphi", . , . , .

.

() . . ( 14, 15, 16), , . . TESBAccount. :

//
TESBAccount=class(TESBBaseClass)
protected
   FCash:double;
   FExceptionCash:boolean;
   FInstruments:TList; //
   FTechCosts:double; // (+), %
   function Get(index:integer):TESBInstrument;
   function GetCount:integer;
public
   property Cash:double read FCash; //
   property ExceptionCash:boolean read FExceptionCash write FExceptionCash;
   property Instruments[index:integer]:TESBInstrument read Get; default;
   property TechCosts:double read FTechCosts write FTechCosts;
   property InstrumetnsCount:integer read GetCount;
   constructor Create;
   procedure Replenish(ACash:double); //
   procedure Withdraw(ACash:double); //
   procedure Serialize(AStream:TStream); override;
   procedure Unserialize(AStream:TStream); override;
   procedure AddInstrument(item:TESBInstrument);
   procedure DeleteInstrument(index:integer);
   procedure Buy(index,count:integer);
   procedure Sell(index,count:integer);
   function GetCashLimit:double; 
   destructor Destroy; virtual;
end;

, Instruments, private FInstruments Get . read only, .

Instruments , AddInstrument DeleteInstrument. InstrumetnsCount, read only GetCount.

Buy Sell . .

, , , ( TechCosts, private FTechCosts).

. . Get GetCount , FInstruments:

function TESBAccount.Get(index:integer):TESBInstrument;
begin
      Result:=FInstruments[index];
end;

function TESBAccount.GetCount:integer;
begin
       Result:=FInstruments.Count;
end;

Crate, FInstruments, :

constructor TESBAccount.Create;
begin
    inherited Create;
    FCash:=0;
    FExceptionCash:=true;
    FInstruments:=TList.Create;
end;

Serialize Unserialize , , " ", , "".

  : AddInstrument DeleteInstrument, :

procedure TESBAccount.AddInstrument(item:TESBInstrument);
begin
     FInstruments.Add(item);
end;

procedure TESBAccount.DeleteInstrument(index:integer);
begin
    FInstruments.Delete(index);
end;

Buy:

procedure TESBAccount.Buy(index,count:integer);
var ACash,CashLimit:double;
begin
     CashLimit:=GetCashLimit;
     ACash:=TESBInstrument(FInstruments[index]).CurrentPrice*Count*(1+FTechCosts/100);
     if FCash-ACash<CashLimit then raise Exception.Create('TESBAccount.Buy - ');
     TESBInstrument(FInstruments[index]).Buy(count);
     FCash:=FCash-ACash;
end;

, : ( , - . , , ). , , , . , Buy TESBInstrument, 16.

Sell:

procedure TESBAccount.Sell(index,count:integer);
var ACash:double;
begin
     ACash:=TESBInstrument(FInstruments[index]).CurrentPrice*Count*(1-FTechCosts/100);
     TESBInstrument(FInstruments[index]).Sell(count);
     FCash:=FCash+ACash;
end;

- , sell. , 16, .

GetCashLimit. , :

function TESBAccount.GetCashLimit:double;
begin
      Result:=0;
end;

:

destructor TESBAccount.Destroy;
var i,cn:integer;
begin
    cn:=FInstruments.Count-1;
    for i:=0 to cn do
    begin
        FreeAndNil(TESBInstrument(FInstruments[i]).FPriceSource);
        TESBInstrument(FInstruments[i]).Free;
    end;
    FreeAndNil(FInstruments);
    inherited Destroy;
end;

: FInstruments, TPASSPriceSource, , , . .

, . , , . *:

 :   TESBAccount (, , ).

(TStringGrid) sgInstrs, Additional*:

 :   TESBAccount (, , ).

. (TButton) Standard, , (TLabel)*:

 :   (, , ).

(Caption) ( Name):

Caption Name
btnAdd
btnBuy
btnSell
. btnNext

edCount . - . lbPrice lbCash.

. , *:

 :   Account (, , ).

:

TfrmStockBot = class(TForm)
   MainMenu: TMainMenu;
   itFile: TMenuItem;
   itSave: TMenuItem;
   itOpen: TMenuItem;
   edCount: TEdit;
   btnBuy: TButton;
   btnSell: TButton;
   SaveDialog: TSaveDialog;
   OpenDialog: TOpenDialog;
   btnNext: TButton;
   itOpenPrices: TMenuItem;
   sgInstrs: TStringGrid;
   btnAdd: TBitBtn;
   lbCash: TLabel;
   procedure FormCreate(Sender: TObject);
   procedure FormDestroy(Sender: TObject);
   procedure btnBuyClick(Sender: TObject);
   procedure btnSellClick(Sender: TObject);
   procedure itSaveClick(Sender: TObject);
   procedure itOpenClick(Sender: TObject);
   procedure itOpenPricesClick(Sender: TObject);
   procedure btnNextClick(Sender: TObject);
   procedure btnAddClick(Sender: TObject);
private
  { Private declarations }
  Account:TESBAccount;
  procedure CreateGrid;
  procedure RedrawGrid;
public
  { Public declarations }
end;
 

CreateGrid:

procedure TfrmStockBot.CreateGrid;
begin
    sgInstrs.Cols[0].Text:='';
    sgInstrs.Cols[1].Text:='';
    sgInstrs.Cols[2].Text:='';
    sgInstrs.Cols[3].Text:='';
    sgInstrs.Cols[4].Text:='';

    sgInstrs.ColWidths[1]:=140;
end;

- .

, RedrawGrid:

procedure TfrmStockBot.RedrawGrid;
var i,cn:integer;
begin
     cn:=Account.InstrumetnsCount;
     sgInstrs.RowCount:=cn+1;
     for i:=1 to cn do
     begin
            sgInstrs.Cells[0,i]:=IntToStr(i);
            sgInstrs.Cells[1,i]:=Account.Instruments[i-1].Name;
            sgInstrs.Cells[2,i]:=FloatToStr(Account.Instruments[i-1].CurrentPrice);
            sgInstrs.Cells[3,i]:=IntToStr(Account.Instruments[i-1].Count);
            sgInstrs.Cells[4,i]:=FloatToStr(Account.Instruments[i-1].Count*Account.Instruments[i-1].CurrentPrice);
     end;
     lbCash.Caption:=' '+FloatToStr(Account.Cash);
end;

TStringGrid , . .

. OnClick btnAdd - " ":

procedure TfrmStockBot.btnAddClick(Sender: TObject);
var frm:TfrmInstrumEdit;
      instr:TESBInstrument;
      PriceSource:TPASSPriceSource;
begin
      frm:=TfrmInstrumEdit.Create(self);
      if frm.ShowModal=mrOk then
      begin
         instr:=TESBInstrument.Create;
         PriceSource:=TPASSPriceSource.Create('',false);
         PriceSource.LoadDataFromTextFile(frm.OpenDialog.FileName);
         PriceSource.First;
         instr.PriceSource:=PriceSource;
         instr.Name:=frm.edName.Text;
         instr.CanShort:=frm.chbCanShort.Checked;
         Account.AddInstrument(instr);
         RedrawGrid;
     end;
end;

, , ( , ,  , ). TPASSPRiceSource, TESBInstrument, , , Account. RedrawGrid.

, TfrmInstrumEdit - . , , "File" -> "New" -> "Form"*:

 :   TESBAccount (, , ).

InstrumEdit, Name InstrumEdit*

 :   TESBAccount (, , ).

*:

 :   TESBAccount (, , ).

( Name)

, TEdit edName
, TEdit edFileName
, TOpenDialog OpenDialog
btnFileDilaog
" " chbCanShort
"" btnOK

*:

http://easyprog.ru/index.php?option=com_content&task=view&id=241&Itemid=29

, , .

"". OnClick

procedure TfrmStockBot.btnBuyClick(Sender: TObject);
begin
     Account.Buy(sgInstrs.Row-1,StrToInt(edCount.Text));
     RedrawGrid;
end;

, Buy TESBAccount .

"". :

procedure TfrmStockBot.btnSellClick(Sender: TObject);
begin
     Account.Sell(sgInstrs.Row-1,StrToInt(edCount.Text));
     RedrawGrid;
end;

". ". :

procedure TfrmStockBot.btnNextClick(Sender: TObject);
begin
     Account.Instruments[sgInstrs.Row-1].Next;
      RedrawGrid;
end;

, . :

1. , " ".

2. "", "" " ":

 :   TESBAccount (, , ).

() , , ". " , TESBAccount .

.


, * ,   "Delphi", "Borland Software Corporation, (a Micro Focus Company). 

 

 

( 17.07.2013 . )
 
« .   . »
 
© 2023 Программирование - это просто
Joomla! - , GNU/GPL.
© 2005-2008 Joom.Ru - Joomla!
Design by Mamboteam.com | Powered by Mambobanner.de
  .