Advertisement
heavenriver

PersistentLogin.java (path: ../login/impl)

May 26th, 2012
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.26 KB | None | 0 0
  1. /**
  2.   * Implements the Login interface. Defines a file-based set of accounts.
  3.   */
  4.  
  5. package login.impl;
  6. import login.*;
  7. import java.io.*;
  8.  
  9. public class PersistentLogin implements Login
  10.     {
  11.    
  12.     public File file;
  13.    
  14.     /**
  15.       * Initializes the file to a given value.
  16.       * @param f The given file.
  17.       */
  18.    
  19.     public PersistentLogin(File f)
  20.         {
  21.         file = f;
  22.         }
  23.    
  24.     /**
  25.       * Checks the presence of a given account in the file. Returns it if present. No exceptions thrown; returns null where an IOException would be present.
  26.       * @param login The username associated to the account.
  27.       * @param password The password associated to the account.
  28.       * @return The account associated to the given access credentials, if present.
  29.       */
  30.    
  31.     public Account login(String login, String password)
  32.         {
  33.         try
  34.             {
  35.             FileReader f = new FileReader(file);
  36.             BufferedReader br = new BufferedReader(f);
  37.             String line = br.readLine();
  38.             while(line != null)
  39.                 {
  40.                 if(!(line.equals("")))
  41.                     {
  42.                     String[] data = line.split(";");
  43.                     if(data[0].equals(login) && data[1].equals(password))
  44.                         return new Account(data[0], data[1], data[2]);
  45.                     }
  46.                 line = br.readLine();
  47.                 }
  48.             }
  49.         catch(IOException e)
  50.             {
  51.             return null;
  52.             }
  53.         return null;
  54.         }
  55.    
  56.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement