/** * This class is uses the PasswordChecker class to create an interface that allows users to enter passwords, and if they meet the criteria, they * are stored in a password file. * @author ***** * @since 2012-08-25 */ // This imports all java Input/Output methods so that they can be used by these class methods import java.io.*; // This allows the PasswordCheckerTester class to use the PasswordChecker class's constructors and methods. import tools.*; class PasswordCheckerMain { // If there is a problem with the user's input, then an IOException will be thrown public static void main(String args[]) throws IOException { // This object is used to read input from the user BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in)); // This is the file that the valid passwords will be output to, as well as the file containing the passwords that the PasswordChecker's // password will be compared to. String fileName = "passwords.txt"; // This creates a File object using the String contained in fileName. The File object is stored in the variable fileCheck File fileCheck = new File(fileName); // This variable will be used as a condition for several while loops. Basically, it is used to determine whether the user has entered // valid input. boolean acceptableAnswer; // This if statement checks to see if the password file already exists if (fileCheck.exists()) { // This prints out a message, and asks the user to input a response. System.out.print("\n" + "There is an existing password text file." + "\n" + "Would you like to view its contents" + "\n" + "(type Y for yes or N for no, and then press Enter/Return)? "); // This is used as a condition to keep the upcoming while loop executing for as long as necessary acceptableAnswer = false; // This while loop will repeat until the user enters a valid response to the previous request while(!acceptableAnswer) { // This reads in the user input and stores it in the variable answer String answer = userInput.readLine(); // This if statement checks to see if the user's input was equal to "Y", ignoring the case of the input if (answer.equalsIgnoreCase("Y")) { // This opens the file fileName so that it can be read FileInputStream fstream = new FileInputStream(fileName); // These next two declarations make it possible to actually read the given file and get its contents. DataInputStream in = new DataInputStream(fstream); BufferedReader bufferRead = new BufferedReader(new InputStreamReader(in)); // This creates the variable stringLine to be used in the upcoming while loop String stringLine; // This while loop reads the contents of the file fileName until it reaches the end. Whenever it reads a line, it stores // the line in the variable stringLine (overwritting what was previously stored) while ((stringLine = bufferRead.readLine()) != null) { // This prints out what is contained in the variable stringLine System.out.println(stringLine); } // This closes the file fileName in.close(); // This assigns boolean true to the variable acceptableAnswer, which will make the outer while loop will stop executing acceptableAnswer = true; } // This if statement checks to see if the user's input was equal to "N", ignoring the case of the input else if (answer.equalsIgnoreCase("N")) { // This assigns boolean true to the variable acceptableAnswer, which will make the outer while loop will stop executing acceptableAnswer = true; } else { // This prints out a message, and asks the user to input a response. System.out.print("Invalid input." + "\n" + "Would you like to view the file's contents" + "\n" + "(type Y for yes or N for no, and then press Enter/Return)? "); } } // This prints out a message, and asks the user to input a response. System.out.print("\n" + "Would you like to erase the file's contents" + "\n" + "(type Y for yes or N for no, and then press Enter/Return)? "); // This is used as a condition to keep the upcoming while loop executing for as long as necessary acceptableAnswer = false; // This while loop will repeat until the user enters a valid response to the previous request while(!acceptableAnswer) { // This reads in the user input and stores it in the variable answer String answer = userInput.readLine(); // This if statement checks to see if the user's input was equal to "Y", ignoring the case of the input if (answer.equalsIgnoreCase("Y")) { // This creates a File object using the String fileName. The File object is stored in the variable yourFile File yourFile = new File(fileName); // This deletes the file used to construct the File object yourFile yourFile.delete(); // This creates a File object using the String fileName. The File object is stored in the variable yourNewFile File yourNewFile = new File(fileName); // This creates an empty file in the same directory as the one that this program is stored. The file's name is the // String in fileName yourNewFile.createNewFile(); // This assigns boolean true to the variable acceptableAnswer, which will make the outer while loop will stop executing acceptableAnswer = true; } // This if statement checks to see if the user's input was equal to "N", ignoring the case of the input else if (answer.equalsIgnoreCase("N")) { // This assigns boolean true to the variable acceptableAnswer, which will make the outer while loop will stop executing acceptableAnswer = true; } else { // This prints out a message, and asks the user to input a response. System.out.print("Invalid input." + "\n" + "Would you like to erase the file's contents" + "\n" + "(type Y for yes or N for no, and then press Enter/Return)? "); } } // This prints out a message System.out.println("\n" + "This program will require you to enter a password that meets the following" + "\n" + "criteria:" + "\n"); // This prints out a message System.out.println("The password must have at least 8 characters, but no more than 10 characters."); // This prints out a message System.out.println("The password must have at least one capital letter."); // This prints out a message System.out.println("The password must have at least one special character (ex. !, @, #, etc.)"); // This prints out a message System.out.println("The password cannot be one that is already stored in the password file." + "\n"); // This prints out a message, and asks the user to input a response. System.out.print("Please enter your password: "); // This is used as a condition to keep the upcoming while loop executing for as long as necessary boolean done = false; // This while loop will repeat until the user is done with this program while (!done) { // This reads in the user input and stores it in the variable userPassword String userPassword = userInput.readLine(); // This creates a PasswordChecker object using the String contained in userPassword. The PasswordChecker object is stored // in the variable checker PasswordChecker checker = new PasswordChecker(userPassword); // This checks to see if the checkLength() method of the PasswordChecker class will return true if (checker.checkLength()) { // This checks to see if the checkCapital() method of the PasswordChecker class will return true if (checker.checkCapital()) { // This checks to see if the checkSpecial() method of the PasswordChecker class will return true if (checker.checkSpecial()) { // This checks to see if the checkNoDuplicate() method of the PasswordChecker class will return true if (checker.checkNoDuplicate(fileName)) { // This prints out a message System.out.println("Valid password."); // It is possible that an exception will occur when the password file is opened for output, hence why a // try/catch block is used try { // This opens the file fileName for output FileWriter fstream = new FileWriter(fileName, true); // This object is what is used to output data tot he file fileName BufferedWriter out = new BufferedWriter(fstream); // This writes whatever is in the first set of parentheses (in this case, it is checker.getPassword()) to // the file that was opend for output (in this case, fileName) out.write(checker.getPassword()); // This writes whatever is in the first set of parentheses (in this case, a newline character) to // the file that was opend for output (in this case, fileName) out.write("\n"); // This closes the file out.close(); // This prints out a message, and asks the user to input a response. System.out.print("\n" + "Would you like to view the password file's contents" + "\n" + "(type Y for yes or N for no, and then press Enter/Return)? "); // This is used as a condition to keep the upcoming while loop executing for as long as necessary acceptableAnswer = false; // This while loop will repeat until the user enters a valid response to the previous request while(!acceptableAnswer) { // This reads in the user input and stores it in the variable answer String answer = userInput.readLine(); // This if statement checks to see if the user's input was equal to "Y", ignoring the case of the input if (answer.equalsIgnoreCase("Y")) { // This opens the file fileName so that it can be read FileInputStream fstream2 = new FileInputStream(fileName); // These next two declarations make it possible to actually read the given file and get its contents. DataInputStream in2 = new DataInputStream(fstream2); BufferedReader bufferRead2 = new BufferedReader(new InputStreamReader(in2)); // This creates the variable stringLine to be used in the upcoming while loop String stringLine2; // This while loop reads the contents of the file fileName until it reaches the end. Whenever it reads a line, it stores // the line in the variable stringLine (overwritting what was previously stored) while ((stringLine2 = bufferRead2.readLine()) != null) { // This prints out what is stored in variable stringLine2 System.out.println(stringLine2); } // This closes the file fileName in2.close(); // This assigns boolean true to the variable acceptableAnswer, which will make the outer while loop will stop executing acceptableAnswer = true; } // This if statement checks to see if the user's input was equal to "N", ignoring the case of the input else if (answer.equalsIgnoreCase("N")) { // This assigns boolean true to the variable acceptableAnswer, which will make the outer while loop will stop executing acceptableAnswer = true; } else { // This prints out a message, and asks the user to input a response. System.out.print("Invalid input." + "\n" + "Would you like to view the password file's contents" + "\n" + "(type Y for yes or N for no, and then press Enter/Return)? "); } } } // If an exception does occur when opening fileName for output, this catch block will execute, print out a // message, and the program will terminate catch (Exception error){ System.err.println("Error: " + error.getMessage()); } } else { // This prints out a message System.out.println("This password is already in the password file."); } } else { // This prints out a message System.out.println("Password does not contain a special character."); } } else { // This prints out a message System.out.println("Password does not contain a capital letter."); } } else { // This prints out a message System.out.println("Invalid password length."); } // This prints out a message, and asks the user to input a response. System.out.print("\n" + "Do you want to enter another password" + "\n" + "(type Y for yes or N for no, and then press Enter/Return)? "); // This is used as a condition to keep the upcoming while loop executing for as long as necessary acceptableAnswer = false; // This while loop will repeat until the user enters a valid response to the previous request while(!acceptableAnswer) { // This reads in the user input and stores it in the variable answer String answer = userInput.readLine(); // This if statement checks to see if the user's input was equal to "Y", ignoring the case of the input if (answer.equalsIgnoreCase("Y")) { // This assigns boolean true to the variable acceptableAnswer, which will make the inner while loop will stop executing acceptableAnswer = true; // This prints out a message System.out.println("\n" + "The password must have at least 8 characters, but no more than 10 characters."); // This prints out a message System.out.println("The password must have at least one capital letter."); // This prints out a message System.out.println("The password must have at least one special character (ex. !, @, #, etc.)"); // This prints out a message System.out.println("The password cannot be one that is already stored in the password file." + "\n"); // This prints out a message, and asks the user to input a response. System.out.print("Please enter your password: "); } // This if statement checks to see if the user's input was equal to "N", ignoring the case of the input else if (answer.equalsIgnoreCase("N")) { // This assigns boolean true to the variable acceptableAnswer, which will make the inner while loop will stop executing acceptableAnswer = true; // This assigns boolean true to the variable done, which will make the outer while loop will stop executing done = true; } else { // This prints out a message, and asks the user to input a response. System.out.print("\n" + "Invalid input." + "\n" + "Would you like to enter another password" + "\n" + "(type Y for yes or N for no, and then press Enter/Return)? "); } } } } else { // This block will only execute if the file used to create fileCheck does not exist. This method will create an empty file with a // name that is equal to the String that was used to create the File object fileCheck fileCheck.createNewFile(); // This prints out a message System.out.println("\n" + "This program will require you to enter a password that meets the following" + "\n" + "criteria:" + "\n"); // This prints out a message System.out.println("The password must have at least 8 characters, but no more than 10 characters."); // This prints out a message System.out.println("The password must have at least one capital letter."); // This prints out a message System.out.println("The password must have at least one special character (ex. !, @, #, etc.)"); // This prints out a message System.out.println("The password cannot be one that is already stored in the password file." + "\n"); // This prints out a message, and asks the user to input a response. System.out.print("Please enter your password: "); // This is used as a condition to keep the upcoming while loop executing for as long as necessary boolean done = false; // This while loop will repeat until the user is done with this program while (!done) { // This reads in the user input and stores it in the variable userPassword String userPassword = userInput.readLine(); // This creates a PasswordChecker object using the String contained in the variable userPassword. The object is stored in // the variable checker PasswordChecker checker = new PasswordChecker(userPassword); // This checks to see if the checkLength() method of the PasswordChecker class will return true if (checker.checkLength()) { // This checks to see if the checkCapital() method of the PasswordChecker class will return true if (checker.checkCapital()) { // This checks to see if the checkSpecial() method of the PasswordChecker class will return true if (checker.checkSpecial()) { // This checks to see if the checkNoDuplicate() method of the PasswordChecker class will return true if (checker.checkNoDuplicate(fileName)) { // This prints out a message System.out.println("Valid password."); // It is possible that an error will occur when the password file is opend for output, hence why a try/catch block // is used try{ // This opens the file fileName for output FileWriter fstream = new FileWriter(fileName, true); // This object is what is used to output data tot he file fileName BufferedWriter out = new BufferedWriter(fstream); // This writes whatever is in the first set of parentheses (in this case, it is checker.getPassword()) to // the file that was opend for output (in this case, fileName) out.write(checker.getPassword()); // This writes whatever is in the first set of parentheses (in this case, a newline character) to // the file that was opend for output (in this case, fileName) out.write("\n"); // This closes the file out.close(); // This prints out a message, and asks the user to input a response. System.out.print("\n" + "Would you like to view the password file's contents" + "\n" + "(type Y for yes or N for no, and then press Enter/Return)? "); // This is used as a condition to keep the upcoming while loop executing for as long as necessary acceptableAnswer = false; // This while loop will repeat until the user enters a valid response to the previous request while(!acceptableAnswer) { // This reads in the user input and stores it in the variable answer String answer = userInput.readLine(); // This if statement checks to see if the user's input was equal to "Y", ignoring the case of the input if (answer.equalsIgnoreCase("Y")) { // This opens the file fileName so that it can be read FileInputStream fstream2 = new FileInputStream(fileName); // These next two declarations make it possible to actually read the given file and get its contents. DataInputStream in2 = new DataInputStream(fstream2); BufferedReader bufferRead2 = new BufferedReader(new InputStreamReader(in2)); // This creates the variable stringLine to be used in the upcoming while loop String stringLine2; // This while loop reads the contents of the file fileName until it reaches the end. Whenever it reads a line, it stores // the line in the variable stringLine (overwritting what was previously stored) while ((stringLine2 = bufferRead2.readLine()) != null) { // This prints out what is stored in variable stringLine2 System.out.println(stringLine2); } // This closes the file fileName in2.close(); // This assigns boolean true to the variable acceptableAnswer, which will make the outer while loop will stop executing acceptableAnswer = true; } // This if statement checks to see if the user's input was equal to "N", ignoring the case of the input else if (answer.equalsIgnoreCase("N")) { // This assigns boolean true to the variable acceptableAnswer, which will make the outer while loop will stop executing acceptableAnswer = true; } else { // This prints out a message, and asks the user to input a response. System.out.print("Invalid input." + "\n" + "Would you like to view the password file's contents" + "\n" + "(type Y for yes or N for no, and then press Enter/Return)? "); } } } // If an exception does occur when opening fileName for output, this catch block will execute, print out a // message, and the program will terminate catch (Exception error){ System.err.println("Error: " + error.getMessage()); } } else { // This prints out a message System.out.println("This password is already in the password file."); } } else { // This prints out a message System.out.println("Password does not contain a special character."); } } else { // This prints out a message System.out.println("Password does not contain a capital letter."); } } else { // This prints out a message System.out.println("Invalid password length."); } // This prints out a message, and asks the user to input a response. System.out.print("\n" + "Do you want to enter another password" + "\n" + "(type Y for yes or N for no, and then press Enter/Return)? "); // This is used as a condition to keep the upcoming while loop executing for as long as necessary acceptableAnswer = false; // This while loop will repeat until the user enters a valid response to the previous request while(!acceptableAnswer) { // This reads in the user input and stores it in the variable answer String answer = userInput.readLine(); // This if statement checks to see if the user's input was equal to "Y", ignoring the case of the input if (answer.equalsIgnoreCase("Y")) { // This assigns boolean true to the variable acceptableAnswer, which will make the inner while loop will stop executing acceptableAnswer = true; // This prints out a message System.out.println("\n" + "The password must have at least 8 characters, but no more than 10 characters."); // This prints out a message System.out.println("The password must have at least one capital letter."); // This prints out a message System.out.println("The password must have at least one special character (ex. !, @, #, etc.)"); // This prints out a message System.out.println("The password cannot be one that is already stored in the password file." + "\n"); // This prints out a message, and asks the user to input a response. System.out.print("Please enter your password: "); } else if (answer.equalsIgnoreCase("N")) { // This assigns boolean true to the variable acceptableAnswer, which will make the inner while loop will stop executing acceptableAnswer = true; // This assigns boolean true to the variable done, which will make the outer while loop will stop executing done = true; } else { // This prints out a message, and asks the user to input a response. System.out.print("\n" + "Invalid input." + "\n" + "Would you like to enter another password" + "\n" + "(type Y for yes or N for no, and then press Enter/Return)? "); } } } } // This will close the userInput channel userInput.close(); } }