Advertisement
Guest User

Untitled

a guest
May 3rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. public class LoginController {
  2.  
  3. @FXML AnchorPane mainPane;
  4. @FXML AnchorPane secondaryPane;
  5. @FXML Button loginButton;
  6. @FXML Button toSignUp;
  7. @FXML PasswordField passwordField;
  8. @FXML TextField usernameField;
  9.  
  10. static String file = "info.txt";
  11. static Scanner scanFile = new Scanner(file);
  12. static Stage secondaryStage;
  13.  
  14.  
  15. public void buttonClickedHandler (ActionEvent evt) {
  16. Button clickedButton = (Button) evt.getTarget();
  17. String buttonLabel = clickedButton.getText();
  18.  
  19. if(loginButton.getText().equals(buttonLabel)) {
  20. String username = usernameField.getText();
  21. String password = passwordField.getText();
  22. boolean found = verifyLogin(username,password,file);
  23. if (found) {
  24. openWindow("loginSuccess", 350, 150);
  25. }
  26. }
  27. if(toSignUp.getText().equals(buttonLabel)) {
  28. openWindow("SignUp", 600, 400);
  29. }
  30. }
  31.  
  32. void openWindow(String name, int width, int height) {
  33. try {
  34. // load the pop up you created
  35. Pane pane = (Pane)FXMLLoader.load(getClass().getResource(name + ".fxml"));
  36.  
  37. // create a new scene
  38. Scene scene = new Scene(pane,width,height);
  39.  
  40. //select corresponding stylesheet to correct window
  41. scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
  42.  
  43. //create new stage to put scene in
  44. secondaryStage = new Stage();
  45. secondaryStage.setScene(scene);
  46. secondaryStage.setResizable(false);
  47. secondaryStage.showAndWait();
  48.  
  49. } catch(Exception e) {
  50. e.printStackTrace();
  51. }
  52. }
  53.  
  54.  
  55. boolean verifyLogin(String username, String password, String filepath) {
  56.  
  57. boolean found = false;
  58. String tempUsername = "";
  59. String tempPassword = "";
  60.  
  61. try {
  62. scanFile = new Scanner(new File(filepath));
  63. scanFile.useDelimiter("/");
  64.  
  65. //While loop read each line from designated file for the records of usernames and passwords
  66. //and save each password and username in temp strings below
  67. while (scanFile.hasNext() && !found) {
  68. //System.out.println("ok");
  69. tempUsername = scanFile.next();
  70. tempPassword = scanFile.next();
  71.  
  72. if (tempUsername.equals(username) && tempPassword .equals(password)) {
  73. found = true;
  74. }
  75. }
  76. scanFile.close();
  77. System.out.println(found);
  78. }
  79. catch(Exception e) {
  80. System.out.println("Error");
  81.  
  82. }
  83. return found;
  84. }
  85. }
  86.  
  87.  
  88.  
  89. *********************************************SIGNUP************************************
  90.  
  91.  
  92. public class SignUpController {
  93. @FXML Button SignUpButton;
  94. @FXML TextField EnterUsername;
  95. @FXML PasswordField EnterPassword;
  96. @FXML PasswordField reEnterPassword;
  97. @FXML Text PassError;
  98. @FXML Text PassError1;
  99. @FXML Text UserExists;
  100.  
  101. static String file = "info.txt";
  102. static Scanner scanFile = new Scanner(file);
  103. static Stage secondaryStage;
  104.  
  105.  
  106. public void buttonClickedHandler (ActionEvent evt) throws IOException {
  107. Button clickedButton = (Button) evt.getTarget();
  108. String buttonLabel = clickedButton.getText();
  109.  
  110. if(SignUpButton.getText().equals(buttonLabel)) {
  111. String newUsername = EnterUsername.getText();
  112. String newPassword = EnterPassword.getText();
  113. String checkPassword = reEnterPassword.getText();
  114. if (newPassword.equals(checkPassword)) {
  115. boolean requiredPassword = checkPassword(newPassword);
  116. if (requiredPassword) {
  117. boolean done = checkCredentials(newUsername, newPassword, checkPassword, file);
  118. if (!done) {
  119. closeCurrentWindow(evt);
  120. }
  121. }
  122. }
  123. }
  124. }
  125.  
  126.  
  127. private boolean checkPassword(String password) {
  128. boolean meetsRequirments = false;
  129. char ch;
  130. for (int i = 0; i < password.length(); i++) {
  131. ch = password.charAt(i);
  132. if (Character.isUpperCase(ch)) {
  133. meetsRequirments = true;
  134. }
  135.  
  136. }
  137. if (!meetsRequirments) {
  138. PassError.setVisible(true);
  139. PassError1.setVisible(true);
  140. }
  141. return meetsRequirments;
  142. }
  143.  
  144.  
  145. boolean checkCredentials(String newUsername, String newPassword, String checkPassword, String filepath) throws IOException {
  146. boolean userExists = false;
  147. try {
  148. String tempUsername = "";
  149. String hold = "";
  150. scanFile = new Scanner(new File(filepath));
  151. scanFile.useDelimiter("/");
  152.  
  153.  
  154. //While loop read each line from designated file for the records of usernames and passwords
  155. //and save each password and username in temp strings below
  156. while (scanFile.hasNext() && !userExists) {
  157. //System.out.println("ok");
  158. tempUsername = scanFile.next();
  159. hold = scanFile.next();
  160.  
  161. if (tempUsername.equals(newUsername)) {
  162. userExists = true;
  163. }
  164. }
  165. scanFile.close();
  166.  
  167. if (userExists) {
  168. UserExists.setVisible(true);
  169. }
  170. }
  171. catch(Exception e) {
  172. System.out.println("Error");
  173. }
  174.  
  175. if (!userExists) {
  176. addCredentials(newUsername, newPassword, file);
  177. }
  178. return userExists;
  179. }
  180.  
  181.  
  182. private void addCredentials(String newUsername, String newPassword, String file) throws IOException {
  183. FileWriter fw = new FileWriter(file, true);
  184. fw.write("/"+newUsername+"/"+newPassword);
  185. fw.close();
  186.  
  187. }
  188.  
  189. private void closeCurrentWindow(ActionEvent evt) {
  190. final Node source = (Node) evt.getSource();
  191. final Stage stage =(Stage)source.getScene().getWindow();
  192. stage.close();
  193. }
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement