Guest User

Untitled

a guest
Jul 6th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 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 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 or false
  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. if (password.length() >= minChars && password.length() <= maxChars) {
  68. while (pos < password.length()) {
  69. char letter = password.charAt(pos);
  70. pos++;
  71. if (Character.isUpperCase(letter)) {
  72. countU++;
  73. }
  74. if (Character.isLowerCase(letter)) {
  75. countL++;
  76. }
  77. if (Character.isDigit(letter)) {
  78. countD++;
  79. }
  80. }
  81. if (password.length() >= minChars && password.length()
  82. <= maxChars && countU > 0 && countL > 0 && countD > 0) {
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88.  
  89. /**
  90. * Check if date of birth is valid and that the birth year is 1900 or later.
  91. *
  92. * @param username User's entered date of birth
  93. * @return true or false
  94. */
  95. public static boolean goodDob(int dob) {
  96. int day, month, year, birthday = dob;
  97. year = birthday % 10000;
  98. birthday = birthday / 10000;
  99. day = birthday % 100;
  100. birthday = birthday / 100;
  101. month = birthday;
  102.  
  103. if (month <= 12 && month > 0 && day <= 31 && day > 0
  104. && year <= 1996 && year >= 1900) {
  105. return true;
  106. } else {
  107. return false;
  108. }
  109. }
  110.  
  111. /**
  112. * Retrieve username
  113. *
  114. * @return
  115. */
  116. public String getUsername() {
  117. return username;
  118. }
  119.  
  120. /**
  121. * Set user's username
  122. *
  123. * @param u User's desired username
  124. */
  125. public void setUsername(String u) {
  126. username = u;
  127. }
  128.  
  129. /**
  130. * Retrieve user's password
  131. *
  132. * @return
  133. */
  134. public String getPassword() {
  135. return password;
  136. }
  137.  
  138. /**
  139. * Set user's password
  140. *
  141. * @param p User's desired password
  142. */
  143. public void setPassword(String p) {
  144. password = p;
  145. }
  146.  
  147. /**
  148. * Retrieve user's date of birth
  149. *
  150. * @return
  151. */
  152. public int getDob() {
  153. return dob;
  154. }
  155.  
  156. /**
  157. * Set user's date of birth
  158. *
  159. * @param d User's date of birth
  160. */
  161. public void setDob(int d) {
  162. dob = d;
  163. }
  164.  
  165. /**
  166. * Retrieve user's secret question
  167. *
  168. * @return
  169. */
  170. public String getSecretQuestion() {
  171. return secretQuestion;
  172. }
  173.  
  174. /**
  175. * Set user's secret question
  176. *
  177. * @param q User's secret question
  178. */
  179. public void setSecretQuestion(String q) {
  180. secretQuestion = q;
  181. }
  182.  
  183. /**
  184. * Retrieve user's secret answer
  185. *
  186. * @return
  187. */
  188. public String getSecretAnswer() {
  189. return secretAnswer;
  190. }
  191.  
  192. /**
  193. * Set user's answer to secret question
  194. *
  195. * @param a User's answer to secret question
  196. */
  197. public void setSecretAnswer(String a) {
  198. secretAnswer = a;
  199. }
  200.  
  201. /**
  202. * Saves the user information to a text file to make sure the checkers are
  203. * working.
  204. *
  205. * @param u1 User account instance
  206. * @throws IOException
  207. */
  208. public static void saveToFile(SecondLifeAccount2 u1) throws IOException {
  209. File makefile = new File(FILENAME);
  210. FileWriter fwrite = new FileWriter(makefile, true);
  211. fwrite.write(u1.username + "~" + u1.password + "~"
  212. + u1.secretQuestion + "~" + u1.secretAnswer + "~"
  213. + u1.dob + "\n");
  214. fwrite.flush();
  215. fwrite.close();
  216. }
  217.  
  218. /**
  219. * Scans text file to check if chosen username already exists.
  220. *
  221. * @param u1 User account instance
  222. * @return true or false
  223. * @throws IOException
  224. */
  225. public static boolean uniqueUsername(SecondLifeAccount2 u1)
  226. throws IOException {
  227. Scanner fileReader = new Scanner(new File(FILENAME));
  228. StringTokenizer st;
  229.  
  230. while (fileReader.hasNextLine()) {
  231. String line = fileReader.nextLine();
  232. if (line.length() >= 1) {
  233. st = new StringTokenizer(line, "~");
  234. String username = st.nextToken();
  235. String password = st.nextToken();
  236. String secretQuestion = st.nextToken();
  237. String secretAnswer = st.nextToken();
  238. String dob = st.nextToken();
  239. int intDOb = Integer.parseInt(dob);
  240.  
  241. if ((username.equals(u1.username))) {
  242. return false;
  243. }
  244. }
  245. }
  246. return true;
  247. }
  248. }
Add Comment
Please, Sign In to add comment