Advertisement
Guest User

Untitled

a guest
Apr 5th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. public void actionPerformed(ActionEvent e) {
  2. boolean userCheck = false;
  3. PrintWriter userWrite;
  4. int createUser;
  5. String username = addressLogOnUsernameText.getText();
  6. char[] pass = addressLogOnPasswordText.getPassword();
  7. String password = String.valueOf(pass);
  8. boolean user = false;
  9. try {
  10. File usernamesFile = new File("Resources//usernames.txt");
  11. ArrayList<String> usernames = new ArrayList<String>();
  12.  
  13. File passwordsFile = new File("Resources//passwords.txt");
  14. ArrayList<String> passwords = new ArrayList<String>();
  15.  
  16. fileScanner = new Scanner(usernamesFile);
  17. fileScanner.useDelimiter(",");
  18. do {
  19. usernames.add(fileScanner.next());
  20. } while (fileScanner.hasNext());
  21. fileScanner = new Scanner(passwordsFile);
  22. fileScanner.useDelimiter(",");
  23. do {
  24. passwords.add(fileScanner.next());
  25. } while (fileScanner.hasNext());
  26. for (int i = 0; i < usernames.size(); i++) {
  27. if ((usernames.get(i).equals(username)) && (passwords.get(i).equals(password))) {
  28. addressLogOnWindow.setVisible(false);
  29. new GUI(username);
  30. user = true;
  31. }
  32. }
  33. if (user == false) {
  34. createUser = JOptionPane.showConfirmDialog(null,
  35. "A user with these credentials could not be found.\n" + "Would you like to create one?",
  36. "Incorrect Username or Password", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE);
  37. if (createUser == JOptionPane.YES_OPTION) {
  38. for (int j = 0; j < usernames.size(); j++) {
  39. if (username.equals(usernames.get(j))) {
  40. userCheck = true;
  41. }
  42. }
  43. if (userCheck == true) {
  44. JOptionPane.showMessageDialog(null, "A user with these credentials already exists.",
  45. "User Could Not Be Created", JOptionPane.ERROR_MESSAGE);
  46. } else {
  47. try {
  48. userWrite = new PrintWriter(
  49. new BufferedWriter(new FileWriter("Resources//usernames.txt", true)));
  50. userWrite.print(username + ",");
  51. userWrite.close();
  52. userWrite = new PrintWriter(
  53. new BufferedWriter(new FileWriter("Resources//passwords.txt", true)));
  54. userWrite.print(password + ",");
  55. userWrite.close();
  56. JOptionPane.showMessageDialog(null,
  57. "Please log on with your new credentials for: " + username, "User Created",
  58. JOptionPane.INFORMATION_MESSAGE);
  59. new LogOnGUI();
  60. addressLogOnWindow.dispose();
  61. } catch (IOException ioe) {
  62. JOptionPane.showMessageDialog(null, "Could not create user", "User Creation Error",
  63. JOptionPane.ERROR_MESSAGE);
  64. }
  65. }
  66. } else {
  67. addressLogOnUsernameText.setText("");
  68. addressLogOnPasswordText.setText("");
  69. }
  70. }
  71. }
  72.  
  73. catch (FileNotFoundException ex) {
  74. JOptionPane.showMessageDialog(addressLogOnWindow,
  75. "Unable to connect to file.\n" + "Please replace the file and try again.",
  76. "File Connect Error", JOptionPane.ERROR_MESSAGE);
  77. fileScanner.close();
  78. } catch (NoSuchElementException nsee) {
  79.  
  80. }
  81. }
  82. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement