Advertisement
Guest User

Untitled

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