Free CoreJava Programs Source codes | Examples | Video Tutorials Download

Tuesday, January 20, 2009

UserNamePassword Validation Program

import java.io.*;
/**
* Accept two arguments (a user code and password) and validate it with predefined hard coded values.

*/
class UserNamePassword
{
/**
* check for username and password
* @param un : username
* @param pwd : password
* @return : true/false
*/
public boolean checkUnPwd(String un, String pwd)
{
if(un.equals("abc") && pwd.equals("bcd"))
return true;
else
return false;
}

/**
* get the username and password from console
* @throws IOException : because of BufferedReader
*/
public void getUnPwd() throws IOException
{
System.out.println("Enter User Name : ");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String un=br.readLine();

System.out.println("Enter Password : ");
String pwd=new BufferedReader(new InputStreamReader(System.in)).readLine();

if(checkUnPwd(un,pwd))
System.out.println("Welcome!!!");
else
System.out.println("Invalid Username/Password!!");
}

/**
* exection begin from here
* @param args : command line argument
*/
public static void main(String[] args)
{
try
{
new UserNamePassword().getUnPwd();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

No comments:

Post a Comment

Followers