| 
             
 OWN CREATED 
            UNIT (clsTMarks.pas)
 unit clsTMarks;
 
 interface
 
 uses Sysutils;         
            //Added because IntToStr is used in this 
            unit
 
 type
 TMarks = class          
            //Defining class TMarks
 private
 Surname : String;
 Test1 : Integer;
 Test2 : Integer;
 Test3 : Integer;
 public
 constructor Create; overload;
 constructor Create(pSurname : String; pTest1, 
            pTest2, pTest3 : Integer); overload;
 function getAverage : String;
 function showMarks : String;
 end;
 
 var
 iAverage : Integer;       
            //Variable used in this unit
 
 implementation
 
 constructor TMarks.Create;
 begin
 Surname := '';
 Test1 := 0;
 Test2 := 0;
 Test3 := 0;
 end;
 
 constructor TMarks.Create(pSurname : String; pTest1, pTest2, pTest3 
            : Integer);
 begin
 Surname := pSurname;     
            //Puts values into this class
 Test1 := pTest1;
 Test2 := pTest2;
 Test3 := pTest3;
 end;
 
 function TMarks.getAverage: String;
 begin
 iAverage := (Test1 + Test2 + Test3) DIV 3;  
            //Calculates average
 getAverage := Surname + #9 + IntToStr(Test1) + #9 + IntToStr(Test2) + #9 
            +
 IntToStr(Test3) + #9 + IntToStr(iAverage);
 end;
 
 function TMarks.showMarks: String;
 begin
 showMarks := Surname + #9 + IntToStr(Test1) + #9 + IntToStr(Test2) + #9 +
 IntToStr(Test3);                     
            //Display marks note the tabs (#9)
 end;
 
 end.
 
 MAIN UNIT 
            (unit1.pas)
 unit Unit1;
 
 interface
 
 uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, 
            Forms,
 Dialogs, StdCtrls, ComCtrls, Buttons;
 
 type
 TForm1 = class(TForm)
 RichEdit1: TRichEdit;
 Button1: TButton;
 Button2: TButton;
 BitBtn1: TBitBtn;
 procedure FormActivate(Sender: TObject);
 procedure Button1Click(Sender: TObject);
 procedure Button2Click(Sender: TObject);
 procedure BitBtn1Click(Sender: TObject);
 private
 { Private declarations }
 public
 { Public declarations }
 end;
 
 var
 Form1: TForm1;
 
 implementation
 
 uses clsTMarks;       
            //Indicating that own created unit is 
            used
 
 var
 arrMarks : array[1..42] of TMarks;   
            //Array that keeps objects of class TMarks
 iCount : Integer;                   
            //Counter keeping track of number of 
            entries
 
 {$R *.dfm}
 
 procedure TForm1.FormActivate(Sender: TObject);
 var
 fFile : TextFile;
 sTemp, sSurname : String;
 iC, iTest1, iTest2, iTest3, iH : Integer;
 begin
 Richedit1.Paragraph.TabCount := 5;   
            //Adds tabs for neat layout in RichEdit
 Richedit1.Paragraph.Tab[1] := 100;
 Richedit1.Paragraph.Tab[2] := 200;
 Richedit1.Paragraph.Tab[3] := 300;
 Richedit1.Paragraph.Tab[4] := 400;
 Richedit1.Paragraph.Tab[5] := 500;
 iCount := 0;                       
            //Initial value for counter
 AssignFile(fFile, 'Marks.txt');    
            //Assign text file
 Reset(fFile);                      
            //Open text file
 While NOT eof(fFile) do            
            //Reads text file from beginning to end
 begin
 inc(iCount);
 Readln(fFile,sTemp);
 iH := pos('#',sTemp);           
            //This and next 12 lines separate fields
 sSurname := copy(sTemp, 1, iH-1);
 Delete(sTemp,1,iH);
 iH := pos('#',sTemp);
 iTest1 := StrToInt(copy(sTemp, 1, iH-1));
 Delete(sTemp,1,iH);
 iH := pos('#',sTemp);
 iTest2 := StrToInt(copy(sTemp, 1, iH-1));
 Delete(sTemp,1,iH);
 iH := pos('#',sTemp);
 iTest3 := StrToInt(copy(sTemp, 1, iH-1));
 Delete(sTemp,1,iH);
 arrMarks[iCount] := 
            TMarks.Create(sSurname,iTest1,iTest2,iTest3); 
            //Into array
 end;
 CloseFile(fFile);                
            //Remember to close file!
 end;
 
 procedure TForm1.Button1Click(Sender: TObject);
 var
 iK : Integer;
 begin
 Richedit1.Clear;                
            //Clear RichEdit and add heading (next 
            line)
 RichEdit1.Lines.Add('SURNAME' + #9 + 'TEST 1' + #9 + 'TEST 2' + #9 + 
            'TEST 3');
 For iK := 1 to iCount do
 begin
 RichEdit1.Lines.Add(arrMarks[iK].showMarks);  
            //Call method showMarks
 end;
 end;
 
 procedure TForm1.Button2Click(Sender: TObject);
 var
 iK : Integer;
 begin
 Richedit1.Clear;                
            //Clear RichEdit and add heading (next 
            line)
 RichEdit1.Lines.Add('SURNAME' + #9 + 'TEST 1' + #9 + 'TEST 2' + #9 + 
            'TEST 3' +
 #9 + 'AVERAGE');
 For iK := 1 to iCount do
 begin
 RichEdit1.Lines.Add(arrMarks[iK].getAverage);
            //Call method getAverage
 end;
 end;
 
 procedure TForm1.BitBtn1Click(Sender: TObject);
 begin
 Application.Terminate;                        
            //Close application
 end;
 
 end.
 |