Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package program1;
- /**
- *
- * @author Matthew Flora
- */
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import views.TheFrame;
- import program1.UserInfo;
- import java.util.ArrayList;
- public class Program1 {
- private final TheFrame frame = new TheFrame();
- ArrayList<UserInfo> userDB = new ArrayList<UserInfo>();
- public Program1() {
- populateDB(userDB);
- //sets the name of the frame to Program1
- frame.setTitle(getClass().getSimpleName());
- //sets location on the screen
- frame.setLocationRelativeTo(null);
- //Change Passwor Button, checks if the username, password, and new pasword are valid
- frame.getChangePasswordButton().addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- //Enter Login Name
- JTextField loginField = frame.getLoginField();
- //Enter Current Password
- JTextField currentPassField = frame.getCurrentPasswordField();
- //Enter New Password
- JTextField newPassField = frame.getNewPasswordField();
- //Message Text Area - Shows if infomration is Valid
- JTextArea showMessage = frame.showMessage();
- String newPass = newPassField.getText();
- String currentPass = currentPassField.getText();
- String userName = loginField.getText();
- boolean isValid = isLoginValid(userName);
- if (!isValid) {
- showMessage.append("Validation: Login Format Incorrect \n");
- } else {
- // showMessage.append("Validation: Login Format Correct \n");
- }
- // boolean isCPassValid = isCPassValid(currentPass);
- // if (!isCPassValid) {
- // //showMessage.append("Validation: Current Password Format Incorrect \n");
- // //return;
- // }
- boolean isNPValid = isNPassValid(newPass, currentPass, showMessage);
- if (!isNPValid) {
- //showMessage.append("Validation: New Password Format Incorrect \n");
- //return;
- }
- if (!isNPValid )//|| !isCPassValid)
- {
- return;
- }
- int userIndex = checkUser(userDB, userName);
- if (userIndex == -1)
- {
- showMessage.append("Validation: No such user \n");
- }
- else
- {
- if (userDB.get(userIndex).getPassword().equals(currentPass))
- {
- UserInfo user = userDB.get(userIndex);
- user.setPassword(newPass);
- userDB.set(userIndex, user);
- showMessage.append("Validation: OK \n");
- printDB(userDB);
- showMessage.append("New Password: OK \n");
- }
- else
- {
- showMessage.append("Validation: Incorrect Current Password \n");
- }
- //showMessage.append("Database: User Exists");
- }
- }
- });
- }
- private void printDB(ArrayList<UserInfo> db)
- {
- for (UserInfo user : db)
- {
- System.out.println(user.toString());
- }
- }
- private void populateDB(ArrayList<UserInfo> db)
- {
- db.add(new UserInfo("aa111111@wcupa.edu", "a2FooBar"));
- db.add(new UserInfo("bb222222@wcupa.edu", "b.FooBar"));
- db.add(new UserInfo("cc333333@wcupa.edu", "c.123456"));
- }
- private int checkUser(ArrayList<UserInfo> db, String username)
- {
- if (db.isEmpty()) return -1;
- for (int i = 0; i < db.size(); i++)
- {
- if (db.get(i).getUsername().equals(username))
- {
- return i;
- }
- else
- {
- return -1;
- }
- }
- return -1; //ok go
- }
- //Checks the current username if it is 2 characters , 6 digits, and has @wcupa.edu in that order
- private boolean isLoginValid(String userName) {
- return userName.matches("[a-zA-Z]{2}\\d{6}\\@wcupa.edu");
- }
- // private boolean isCPassValid(String currentPass) {
- //
- // return currentPass.matches("/^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{6,16}$/");
- // }
- private boolean isNPassValid(String newPass, String currentPass, JTextArea showMessage) {
- if (newPass.length() < 8) {
- showMessage.append("New password: Too short \n");
- return false;
- } else if (newPass.equals(currentPass)) {
- showMessage.append("New password: cannot be current password \n");
- return false;
- } else {
- int count = 0;
- if (newPass.matches(".*\\d.*")) {
- count++;
- }
- if (newPass.matches(".*[a-z].*")) {
- count++;
- }
- if (newPass.matches(".*[A-Z].*")) {
- count++;
- }
- if (newPass.matches(".*[!”#$%&’()*+,./;:=?_@>-].*")) {
- count++;
- }
- return count >= 3;
- }
- return showMessage.append("New password: too weak");
- }
- }
- //frame.getLoginField().setText("ab112233@wcupa.edu");
- // you can adjust the size with something like this:
- // frame.setSize(600, 500);
- // event handlers
- //System.out.println("am".matches("[a-z]") );
- //private boolean isValid2(String custNumber) {
- //return custNumber.matches("[a-zA-Z]{3}\\d{4}");}
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- Program1 app = new Program1();
- app.frame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement