Guest User

Untitled

a guest
Jul 12th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.76 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.util.Scanner;
  5. import java.util.StringTokenizer;
  6.  
  7. public class SecondLifeAccount2 {
  8.  
  9. private static String username;
  10. private String password;
  11. private int dob;
  12. private String secretQuestion;
  13. private String secretAnswer;
  14. static final String FILENAME = "SLAccount.txt";
  15.  
  16. /**
  17. * Create variables for SecondLifeAccount2 object.
  18. *
  19. * @param u user's desired username
  20. * @param p user's desired password
  21. * @param d user's date of birth
  22. * @param q user's secret question
  23. * @param a user's answer to secret question
  24. */
  25. public SecondLifeAccount2(String u, String p, int d,
  26. String q, String a) {
  27. username = u;
  28. password = p;
  29. dob = d;
  30. secretQuestion = q;
  31. secretAnswer = a;
  32.  
  33. }
  34.  
  35. /**
  36. * Check if username is appropriate number of characters.
  37. *
  38. * @param username User's entered username
  39. * @return true or false
  40. */
  41. public static boolean goodUsername(String username) {
  42. int minChars = 10;
  43. int maxChars = 20;
  44.  
  45. if (username.length() >= minChars && username.length() <= maxChars) {
  46. return true;
  47. } else {
  48. return false;
  49. }
  50. }
  51.  
  52. /**
  53. * Check if password is appropriate number of characters and if it meets
  54. * uppercase, lower case, and digit criteria.
  55. *
  56. * @param username User's entered password
  57. * @return true if all requirements are met or false if ANY are not.
  58. */
  59. public static boolean goodPassword(String password) {
  60. int pos = 0;
  61. int countU = 0;
  62. int countL = 0;
  63. int countD = 0;
  64. int minChars = 6;
  65. int maxChars = 16;
  66. /**
  67. * Check password length.
  68. */
  69. if (password.length() >= minChars && password.length() <= maxChars) {
  70. /**
  71. * If password meets length requirements, check for upper/lowercase,
  72. * number digit, and if not, skip to false.
  73. */
  74. while (pos < password.length())
  75. {
  76. char letter = password.charAt(pos);
  77. pos++;
  78. if (Character.isUpperCase(letter))
  79. {
  80. countU++;
  81. }
  82. if (Character.isLowerCase(letter)) {
  83. countL++;
  84. }
  85. if (Character.isDigit(letter)) {
  86. countD++;
  87. }
  88. }
  89. if (password.length() >= minChars && password.length()
  90. <= maxChars && countU > 0 && countL > 0 && countD > 0) {
  91. return true;
  92. }
  93. }
  94. /**
  95. * If ANY requirements are not met, return false.
  96. */
  97. return false;
  98. }
  99.  
  100. /**
  101. * Check if date of birth is valid and that the birth year is 1900 or later.
  102. *
  103. * @param username User's entered date of birth
  104. * @return true if valid or false if invalid.
  105. */
  106. public static boolean goodDob(int dob) {
  107. int day, month, year, birthday = dob;
  108. year = birthday % 10000;
  109. birthday = birthday / 10000;
  110. day = birthday % 100;
  111. birthday = birthday / 100;
  112. month = birthday;
  113.  
  114. if (month <= 12 && month > 0 && day <= 31 && day > 0
  115. && year <= 1996 && year >= 1900) {
  116. return true;
  117. } else {
  118. return false;
  119. }
  120. }
  121.  
  122. /**
  123. * Retrieve username
  124. *
  125. * @return
  126. */
  127. public String getUsername() {
  128. return username;
  129. }
  130.  
  131. /**
  132. * Set user's username
  133. *
  134. * @param u User's desired username
  135. */
  136. public void setUsername(String u) {
  137. username = u;
  138. }
  139.  
  140. /**
  141. * Retrieve user's password
  142. *
  143. * @return
  144. */
  145. public String getPassword() {
  146. return password;
  147. }
  148.  
  149. /**
  150. * Set user's password
  151. *
  152. * @param p User's desired password
  153. */
  154. public void setPassword(String p) {
  155. password = p;
  156. }
  157.  
  158. /**
  159. * Retrieve user's date of birth
  160. *
  161. * @return
  162. */
  163. public int getDob() {
  164. return dob;
  165. }
  166.  
  167. /**
  168. * Set user's date of birth
  169. *
  170. * @param d User's date of birth
  171. */
  172. public void setDob(int d) {
  173. dob = d;
  174. }
  175.  
  176. /**
  177. * Retrieve user's secret question
  178. *
  179. * @return
  180. */
  181. public String getSecretQuestion() {
  182. return secretQuestion;
  183. }
  184.  
  185. /**
  186. * Set user's secret question
  187. *
  188. * @param q User's secret question
  189. */
  190. public void setSecretQuestion(String q) {
  191. secretQuestion = q;
  192. }
  193.  
  194. /**
  195. * Retrieve user's secret answer
  196. *
  197. * @return
  198. */
  199. public String getSecretAnswer() {
  200. return secretAnswer;
  201. }
  202.  
  203. /**
  204. * Set user's answer to secret question
  205. *
  206. * @param a User's answer to secret question
  207. */
  208. public void setSecretAnswer(String a) {
  209. secretAnswer = a;
  210. }
  211.  
  212. /**
  213. * Saves the user information to a text file to make sure the checkers are
  214. * working.
  215. *
  216. * @param u1 User account instance
  217. * @throws IOException
  218. */
  219. public static void saveToFile(SecondLifeAccount2 u1) throws IOException {
  220. File makefile = new File(FILENAME);
  221. FileWriter fwrite = new FileWriter(makefile, true);
  222. fwrite.write(u1.username + "~" + u1.password + "~"
  223. + u1.secretQuestion + "~" + u1.secretAnswer + "~"
  224. + u1.dob + "\n");
  225. fwrite.flush();
  226. fwrite.close();
  227. }
  228.  
  229. /**
  230. * Scans text file to check if chosen username already exists.
  231. *
  232. * @param u1 User account instance
  233. * @return true if entered username is unique or false if it already exists
  234. * in .txt file.
  235. * @throws IOException
  236. */
  237. public static boolean existingUsername(String username)
  238. throws IOException {
  239. File makefile = new File(FILENAME);
  240. if (!makefile.exists()) {
  241. makefile.createNewFile();
  242. }
  243. Scanner fileReader = new Scanner(new File(FILENAME));
  244. StringTokenizer st;
  245.  
  246. while (fileReader.hasNextLine()) {
  247. String line = fileReader.nextLine();
  248. if (line.length() >= 1) {
  249. st = new StringTokenizer(line, "~");
  250. String userN = st.nextToken();
  251. /**
  252. * Checks if any username scanned from .txt file matches
  253. * keyboard input.
  254. */
  255. if ((userN.equals(username))) {
  256. return false;
  257. }
  258. }
  259. }
  260. return true;
  261. }
  262. public static boolean existingAccount(String username, String password)
  263. throws IOException {
  264. File makefile = new File(FILENAME);
  265. if (!makefile.exists()) {
  266. makefile.createNewFile();
  267. }
  268. Scanner fileReader = new Scanner(new File(FILENAME));
  269. StringTokenizer st;
  270.  
  271. while (fileReader.hasNextLine()) {
  272. String line = fileReader.nextLine();
  273. if (line.length() >= 1) {
  274. st = new StringTokenizer(line, "~");
  275. String userN = st.nextToken();
  276. String passW = st.nextToken();
  277. /**
  278. * Checks if any password scanned from .txt file matches
  279. * keyboard input.
  280. */
  281. if ((userN.equals(username) && passW.equals(password))) {
  282. return true;
  283. }
  284. }
  285. }
  286. return false;
  287. }
  288. }
Add Comment
Please, Sign In to add comment