Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.58 KB | None | 0 0
  1. unit LoginUnit;
  2.  
  3. interface
  4.  
  5. uses
  6.   Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  7.   Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Mask;
  8.  
  9. type
  10.   TfrmLogin = class(TForm)
  11.     btnEnter: TButton;
  12.     ledtUsername: TLabeledEdit;
  13.     btnExit: TButton;
  14.     medtPassword: TMaskEdit;
  15.     Label1: TLabel;
  16.     procedure btnEnterClick(Sender: TObject);
  17.     procedure btnExitClick(Sender: TObject);
  18.  
  19.  
  20. //record for username and password
  21. Type TCredentials = Record
  22.   Username : String[15];
  23.   Password : String[20];
  24.   UserType : String[7];
  25. End;
  26.  
  27.   private
  28.     { Private declarations }
  29.   public
  30.     { Public declarations }
  31.     UserArray : array [1..500] of TCredentials;//array storing sorted usernames + passwords
  32.     i : integer;
  33.   end;
  34.  
  35.  
  36. var
  37.   frmLogin: TfrmLogin;
  38.  
  39. implementation
  40.  
  41. {$R *.dfm}
  42.  
  43. Uses
  44.   TeacherMenuUnit , StudentMenuUnit , TeachersReportUnit;
  45.  
  46.  
  47. procedure TfrmLogin.btnEnterClick(Sender: TObject);
  48. var
  49.   FileArray : array [1..500] of TCredentials;  //Stores content from file in separate indexes
  50.   LoginFile : TextFile;
  51.   userptr , fileptr , counter : Integer;
  52.   credentials : String;
  53.   TeacherValid , StudentValid : Boolean;
  54. begin
  55.  
  56.   //storing usernames and passwords into array 'FileArray' in separate indexes:
  57.   i := 1;
  58.   assignfile(LoginFile , 'LoginDetails.txt');
  59.   Reset(LoginFile);
  60.   while not EOF(LoginFile) do
  61.     Begin
  62.       readln(LoginFile , credentials); //store username/password from file
  63.       FileArray[i].Username := credentials; //write username/password to array
  64.       i := i + 1; //increment array index by one
  65.     End;
  66.   CloseFile(LoginFile);
  67.  
  68.   //store username with coresponding passwords in the same index in array 'UserArray'
  69.   userptr := 1;
  70.   fileptr := 1;
  71.   i := i DIV 3;  //makes sure no extra array indexes are used
  72.   for counter := 1 to i do
  73.     Begin
  74.       //store username in one index:
  75.       UserArray[userptr].Username := FileArray[fileptr].Username;
  76.       //store password in same index:
  77.       UserArray[userptr].Password := FileArray[fileptr + 1].Username;
  78.       //store user type in same index:
  79.       UserArray[userptr].UserType := FileArray[fileptr + 2].Username;
  80.       userptr := userptr + 1; //increment userarray by one
  81.       fileptr := fileptr + 3  //increment filearray by two (goes to next username)
  82.     End;
  83.  
  84.  
  85.   //Search for username and password in array
  86.   TeacherValid := False;
  87.   StudentValid := False;
  88.  
  89.   for counter := 1 to i do
  90.     Begin
  91.       //comparing username and password to array
  92.  
  93.       //teacher login prerequisites
  94.       if (UserArray[counter].Username = ledtUsername.Text)
  95.          AND (UserArray[counter].Password = medtPassword.Text)
  96.          AND (UserArray[counter].UserType = 'Teacher') then
  97.         Begin
  98.           TeacherValid := True;
  99.         End;
  100.  
  101.       //student login prerequisites
  102.       if (UserArray[counter].Username = ledtUsername.Text)
  103.          AND (UserArray[counter].Password = medtPassword.Text)
  104.          AND (UserArray[counter].UserType = 'Student') then
  105.         Begin
  106.           StudentValid := True;
  107.         End;
  108.     End;
  109.  
  110.  
  111.   //Teacher logging in
  112.   if TeacherValid = True then
  113.     Begin
  114.       ShowMessage('Login Successful');
  115.       frmTeacherMenu.Show; //Display Teacher's Menu
  116.       frmLogin.Hide;
  117.     End
  118.  
  119.   //Student logging in
  120.   Else if StudentValid = True then
  121.     Begin
  122.       ShowMessage('Login Successful');
  123.       frmStudentMenu.Show; //Display Student's Menu
  124.       frmLogin.Hide;
  125.     End
  126.  
  127.   //when both username and password left blank:
  128.   Else if (medtPassword.Text = '') AND (ledtUsername.Text = '') then
  129.     ShowMessage('Please enter a username and password before logging in')
  130.  
  131.   //when no username entered
  132.   Else if ledtUsername.Text = '' then
  133.     ShowMessage('Please enter a username')
  134.  
  135.   //when no password entered
  136.   Else if medtPassword.Text = '' then
  137.     ShowMessage('Please enter a Password')
  138.  
  139.   //incorrect login details
  140.   Else
  141.     Begin
  142.         //create custom messagebox
  143.       IF Application.Messagebox('Incorrect Login Details, Please Try Again',
  144.                                   'Incorrect',
  145.                                   MB_RetryCancel or MB_IconError) = IDCancel then
  146.         Begin
  147.           Close;   //closes the form when 'Cancel' is clicked in messagebox
  148.         End
  149.       ELSE
  150.         Begin
  151.           ledtUsername.Text := ''; //clears username
  152.           medtPassword.Text := ''; //clears password
  153.         End;
  154.     End;
  155. End;
  156.  
  157.  
  158.  
  159.  
  160. procedure TfrmLogin.btnExitClick(Sender: TObject);
  161. begin
  162.   frmLogin.Close; //closes login form
  163. end;
  164.  
  165. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement