Skip to content

Instantly share code, notes, and snippets.

@0wlbear
Created February 27, 2016 16:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 0wlbear/adf3481453665e04cbc0 to your computer and use it in GitHub Desktop.
Save 0wlbear/adf3481453665e04cbc0 to your computer and use it in GitHub Desktop.
import javax.swing.JFrame;
import java.awt.BorderLayout;
public class Login
{
public static void main(String[] args)
{
LoginGUI cuFrame = new LoginGUI();
cuFrame.setVisible(true);
cuFrame.setTitle("Login");
cuFrame.setSize(300,150);
cuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.border.*;
import javax.swing.*;
import java.util.*;
import java.io.*;
public class LoginGUI extends JFrame
{
private JTextField txtUser;
private JPasswordField txtPass;
private JTextField longID;
private JLabel lblUser;
private JLabel lblPass;
private JLabel lblID;
private JButton btnLogin;
private JButton btnExit;
private JPanel panel1;
ArrayList<Person> student = new ArrayList<Person>();
public LoginGUI()
{
setLayout(new BorderLayout(10, 10));
panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(250, 250));
txtUser = new JTextField();
txtUser.setPreferredSize(new Dimension(150, 20));
txtPass = new JPasswordField();
txtPass.setPreferredSize(new Dimension(150, 20));
longID= new JTextField();
longID.setPreferredSize(new Dimension(150, 20));
lblUser = new JLabel("Username: ");
lblPass = new JLabel("Password: ");
lblID = new JLabel ("Student ID: ");
btnLogin = new JButton("Login");
btnExit = new JButton("Exit");
panel1.add(lblUser);
panel1.add(txtUser);
panel1.add(lblPass);
panel1.add(txtPass);
panel1.add(lblID);
panel1.add(longID);
panel1.add(btnLogin);
panel1.add(btnExit);
ActionListener LoginAction = new LoginListener();
btnLogin.addActionListener(LoginAction);
ActionListener ExitAction = new ExitListener();
btnExit.addActionListener(ExitAction);
add(panel1, BorderLayout.CENTER);
}
{
try
{
Scanner in = new Scanner(new File("USERDATA.txt"));
while (in.hasNextLine())
{
String S=in.nextLine();
String[] Sa= S.split(",");
boolean hasUppercase = !txtPass.equals(txtPass.getText().toLowerCase()); //
boolean hasSpecial = !Sa[1].matches("[A-Za-z0-9 ]*");
boolean hasSpace = Sa[1].matches(" ");
if ((Sa[1].length() < 10))
{
JOptionPane.showMessageDialog(null,
"Password must have a minimum of 10 characters", "Error",
JOptionPane.ERROR_MESSAGE);
}
else if (hasSpace)
{
JOptionPane.showMessageDialog(null,
"Password must not have a space", "Error",
JOptionPane.ERROR_MESSAGE);
}
else if (!hasUppercase)
{
JOptionPane.showMessageDialog(null,
"Password must have at least 1 uppercase letter", "Error",
JOptionPane.ERROR_MESSAGE);
}
else if (!hasSpecial)
{
JOptionPane.showMessageDialog(null,
"Password must have at least 1 number", "Error",
JOptionPane.ERROR_MESSAGE);
}
else
{
JOptionPane.showMessageDialog(null,
"Invalid Username / Password Combo", "Error",
JOptionPane.ERROR_MESSAGE);
}
in.close();
}
in.close();
}
catch(IOException e)
{
JOptionPane.showMessageDialog(null, e.getMessage());
}
}
}
class ExitListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}
class LoginListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
boolean found = false;
Person temp = new Person(username, password, studentID); //cannot find symbol
for(array p : array)
{
if(p.compareTo(temp) == 0)
{
JOptionPane.showMessageDialog(null,
"Your user account has been validated!");
}
else if(p.compareTo(temp) !=0)
{
JOptionPane
.showMessageDialog(null,
"Your Username or Password is incorrect. Please try again");
}
txtUser.setText("");
txtPass.setText("");
}
}
}
public class Person implements Comparable<Person>
{
private static String username;
private static String password;
private static Long studentID;
public Person(String username, String password, Long studentID)
{
this.username = User;
this.password = Pass;
this.studentID = ID;
}
public String getPassword ()
{
return this.password;
}
public String getUsername()
{
return this.username;
}
public Long getStudentID()
{
return this.StudentID;
}
}
USER1,fBqj!P4B33,900000003
USER2,2Hmkq98NW+,900000006
USER3,_SNT8wsz85,900000009
USER4,5LDz2H%t3t,900000013
USER5,at!tbX92FC,900000016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment