Advertisement
Guest User

Untitled

a guest
Apr 25th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import javax.swing.*;
  4. import java.lang.*;
  5.  
  6. public class Main {
  7. private static File file = new File ("Users.txt");
  8. public static void main(String args[]) throws Exception {
  9. CreateUser();
  10.  
  11.  
  12.  
  13. }
  14. public static void CreateUser() throws Exception{ //login method
  15. String WriteToFile;
  16. FileWriter fw = new FileWriter(file,true);//writes to admins.txt all in one chunk
  17. PrintWriter pw = new PrintWriter(fw);//allows u to write to filewriter
  18. FileReader fr= new FileReader(file);
  19. LineNumberReader lnr = new LineNumberReader(fr);
  20. int LineNumber=lnr.getLineNumber();
  21. String UserID = Integer.toString(LineNumber);
  22. String s,username,password,DetailsConfirm;
  23. username="";
  24. boolean ValidEmail=false;
  25. boolean CorrectFormat=false;
  26. boolean IsInUse=false;
  27. while (ValidEmail==false)
  28. {
  29. ValidEmail=(CorrectFormat && IsInUse);
  30. username = JOptionPane.showInputDialog(null,"Please enter a valid email address"); // enter user and pass
  31.  
  32. CorrectFormat=isValidEmailAddress(username);
  33. if (CorrectFormat==true)
  34. IsInUse= isEmailInUse(username);
  35.  
  36. }
  37.  
  38.  
  39.  
  40. password = RandomPassword();
  41.  
  42.  
  43. DetailsConfirm=("Your username is "+username+ "Your Password is "+password);
  44. JOptionPane.showMessageDialog(null, DetailsConfirm);
  45. WriteToFile =("\n"+username + "," + password+","+UserID); // creates string to be stored in file
  46. pw.println(WriteToFile);//printwriter will write the account details to filewriter
  47. pw.close();//at this point it is printed to the text file
  48. }
  49.  
  50.  
  51. static String RandomPassword() throws Exception {
  52. String alphabet= "abcdefghijklmnopqrstuvwxyz1234567890";
  53. String GeneratedString = "";
  54. Random random = new Random();
  55. int randomLen = 5+random.nextInt(8); // between 5 and 12
  56. for (int i = 0; i < randomLen; i++) {
  57. char c = alphabet.charAt(random.nextInt(36));
  58. GeneratedString+=c;
  59. }
  60.  
  61. return GeneratedString;
  62. }
  63. private static boolean isValidEmailAddress(String email) {
  64. String ePattern = "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$";
  65. java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);
  66. java.util.regex.Matcher m = p.matcher(email);
  67. return m.matches();
  68.  
  69. }
  70.  
  71. public static boolean isEmailInUse(String email) throws IOException {
  72.  
  73. boolean found = false;
  74. File aFile = new File("Users.txt");
  75.  
  76. Scanner in = new Scanner(aFile);
  77. String fileElements[];
  78. while (in.hasNext()) {
  79. fileElements = (in.nextLine()).split(",");
  80. if ((fileElements[0]).equals(email))
  81. found = true;
  82.  
  83.  
  84. }
  85. if (found==true)
  86. {JOptionPane.showMessageDialog(null, "Email already in use");
  87. return false;
  88. }
  89. else
  90. return true;
  91. }
  92.  
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement