Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.File;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.Scanner;
- import java.util.StringTokenizer;
- public class SecondLifeAccount2 {
- private static String username;
- private String password;
- private int dob;
- private String secretQuestion;
- private String secretAnswer;
- static final String FILENAME = "SLAccount.txt";
- /**
- * Create variables for SecondLifeAccount2 object.
- *
- * @param u user's desired username
- * @param p user's desired password
- * @param d user's date of birth
- * @param q user's secret question
- * @param a user's answer to secret question
- */
- public SecondLifeAccount2(String u, String p, int d,
- String q, String a) {
- username = u;
- password = p;
- dob = d;
- secretQuestion = q;
- secretAnswer = a;
- }
- /**
- * Check if username is appropriate number of characters.
- *
- * @param username User's entered username
- * @return true or false
- */
- public static boolean goodUsername(String username) {
- int minChars = 10;
- int maxChars = 20;
- if (username.length() >= minChars && username.length() <= maxChars) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * Check if password is appropriate number of characters and if it meets
- * uppercase, lower case, and digit criteria.
- *
- * @param username User's entered password
- * @return true if all requirements are met or false if ANY are not.
- */
- public static boolean goodPassword(String password) {
- int pos = 0;
- int countU = 0;
- int countL = 0;
- int countD = 0;
- int minChars = 6;
- int maxChars = 16;
- /**
- * Check password length.
- */
- if (password.length() >= minChars && password.length() <= maxChars) {
- /**
- * If password meets length requirements, check for upper/lowercase,
- * number digit, and if not, skip to false.
- */
- while (pos < password.length())
- {
- char letter = password.charAt(pos);
- pos++;
- if (Character.isUpperCase(letter))
- {
- countU++;
- }
- if (Character.isLowerCase(letter)) {
- countL++;
- }
- if (Character.isDigit(letter)) {
- countD++;
- }
- }
- if (password.length() >= minChars && password.length()
- <= maxChars && countU > 0 && countL > 0 && countD > 0) {
- return true;
- }
- }
- /**
- * If ANY requirements are not met, return false.
- */
- return false;
- }
- /**
- * Check if date of birth is valid and that the birth year is 1900 or later.
- *
- * @param username User's entered date of birth
- * @return true if valid or false if invalid.
- */
- public static boolean goodDob(int dob) {
- int day, month, year, birthday = dob;
- year = birthday % 10000;
- birthday = birthday / 10000;
- day = birthday % 100;
- birthday = birthday / 100;
- month = birthday;
- if (month <= 12 && month > 0 && day <= 31 && day > 0
- && year <= 1996 && year >= 1900) {
- return true;
- } else {
- return false;
- }
- }
- /**
- * Retrieve username
- *
- * @return
- */
- public String getUsername() {
- return username;
- }
- /**
- * Set user's username
- *
- * @param u User's desired username
- */
- public void setUsername(String u) {
- username = u;
- }
- /**
- * Retrieve user's password
- *
- * @return
- */
- public String getPassword() {
- return password;
- }
- /**
- * Set user's password
- *
- * @param p User's desired password
- */
- public void setPassword(String p) {
- password = p;
- }
- /**
- * Retrieve user's date of birth
- *
- * @return
- */
- public int getDob() {
- return dob;
- }
- /**
- * Set user's date of birth
- *
- * @param d User's date of birth
- */
- public void setDob(int d) {
- dob = d;
- }
- /**
- * Retrieve user's secret question
- *
- * @return
- */
- public String getSecretQuestion() {
- return secretQuestion;
- }
- /**
- * Set user's secret question
- *
- * @param q User's secret question
- */
- public void setSecretQuestion(String q) {
- secretQuestion = q;
- }
- /**
- * Retrieve user's secret answer
- *
- * @return
- */
- public String getSecretAnswer() {
- return secretAnswer;
- }
- /**
- * Set user's answer to secret question
- *
- * @param a User's answer to secret question
- */
- public void setSecretAnswer(String a) {
- secretAnswer = a;
- }
- /**
- * Saves the user information to a text file to make sure the checkers are
- * working.
- *
- * @param u1 User account instance
- * @throws IOException
- */
- public static void saveToFile(SecondLifeAccount2 u1) throws IOException {
- File makefile = new File(FILENAME);
- FileWriter fwrite = new FileWriter(makefile, true);
- fwrite.write(u1.username + "~" + u1.password + "~"
- + u1.secretQuestion + "~" + u1.secretAnswer + "~"
- + u1.dob + "\n");
- fwrite.flush();
- fwrite.close();
- }
- /**
- * Scans text file to check if chosen username already exists.
- *
- * @param u1 User account instance
- * @return true if entered username is unique or false if it already exists
- * in .txt file.
- * @throws IOException
- */
- public static boolean existingUsername(String username)
- throws IOException {
- File makefile = new File(FILENAME);
- if (!makefile.exists()) {
- makefile.createNewFile();
- }
- Scanner fileReader = new Scanner(new File(FILENAME));
- StringTokenizer st;
- while (fileReader.hasNextLine()) {
- String line = fileReader.nextLine();
- if (line.length() >= 1) {
- st = new StringTokenizer(line, "~");
- String userN = st.nextToken();
- /**
- * Checks if any username scanned from .txt file matches
- * keyboard input.
- */
- if ((userN.equals(username))) {
- return false;
- }
- }
- }
- return true;
- }
- public static boolean existingAccount(String username, String password)
- throws IOException {
- File makefile = new File(FILENAME);
- if (!makefile.exists()) {
- makefile.createNewFile();
- }
- Scanner fileReader = new Scanner(new File(FILENAME));
- StringTokenizer st;
- while (fileReader.hasNextLine()) {
- String line = fileReader.nextLine();
- if (line.length() >= 1) {
- st = new StringTokenizer(line, "~");
- String userN = st.nextToken();
- String passW = st.nextToken();
- /**
- * Checks if any password scanned from .txt file matches
- * keyboard input.
- */
- if ((userN.equals(username) && passW.equals(password))) {
- return true;
- }
- }
- }
- return false;
- }
- }
Add Comment
Please, Sign In to add comment