Advertisement
romanboy

PersonClass.java

Jan 18th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.82 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.IOException;
  4. import java.util.Scanner;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7.  
  8. /*
  9.  * To change this license header, choose License Headers in Project Properties.
  10.  * To change this template file, choose Tools | Templates
  11.  * and open the template in the editor.
  12.  */
  13.  
  14. /**
  15.  *
  16.  * @author Radu Ciuca
  17.  */
  18. public class PersonClass {
  19.      
  20.     //attributes which are required to specify person
  21.     File loginFile = new File("login.txt");
  22.     String username;
  23.     String password;
  24.     String usertype;
  25.     String fullName;
  26.     String gender;
  27.     String phoneNo;
  28.     String email;
  29.     String DOB;
  30.  
  31.     public boolean LoggedIN = true;
  32.     String setUtype;
  33.    
  34.     /*****************************************************************
  35.        About: This functions which starts with set are for setting values for variables
  36.         *****************************************************************/
  37.     public void setPassword (String pword){
  38.         password = pword;
  39.     }
  40.  
  41.     public void setUsername (String uname){
  42.         username = uname;
  43.     }
  44.     public void setUtype (String utype){
  45.         usertype = utype;
  46.     }
  47.        /*****************************************************************
  48.        About: This function is logging in user to a system and allocates
  49.        relevant, in order to continue working.
  50.         *****************************************************************/
  51.     public boolean LogIn() throws FileNotFoundException, IOException
  52.     {
  53.          try {
  54.             // TODO add your handling code here:
  55.             Scanner sc = new Scanner(loginFile);
  56.             String line = ""; //get data from line by line, then put it in variable line
  57.             while(sc.hasNextLine())
  58.             {
  59.                 line = sc.nextLine();
  60.                 String uname =line.substring(line.indexOf("username:")+ 10,
  61.                         line.indexOf(", password"));
  62.                 String pword =line.substring(line.indexOf("password:")+ 10,
  63.                         line.indexOf(", usertype:"));
  64.                 String utype = line.substring(line.indexOf("usertype: ")+ 10, line.indexOf(".;")).trim();
  65.                 //student, admin, tutor or parent?
  66.                 if(uname.equals(username) && pword.equals(password) && utype.equals(usertype))
  67.                 {
  68.                     //login
  69.                     if(utype.equals("admin"))
  70.                     {
  71.                         //jLabel3.setText("admin.");
  72.                         AdminStaff.mainForm.setVisible(true);
  73.                     }
  74.                     else if(utype.equals("parent"))
  75.                     {
  76.                         //jLabel3.setText("parent.");
  77.                         Parent.mainForm.setVisible(true);
  78.                     }
  79.                     else if (utype.equals("tutor"))
  80.                     {
  81.                         //jLabel3.setText("tutor.");
  82.                         Tutor.mainForm.setVisible(true);
  83.                     }
  84.                     else if(utype.equals("student"))
  85.                     {
  86.                         //jLabel3.setText("student.");
  87.                         Student.mainForm.setVisible(true);
  88.                     }
  89.                            
  90.                     //Person.mainForm.setVisible(true);
  91.                     break;
  92.                 }
  93.                 else
  94.                 {
  95.                    
  96.                     //don't login, show error
  97.                 }
  98.             }
  99.        
  100.             //closing file
  101.             sc.close();
  102.          }
  103.          catch (FileNotFoundException ex) {
  104.             Logger.getLogger(PersonClass.class.getName()).log(Level.SEVERE, null, ex);
  105.         }
  106.          //returnign boolean value to get access in system
  107.         return LoggedIN;
  108.     }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement