Created
September 19, 2016 10:23
-
-
Save GedMullen/5f5b73758d0f2cfeac56a5a90e88d5e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package warmup; | |
import java.io.Console; | |
import java.util.Arrays; | |
import java.io.IOException; | |
public class Password { | |
public static String [] passwords = {"monday", "tuesday", "wednesday"}; | |
public static void main (String args[]) throws IOException { | |
Console c = System.console(); | |
if (c == null) { | |
System.err.println("No console."); | |
System.exit(1); | |
} | |
String login = c.readLine("Enter your login: "); | |
char [] oldPassword = c.readPassword("Enter your old password: "); | |
if (verify(login, oldPassword)) { | |
boolean match= false; | |
do { | |
char [] newPassword1 = c.readPassword("Enter your new password: "); | |
char [] newPassword2 = c.readPassword("Enter new password again: "); | |
match = Arrays.equals(newPassword1, newPassword2); | |
if (!match) { | |
c.format("Passwords don't match. Try again.%n"); | |
} else { | |
change(login, newPassword1); | |
c.format("Password for %s changed.%n", login); | |
} | |
Arrays.fill(newPassword1, ' '); | |
Arrays.fill(newPassword2, ' '); | |
} while (!match); | |
} | |
} | |
static boolean verify(String login, char[] password) { | |
char [] passwordToCheck = null; | |
boolean match = false; | |
for(int i=0;i<passwords.length; i++){ | |
passwordToCheck = passwords[i].toCharArray(); | |
if(Arrays.equals(password, passwordToCheck)){ | |
match = true; | |
break; | |
} | |
} | |
return match; | |
} | |
static void change(String login, char[] password) { | |
// Modify this method to change | |
// password according to your rules. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment