import java.awt.*;
import
javax.swing.*;
import java.io.*;
import
java.util.regex.*;
import
java.awt.event.*;
class login1 extends
JFrame implements ActionListener,KeyListener,FocusListener
{
int cnt=0;
String str1,uname,upass,ans,answer,name;
String password;
Boolean flag=false,flag1=false;
String opass,npass,cpass;
String que;
JLabel l1=new
JLabel("Username :");
JLabel l2=new
JLabel("Password :");
JLabel qu=new JLabel(" ");
JTextField t1=new
JTextField(20);
JPasswordField t2=new
JPasswordField(20);
ImageIcon image1=new
ImageIcon("login.jpg");
JButton login=new
JButton(image1);
ImageIcon image2=new
ImageIcon("for.jpg");
JButton forget=new
JButton(image2);
ImageIcon image3=new
ImageIcon("change.jpg");
JButton change=new
JButton(image3);
JButton recover=new
JButton("RecoverPassword");
JLabel jb=new
JLabel();
JLabel jl=new
JLabel("Username :");
JTextField tf=new
JTextField(15);
JLabel qq=new
JLabel("Question :");
JLabel j2=new
JLabel("Answer :");
JTextField tf2=new
JTextField(15);
JPasswordField oldpass=new
JPasswordField(15);
JPasswordField newpass=new
JPasswordField(15);
JPasswordField confirm=new JPasswordField(15);
JTextField uname1=new
JTextField(15);
JButton changepass=new JButton("ChangePassword");
Font f;
JFrame frame;
login1(String
str)
{
super(str);
JPanel p=new
JPanel(new GridLayout(2,2));
setSize(700,300);
JPanel p1=new
JPanel(new GridLayout(1,3));
JPanel p2=new
JPanel();
Insets insets = new Insets(0,1,0,0);
login.setMargin(insets);
forget.setMargin(insets);
change.setMargin(insets);
login.setToolTipText("Login");
forget.setToolTipText("Recover
Password");
change.setToolTipText("Change
Password");
p1.add(login);
p1.add(forget);
p1.add(change);
p2.add(p1);
p2.setBounds(0,70,650,100);
add(p2);
JPanel
p3=new JPanel();
p.add(l1);
p.add(t1);
p.add(l2);
p.add(t2);
p3.add(p);
add(p3,BorderLayout.CENTER);
setResizable(false);
login.addActionListener(this);
login.addKeyListener(this);
forget.addActionListener(this);
forget.addKeyListener(this);
change.addKeyListener(this);
change.addActionListener(this);
recover.addActionListener(this);
recover.addKeyListener(this);
changepass.addActionListener(this);
changepass.addKeyListener(this);
tf.addFocusListener(this);
oldpass.addFocusListener(this);
newpass.addFocusListener(this);
confirm.addFocusListener(this);
uname1.addFocusListener(this);
str1="Well
Come";
}
public
boolean validdateuserandpass(String f,String use,String pass)
{
try
{
File
infile=new File(f);
int
flag=0;
BufferedReader br=new
BufferedReader(new FileReader(f));
String uname=null;
while((uname=br.readLine())!=null)
{
uname=uname.trim();
Pattern pat=Pattern.compile("/");
String
str[]=pat.split(uname);
if(str[0].equals(use)
&& str[1].equals(pass))
{
flag++;
break;
}
else
{
flag=0;
}
}
br.close();
if(flag>0)
{
return
true;
}
else
{
return
false;
}
}
catch(Exception
e)
{
System.out.println(e);
}
return
true;
}
public
void changepassword(String f,String oldp,String newp,String use)
{
int flag=0,flag1=0;
try
{
File infile=new
File(f);
BufferedReader
br=new BufferedReader(new FileReader(f));
File tempFile=new
File(infile.getAbsolutePath() + ".tmp");
PrintWriter
pw=new PrintWriter(new FileWriter(tempFile));
String
uname=null;
while((uname=br.readLine())!=null)
{
uname=uname.trim();
Pattern
pat=Pattern.compile("/");
String
str[]=pat.split(uname);
if(str[0].equals(use))
{
flag++;
if(str[1].equals(oldp))
{
name=str[0];
password=str[1];
flag1++;
str[1]=newp;
uname=str[0]+"/"+str[1]+"/"+str[2]+"/"+str[3];
//break;
}
}
pw.println(uname);
pw.flush();
}
pw.close();
br.close();
if(!infile.delete())
{
System.out.println("Could not
delete file");
return;
}
if(!tempFile.renameTo(infile))
{
System.out.println("Could not rename
file");
}
if(flag==0)
{
JOptionPane.showMessageDialog(frame,"Invalid
Username");
}
else
if(flag1==0)
{
JOptionPane.showMessageDialog(frame,"Invalid
Password");
}
else
if(flag>0 && flag1>0)
{
JOptionPane.showMessageDialog(frame,"Password
Successfully Changed");
frame.dispose();
}
}
catch(Exception
e)
{
System.out.println(e);
}
}
public boolean
forgetpassword(String f,String use)
{
try
{
int flag=0;
File infile=new File(f);
BufferedReader br=new
BufferedReader(new
FileReader(f));
String uname=null;
while((uname=br.readLine())!=null)
{
uname=uname.trim();
Pattern pat=Pattern.compile("/");
String str[]=pat.split(uname);
if(str[0].equals(use))
{
que=str[2];
ans=str[3];
password=str[1];
name=str[0];
flag++;
break;
}
else
{
flag=0;
}
}
br.close();
if(flag>0)
{
return
true;
}
else
{
return
false;
}
}
catch(Exception
e)
{
System.out.println(e);
}
return
true;
}
public void
keyTyped(KeyEvent ke)
{}
public void
keyReleased(KeyEvent ke)
{}
public void
keyPressed(KeyEvent ke)
{
if(ke.getSource().equals(recover))
{
answer=tf2.getText();
uname=tf.getText();
if(!uname.equals(name)
&& !uname.equals(""))
{
JOptionPane.showMessageDialog(frame,"Invalid
Username");
}
else
if(uname.equals(""))
{
JOptionPane.showMessageDialog(frame,"Please
First Enter Username");
}
else
{
if(answer.equals(""))
{
JOptionPane.showMessageDialog(frame,"Please
First Enter Answer");
}
else
{
if(answer.equals(ans))
{
JOptionPane.showMessageDialog(frame,"Your
Password is " + password);
frame.dispose();
}
else
{
JOptionPane.showMessageDialog(frame,"Incorrect
Answer please try again");
}
}
}
}
if(ke.getSource().equals(login))
{
uname=t1.getText();
upass=t2.getText();
if(validdateuserandpass("login.txt",uname,upass))
{
frame=new
JFrame("Wellcome" );
f=new
Font(str1,Font.PLAIN,20);
setFont(f);
frame.setSize(500,200);
frame.setVisible(true);
str1+=
" "+ uname;
jb.setText(str1);
JPanel
p1=new JPanel();
p1.add(jb);
frame.add(p1);
frame.setResizable(false);
dispose();
}
else
{
JOptionPane.showMessageDialog(login,"Invalid
Username & Password");
}
}
if(ke.getSource().equals(forget))
{
frame=new JFrame("ForgetPassword");
frame.setSize(600,200);
tf2.setText("");
tf.setText("");
qu.setText("");
JPanel
p=new JPanel(new GridLayout(1,2));
JPanel
p1=new JPanel();
JPanel
p2=new JPanel(new GridLayout(2,2));
JPanel
p3=new JPanel();
JPanel
p4=new JPanel(new GridLayout(1,1));
JPanel
p5=new JPanel();
p2.add(qq);
p2.add(qu);
p2.add(j2);
p2.add(tf2);
p3.add(p2);
p3.setBounds(10,60,600,150);
frame.add(p3,BorderLayout.CENTER);
p4.add(recover);
p5.add(p4);
p5.setBounds(10,110,600,150);
frame.add(p5);
p.add(jl);
p.add(tf);
p1.add(p);
frame.add(p1,BorderLayout.CENTER);
frame.setResizable(false);
frame.setVisible(true);
}
if(ke.getSource().equals(change))
{
frame=new
JFrame("ChangePassword");
frame.setSize(500,200);
uname1.setText("");
oldpass.setText("");
newpass.setText("");
confirm.setText("");
jl.setText("UserName
: ");
jb.setText("OldPassword
:");
qq.setText("NewPassword
:");
j2.setText("Confirm
: ");
JPanel
p1=new JPanel(new GridLayout(4,2));
JPanel
p2=new JPanel();
JPanel
p3=new JPanel(new GridLayout(1,1));
JPanel
p4=new JPanel();
p3.add(changepass);
p4.add(p3);
p4.setBounds(0,90,500,175);
frame.add(p4);
p1.add(jl);
p1.add(uname1);
p1.add(jb);
p1.add(oldpass);
p1.add(qq);
p1.add(newpass);
p1.add(j2);
p1.add(confirm);
p2.add(p1);
frame.add(p2,BorderLayout.CENTER);
frame.setResizable(false);
frame.setVisible(true);
}
if(ke.getSource().equals(changepass))
{
uname=uname1.getText();
opass=oldpass.getText();
npass=newpass.getText();
cpass=confirm.getText();
if(uname.equals(""))
{
JOptionPane.showMessageDialog(frame,"Please
Enter the Username");
}
else
if(opass.equals(""))
{
JOptionPane.showMessageDialog(frame,"Please
Enter the OldPassword");
}
else
if(npass.equals(""))
{
JOptionPane.showMessageDialog(frame,"Please
Enter the NewPassword");
}
else
if(cpass.equals(npass))
{
changepassword("login.txt",opass,npass,uname);
}
else
{
JOptionPane.showMessageDialog(frame,"NewPassword and ConfirmPassword not matched");
}
}
}
public void
focusLost(FocusEvent te)
{
if(te.getSource().equals(tf))
{
uname=tf.getText();
if(forgetpassword("login.txt",uname))
{
qu.setText(que);
}
else
{
if(frame.isVisible())
{
JOptionPane.showMessageDialog(frame,"Incorrect
Username");
tf.setText("");
}
}
}
if(te.getSource().equals(uname1))
{
uname=uname1.getText();
flag1=forgetpassword("login.txt",uname);
if(flag1)
{
uname1.setEnabled(false);
}
else
{
if(frame.isVisible())
{
JOptionPane.showMessageDialog(frame,"Incorrect
Username");
uname1.setText("");
}
}
}
if(te.getSource().equals(oldpass))
{
uname=uname1.getText();
opass=oldpass.getText();
npass=newpass.getText();
cpass=confirm.getText();
flag=validdateuserandpass("login.txt",uname,opass);
if(flag)
{
oldpass.setEnabled(false);
}
else
if(flag1)
{
if(frame.isVisible())
{
JOptionPane.showMessageDialog(frame,"Password
not Matched");
oldpass.setText("");
}
}
}
if(te.getSource().equals(newpass))
{
npass=newpass.getText();
if(npass.equals("")
&& flag)
{
if(frame.isVisible())
{
JOptionPane.showMessageDialog(frame,"Please
enter the
NewPassword");
newpass.setText("");
}
}
else
if(flag)
{
newpass.setEnabled(false);
}
}
if(te.getSource().equals(confirm))
{
npass=newpass.getText();
cpass=confirm.getText();
if(cpass.equals(npass)
&& !npass.equals(""))
{
confirm.setEnabled(false);
}
else
if(!npass.equals(""))
{
if(frame.isVisible())
{
JOptionPane.showMessageDialog(frame,"NewPassword
and ConfirmPassword not Matched");
newpass.setEnabled(true);
newpass.setText("");
confirm.setText("");
}
}
}
}
public
void focusGained(FocusEvent te)
{
}
public void
actionPerformed(ActionEvent ae)
{
uname=t1.getText();
upass=t2.getText();
if(ae.getSource().equals(login))
{
if(validdateuserandpass("login.txt",uname,upass))
{
frame=new
JFrame("Wellcome" );
f=new
Font(str1,Font.PLAIN,20);
setFont(f);
frame.setSize(500,200);
frame.setVisible(true);
str1+=
" "+ uname;
jb.setText(str1);
JPanel
p1=new JPanel();
p1.add(jb);
frame.add(p1);
frame.setResizable(false);
dispose();
}
else
{
JOptionPane.showMessageDialog(login,"Invalid
Username & Password");
}
}
if(ae.getSource().equals(forget))
{
frame=new
JFrame("ForgetPassword");
frame.setSize(600,200);
tf2.setText("");
tf.setText("");
qu.setText("");
JPanel
p=new JPanel(new GridLayout(1,2));
JPanel
p1=new JPanel();
JPanel
p2=new JPanel(new GridLayout(2,2));
JPanel
p3=new JPanel();
JPanel
p4=new JPanel(new GridLayout(1,1));
JPanel
p5=new JPanel();
p2.add(qq);
p2.add(qu);
p2.add(j2);
p2.add(tf2);
p3.add(p2);
p3.setBounds(10,60,600,150);
frame.add(p3,BorderLayout.CENTER);
p4.add(recover);
p5.add(p4);
p5.setBounds(10,110,600,150);
frame.add(p5);
p.add(jl);
p.add(tf);
p1.add(p);
frame.add(p1,BorderLayout.CENTER);
frame.setResizable(false);
frame.setVisible(true);
}
if(ae.getSource().equals(recover))
{
answer=tf2.getText();
uname=tf.getText();
if(!uname.equals(name)
&& !uname.equals(""))
{
JOptionPane.showMessageDialog(frame,"Invalid
Username");
}
else
if(uname.equals(""))
{
JOptionPane.showMessageDialog(frame,"Please
First Enter Username");
}
else
{
if(answer.equals(""))
{
JOptionPane.showMessageDialog(frame,"Please
First Enter Answer");
}
else
{
if(answer.equals(ans))
{
JOptionPane.showMessageDialog(frame,"Your
Password is " + password);
frame.dispose();
}
else
{
JOptionPane.showMessageDialog(frame,"Incorrect
Answer please try again");
}
}
}
}
if(ae.getSource().equals(change))
{
frame=new
JFrame("ChangePassword");
frame.setSize(500,200);
oldpass.setEnabled(true);
uname1.setEnabled(true);
newpass.setEnabled(true);
confirm.setEnabled(true);
uname1.setText("");
oldpass.setText("");
newpass.setText("");
confirm.setText("");
jl.setText("UserName
: ");
jb.setText("OldPassword
:");
qq.setText("NewPassword
:");
j2.setText("Confirm
: ");
JPanel
p1=new JPanel(new GridLayout(4,2));
JPanel
p2=new JPanel();
JPanel
p3=new JPanel(new GridLayout(1,1));
JPanel
p4=new JPanel();
p3.add(changepass);
p4.add(p3);
p4.setBounds(0,90,500,175);
frame.add(p4);
p1.add(jl);
p1.add(uname1);
p1.add(jb);
p1.add(oldpass);
p1.add(qq);
p1.add(newpass);
p1.add(j2);
p1.add(confirm);
p2.add(p1);
frame.add(p2,BorderLayout.CENTER);
frame.setResizable(false);
frame.setVisible(true);
}
if(ae.getSource().equals(changepass))
{
uname=uname1.getText();
opass=oldpass.getText();
npass=newpass.getText();
cpass=confirm.getText();
if(uname.equals(""))
{
JOptionPane.showMessageDialog(frame,"Please
Enter the Username");
}
else
if(opass.equals(""))
{
JOptionPane.showMessageDialog(frame,"Please
Enter the OldPassword");
}
else
if(npass.equals(""))
{
JOptionPane.showMessageDialog(frame,"Please
Enter the NewPassword");
}
else
if(cpass.equals(npass))
{
changepassword("login.txt",opass,npass,uname);
}
else
{
JOptionPane.showMessageDialog(frame,"NewPassword
and ConfirmPassword not Matched");
}
}
}
public
static void main(String args[])
{
Image
image;
login1
l=new login1("Login");
l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Toolkit
tool = Toolkit.getDefaultToolkit();
image=tool.getImage("Login.png");
l.setIconImage(image);
l.setVisible(true);
}
}
/*Notepad File
Hetal/Adhvaryu/Who is your best Friend ?/Sneha
Avni/goenka/What is your favourite book ?/Java
dhruvil/shah/What is id no ?/123
manish/dalwadi/What was the name of your First Teacher
?/Hiral
*/
OUTPUT :=
=======