Advertisement
Guest User

Untitled

a guest
Feb 16th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. package program1;
  2.  
  3. /**
  4. *
  5. * @author Matthew Flora
  6. */
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import javax.swing.JTextArea;
  10. import javax.swing.JTextField;
  11. import views.TheFrame;
  12.  
  13. import program1.UserInfo;
  14. import java.util.ArrayList;
  15.  
  16. public class Program1 {
  17.  
  18. private final TheFrame frame = new TheFrame();
  19.  
  20. ArrayList<UserInfo> userDB = new ArrayList<UserInfo>();
  21.  
  22. public Program1() {
  23.  
  24. populateDB(userDB);
  25.  
  26. //sets the name of the frame to Program1
  27. frame.setTitle(getClass().getSimpleName());
  28.  
  29. //sets location on the screen
  30. frame.setLocationRelativeTo(null);
  31.  
  32. //Change Passwor Button, checks if the username, password, and new pasword are valid
  33. frame.getChangePasswordButton().addActionListener(new ActionListener() {
  34. @Override
  35. public void actionPerformed(ActionEvent e) {
  36.  
  37. //Enter Login Name
  38. JTextField loginField = frame.getLoginField();
  39.  
  40. //Enter Current Password
  41. JTextField currentPassField = frame.getCurrentPasswordField();
  42.  
  43. //Enter New Password
  44. JTextField newPassField = frame.getNewPasswordField();
  45.  
  46. //Message Text Area - Shows if infomration is Valid
  47. JTextArea showMessage = frame.showMessage();
  48.  
  49. String newPass = newPassField.getText();
  50.  
  51. String currentPass = currentPassField.getText();
  52.  
  53. String userName = loginField.getText();
  54.  
  55. boolean isValid = isLoginValid(userName);
  56.  
  57. if (!isValid) {
  58. showMessage.append("Validation: Login Format Incorrect \n");
  59. } else {
  60. // showMessage.append("Validation: Login Format Correct \n");
  61. }
  62.  
  63. // boolean isCPassValid = isCPassValid(currentPass);
  64. // if (!isCPassValid) {
  65. // //showMessage.append("Validation: Current Password Format Incorrect \n");
  66. // //return;
  67. // }
  68. boolean isNPValid = isNPassValid(newPass, currentPass, showMessage);
  69.  
  70. if (!isNPValid) {
  71. //showMessage.append("Validation: New Password Format Incorrect \n");
  72. //return;
  73. }
  74.  
  75. if (!isNPValid )//|| !isCPassValid)
  76. {
  77. return;
  78. }
  79.  
  80. int userIndex = checkUser(userDB, userName);
  81. if (userIndex == -1)
  82. {
  83. //showMessage.append("Database: User Does Not Exist");
  84. }
  85. else
  86. {
  87. if (userDB.get(userIndex).getPassword().equals(currentPass))
  88. {
  89. UserInfo user = userDB.get(userIndex);
  90. user.setPassword(newPass);
  91. userDB.set(userIndex, user);
  92. showMessage.append("Validation: OK \n");
  93. printDB(userDB);
  94. showMessage.append("New Password: OK \n");
  95. }
  96. else
  97. {
  98. showMessage.append("Validation: Incorrect Current Password \n");
  99. }
  100. //showMessage.append("Database: User Exists");
  101.  
  102. }
  103.  
  104.  
  105. }
  106. });
  107. }
  108.  
  109. private void printDB(ArrayList<UserInfo> db)
  110. {
  111. for (UserInfo user : db)
  112. {
  113. System.out.println(user.toString());
  114. }
  115. }
  116.  
  117. private void populateDB(ArrayList<UserInfo> db)
  118. {
  119. db.add(new UserInfo("aa111111@wcupa.edu", "a2FooBar"));
  120. db.add(new UserInfo("bb222222@wcupa.edu", "b.FooBar"));
  121. db.add(new UserInfo("cc333333@wcupa.edu", "c.123456"));
  122. }
  123.  
  124. private int checkUser(ArrayList<UserInfo> db, String username)
  125. {
  126. if (db.isEmpty()) return -1;
  127.  
  128. for (int i = 0; i < db.size(); i++)
  129. {
  130. if (db.get(i).getUsername().equals(username))
  131. {
  132. return i;
  133. }
  134. else
  135. {
  136. return -1;
  137. }
  138. }
  139.  
  140. return -1; //ok go
  141. }
  142.  
  143. //Checks the current username if it is 2 characters , 6 digits, and has @wcupa.edu in that order
  144. private boolean isLoginValid(String userName) {
  145. return userName.matches("[a-zA-Z]{2}\\d{6}\\@wcupa.edu");
  146. }
  147.  
  148. // private boolean isCPassValid(String currentPass) {
  149. //
  150. // return currentPass.matches("/^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{6,16}$/");
  151. // }
  152.  
  153. private boolean isNPassValid(String newPass, String currentPass, JTextArea showMessage) {
  154.  
  155. if (newPass.length() < 8) {
  156. showMessage.append("New password: Too short \n");
  157. return false;
  158.  
  159. } else if (newPass.equals(currentPass)) {
  160. showMessage.append("New password: cannot be current password \n");
  161. return false;
  162.  
  163. } else {
  164.  
  165. return newPass.matches("^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=!])(?=\\S+$).{8,}$");
  166. }
  167.  
  168. }
  169.  
  170. //frame.getLoginField().setText("ab112233@wcupa.edu");
  171. // you can adjust the size with something like this:
  172. // frame.setSize(600, 500);
  173. // event handlers
  174. //System.out.println("am".matches("[a-z]") );
  175. //private boolean isValid2(String custNumber) {
  176. //return custNumber.matches("[a-zA-Z]{3}\\d{4}");}
  177. /**
  178. * @param args the command line arguments
  179. */
  180. public static void main(String[] args) {
  181. Program1 app = new Program1();
  182.  
  183. app.frame.setVisible(true);
  184. }
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement