Advertisement
Guest User

Untitled

a guest
Mar 30th, 2017
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.15 KB | None | 0 0
  1. unit Unit_Login;
  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;
  8.  
  9. type
  10.   TfrmLogin = class(TForm)
  11.     lbledtUsername: TLabeledEdit;
  12.     lbledtPassword: TLabeledEdit;
  13.     btnEnter: TButton;
  14.     Exit: TButton;
  15.     procedure btnEnterClick(Sender: TObject);
  16.     procedure ExitClick(Sender: TObject);
  17.     procedure FormCreate(Sender: TObject);
  18.   private
  19.     { Private declarations }
  20.   public
  21.     { Public declarations }
  22.     Type
  23.     TPassword = record    //creating a record for the file to store passwords.
  24.     StudentPassword : string[20];
  25.     end;
  26.  
  27.     Type
  28.     TStudentNumber = record
  29.     StudentNumber : String[6];
  30.     end;
  31.  
  32.     Type
  33.     TTeacherUser = record
  34.     TeacherUsername : String[25];
  35.     end;
  36.  
  37.     Type
  38.     TTeacherPassword = record
  39.     TeacherPassword : String[25];
  40.     end;
  41.  
  42.     var
  43.     //Variables for Student accounts
  44.     PasswordFile : File of Tpassword;    // This will be the file whch stores the usernames
  45.     StudentNumberFile : File of TStudentNumber;
  46.     Student_Password : array [0..1000] of TPassword;       //An array used to store multiple Passwords
  47.     Student_Number : array [0..1000] of TStudentNumber;
  48.     //Variabels for teacher account
  49.     TeacherUsernameFile : File of TTeacherUser;
  50.     TeacherPasswordFile : File of TTeacherPassword;
  51.     Teacher_Username : array [0..1000] of TTeacherUser;
  52.     Teacher_Password : array [0..1000] of TTeacherPassword ;
  53.     FinalStudent,FinalTeacher : Integer;
  54.   end;
  55.  
  56. var
  57.   frmLogin: TfrmLogin;
  58.  
  59. implementation
  60. uses
  61.  Unit_AddLogin,UnitStudent_menu,Unit_TeacherMenu;  //Allows access to the form add login and studnet menu
  62. {$R *.dfm}
  63.  
  64. var
  65. Path : String;
  66.  
  67. procedure TfrmLogin.btnEnterClick(Sender: TObject);
  68. var
  69. count,counter,I,Temp_num : integer;  //Variables for Student account
  70. N,Current,Counts : Integer;   //Variables for Teacher accounts
  71. StudentCorrect,TeacherCorrect : Boolean;
  72. begin
  73.  
  74.    //Reads the username and password file for students
  75.    AssignFile(PasswordFile ,Path +'Password.dat');   //assigns exsiting file to Passwordfile
  76.    reset(PasswordFile );             //opens the file
  77.    AssignFile(StudentNumberFile ,Path +'StudentNumber.dat');   //assigns exsiting file to StudentNumberFile
  78.    reset(StudentNumberFile );
  79.    count := 0;    //intialises count
  80.  
  81.    while not  eof (StudentNumberFile) and not Eof (passwordfile) do
  82.    {This will repeat the code below untill the end of file is reached}
  83.     begin
  84.       Read(Passwordfile,Student_Password[count]);  //reads the password from Passwordfile
  85.       Read(StudentNumberFile,Student_Number[count]);
  86.       if ((lbledtusername.Text = Student_Number[count].StudentNumber)  and
  87.       (lbledtPassword.Text = Student_Password[count].StudentPassword)) then
  88.        begin
  89.         Showmessage('Login Successful');
  90.         frmStudentmenu.Show;   //if the login credentials is correct it shows the student menu
  91.        end;
  92.        count := count + 1;
  93.     end;
  94.     CloseFile(Passwordfile);  //Closes file which contains passwords
  95.     CloseFile(StudentNumberFile);
  96.    {Code to output incorrect login}
  97.     I := 0;
  98.    for counter  := 0 to count do
  99.     begin
  100.       I := I + 1;
  101.       if (lbledtusername.Text = Student_Number[I].StudentNumber) and
  102.        (lbledtPassword.Text = Student_Password[I].StudentPassword)  then
  103.        begin
  104.         Temp_num:= I;
  105.        end;
  106.       end;
  107.  
  108.       StudentCorrect := True;
  109.       if ((lbledtusername.Text <> Student_Number[temp_num].StudentNumber)) then
  110.           begin
  111.             StudentCorrect := False;
  112.           end;
  113.  
  114.    //Read from the teacher's Username and Password file
  115.      AssignFile(TeacherPasswordFile ,Path +'TeacherPassword.dat');   //assigns exsiting file to Passwordfile
  116.      reset(TeacherPasswordFile);             //opens the file
  117.      AssignFile(TeacherUserNameFile ,Path +'TeacherUser.dat');   //assigns exsiting file to StudentNumberFile
  118.      reset(TeacherUserNameFile);
  119.      counts := 0;    //intialises count
  120.      while not  eof (TeacherUserNameFile) and not Eof (TeacherPasswordFile) do
  121.    {This will repeat the code below untill the end of file is reached}
  122.     begin
  123.       Read(TeacherPasswordfile,Teacher_Password[counts]);  //reads the password from Passwordfile
  124.       Read(TeacherUsernameFile,Teacher_Username[counts]);
  125.       if ((lbledtusername.Text = Teacher_Username[counts].TeacherUsername)  and
  126.       (lbledtPassword.Text = Teacher_Password[counts].TeacherPassword)) then
  127.        begin
  128.         Showmessage('Login Successful');
  129.         frmTeachermenu.Show;   //if the login credentials is correct it shows the student menu
  130.        end;
  131.        counts := counts + 1;
  132.     end;
  133.     CloseFile(TeacherPasswordfile);  //Closes file which contains passwords
  134.     CloseFile(TeacherUserNameFile);
  135.  
  136.        N := 0;
  137.    for counter  := 0 to counts do
  138.     {This checks to see if the account credntials match the ones stored in the system }
  139.     begin
  140.       if (lbledtusername.Text = Teacher_Username[N].TeacherUsername) and
  141.        (lbledtPassword.Text = Teacher_Password[N].TeacherPassword)  then
  142.        begin
  143.         Current:= N;
  144.        end;
  145.        N := N + 1;
  146.       end;
  147.  
  148.       TeacherCorrect := True;
  149.       if ((lbledtusername.Text <> Teacher_Username[N].TeacherUsername)) then
  150.           begin
  151.             TeacherCorrect := False;
  152.           end;
  153.  
  154.    //This checks that the user enters either teacher or student login
  155.   if  ((StudentCorrect = False) or (TeacherCorrect = False) and
  156.       (lbledtPassword.Text <> Student_Password[temp_num].StudentPassword)) and ((lbledtusername.Text <>
  157.         Teacher_Username[Current].TeacherUsername)
  158.         or (lbledtPassword.Text <> Teacher_Password[Current].TeacherPassword))
  159.         or (lbledtPassword.Text = '') or
  160.        (lbledtUsername.Text = '')then
  161.          begin
  162.           Showmessage('Incorrect Login');
  163.          end
  164. end;
  165.  
  166. procedure TfrmLogin.ExitClick(Sender: TObject);
  167. begin
  168.   close;
  169. end;
  170.  
  171. procedure TfrmLogin.FormCreate(Sender: TObject);
  172. var
  173. count : integer;
  174. begin
  175.   Count := Count + 1;
  176.   if Count = 1 then
  177.    begin
  178.     Path := GetCurrentDir;
  179.     Count := 2;
  180.    end;
  181. end;
  182. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement