Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- import javax.swing.*;
- import java.lang.*;
- public class Main {
- private static File file = new File ("Users.txt");
- public static void main(String args[]) throws Exception {
- CreateUser();
- }
- public static void CreateUser() throws Exception{ //login method
- String WriteToFile;
- FileWriter fw = new FileWriter(file,true);//writes to admins.txt all in one chunk
- PrintWriter pw = new PrintWriter(fw);//allows u to write to filewriter
- FileReader fr= new FileReader(file);
- LineNumberReader lnr = new LineNumberReader(fr);
- int LineNumber=lnr.getLineNumber();
- String UserID = Integer.toString(LineNumber);
- String s,username,password,DetailsConfirm;
- username="";
- boolean ValidEmail=false;
- boolean CorrectFormat=false;
- boolean IsInUse=false;
- while (ValidEmail==false)
- {
- ValidEmail=(CorrectFormat && IsInUse);
- username = JOptionPane.showInputDialog(null,"Please enter a valid email address"); // enter user and pass
- CorrectFormat=isValidEmailAddress(username);
- if (CorrectFormat==true)
- IsInUse= isEmailInUse(username);
- }
- password = RandomPassword();
- DetailsConfirm=("Your username is "+username+ "Your Password is "+password);
- JOptionPane.showMessageDialog(null, DetailsConfirm);
- WriteToFile =("\n"+username + "," + password+","+UserID); // creates string to be stored in file
- pw.println(WriteToFile);//printwriter will write the account details to filewriter
- pw.close();//at this point it is printed to the text file
- }
- static String RandomPassword() throws Exception {
- String alphabet= "abcdefghijklmnopqrstuvwxyz1234567890";
- String GeneratedString = "";
- Random random = new Random();
- int randomLen = 5+random.nextInt(8); // between 5 and 12
- for (int i = 0; i < randomLen; i++) {
- char c = alphabet.charAt(random.nextInt(36));
- GeneratedString+=c;
- }
- return GeneratedString;
- }
- private static boolean isValidEmailAddress(String email) {
- 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,}))$";
- java.util.regex.Pattern p = java.util.regex.Pattern.compile(ePattern);
- java.util.regex.Matcher m = p.matcher(email);
- return m.matches();
- }
- public static boolean isEmailInUse(String email) throws IOException {
- boolean found = false;
- File aFile = new File("Users.txt");
- Scanner in = new Scanner(aFile);
- String fileElements[];
- while (in.hasNext()) {
- fileElements = (in.nextLine()).split(",");
- if ((fileElements[0]).equals(email))
- found = true;
- }
- if (found==true)
- {JOptionPane.showMessageDialog(null, "Email already in use");
- return false;
- }
- else
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement