Advertisement
Guest User

User

a guest
Dec 3rd, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.18 KB | None | 0 0
  1.  
  2. import java.nio.charset.StandardCharsets;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.io.*;
  6. import java.nio.file.*;
  7. import java.util.Scanner;
  8.  
  9. public class User
  10. {
  11. private String name;
  12. private String surname;
  13. private String username;
  14. private String password;
  15. private int timesPlayed = 0;
  16. private int currentScore = 0;
  17. private int highscore;
  18. private int userLine = -1;
  19. public static boolean Registration;
  20.  
  21. private final static int MIN_USERNAME = 5; //sets minimum username characters to 5
  22. private final static int MIN_PASSWORD = 8; //sets minimum password characters to 8
  23. private final static int MAX_CHAR = 15; //sets maximum username and password characters to 15
  24.  
  25.  
  26.  
  27. public User ()
  28. {
  29. this("","");
  30. }
  31.  
  32. public User (String username, String password)
  33. {
  34. this.username=username;
  35. this.password=password;
  36. }
  37.  
  38.  
  39.  
  40. public void register()
  41. {
  42.  
  43. Scanner scan = new Scanner(System.in);
  44. System.out.println("Enter your first name: ");
  45. this.name=scan.nextLine();
  46. System.out.println("Enter your last name: ");
  47. this.surname=scan.nextLine();
  48.  
  49. setUsername();
  50. if (readUserData() == null) {
  51. setPassword();
  52. UserInfo.addUser (username,password);
  53. saveData();
  54. } else {
  55. this.username = "";
  56. System.out.println("Username already exists!");
  57. }
  58.  
  59. }
  60.  
  61. public String getName()
  62. {
  63. return (name + "," + surname);
  64. }
  65.  
  66. public void setUsername()
  67. {
  68. boolean usernameError=false;
  69. Scanner scan = new Scanner(System.in);
  70. System.out.println("Enter a username between 5-15 characters:");
  71. username= scan.nextLine();
  72. while (username.length()<MIN_USERNAME)//if the username is too short
  73. {
  74. usernameError=true;
  75. System.out.println("Username too short");
  76. System.out.println("Try again: ");
  77. username= scan.nextLine();
  78. }
  79. while (username.length()>MAX_CHAR)//if the username is too long
  80. {
  81. usernameError=true;
  82. System.out.println("Username too long");
  83. System.out.println("Try again: ");
  84. username= scan.nextLine();
  85. }
  86.  
  87. while (UserInfo.usernameExists(username))
  88. {
  89. System.out.println("Username taken!");
  90. System.out.println("Choose another one: ");
  91. username= scan.nextLine();
  92. }
  93.  
  94. usernameError=false;
  95. }
  96.  
  97. public String[] readUserData() {
  98. ArrayList<String> users = new ArrayList<String>();
  99. try (Scanner inputStream = new Scanner(new FileInputStream("userInfo.txt"))) {
  100. while (inputStream.hasNextLine()) {
  101. String line = inputStream.nextLine();
  102. users.add(line);
  103. }
  104. }
  105. catch(FileNotFoundException e)
  106. {
  107. // System.out.println("File userInfo.txt was not found");
  108. // System.out.println("or could not be opened.");
  109. // System.exit(0);
  110. return null;
  111. }
  112.  
  113. int x = 0;
  114. for (String u : users) {
  115. String[] userInfo = u.split(",");
  116. if (userInfo[2].equals(username)) {
  117. userLine = x;
  118. return userInfo;
  119. }
  120. x++;
  121. }
  122.  
  123. return null;
  124.  
  125. }
  126.  
  127. public void setPassword()
  128. {
  129. do
  130. {
  131. Scanner scan = new Scanner(System.in);
  132. System.out.println("Enter your password:");
  133. System.out.print("(Your password must have 8-15 characters");
  134. System.out.println("and must contain at least one letter and one digit)");
  135. password= scan.nextLine();
  136. }
  137. while ((password.length() < MIN_PASSWORD) || (password.length() > MAX_CHAR) || (!password.matches(".*[0-9]+.*")));
  138.  
  139. //Registration=true;
  140. System.out.println("You have now been registered!");
  141.  
  142. }
  143. public String getUsername()
  144. {
  145. return this.username;
  146. }
  147.  
  148. public String getPassword()
  149. {
  150. return this.password;
  151. }
  152. public void increaseScore()
  153. {
  154. currentScore++;
  155. if (currentScore > highscore) {
  156. highscore = currentScore;
  157. }
  158. }
  159.  
  160. public void increaseTimesPlayed() {
  161. timesPlayed++;
  162. }
  163.  
  164. public int getCurrentScore() {
  165. return this.currentScore;
  166. }
  167.  
  168. public boolean isLoginCorrect() {
  169. String[] userData = readUserData();
  170. if (userData != null) {
  171. if (userData[3].equals(password)) {
  172. highscore = Integer.parseInt(userData[5]);
  173. this.name = userData[0];
  174. this.surname = userData[1];
  175. this.timesPlayed = Integer.parseInt(userData[4]) + this.timesPlayed;
  176. return true;
  177. }
  178. return false;
  179. }
  180. return false;
  181. }
  182.  
  183. public void saveData() {
  184. String[] userData = readUserData();
  185. if (userData == null) {
  186. PrintWriter outputStream = null;
  187. try
  188. {
  189. outputStream = new PrintWriter(new FileOutputStream("userInfo.txt", true));
  190. }
  191. catch (FileNotFoundException e)
  192. {
  193. System.out.println("Error opening the file userInfo.txt.");
  194. System.exit(0);
  195. }
  196. outputStream.println(toString());
  197.  
  198. outputStream.close();
  199. } else {
  200. try {
  201. Path path = Paths.get("userInfo.txt");
  202. List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
  203. lines.set(userLine, toString());
  204. Files.write(path, lines, StandardCharsets.UTF_8);
  205. } catch (IOException e) {
  206. System.out.println("Error updating values.");
  207. System.exit(0);
  208. }
  209.  
  210. }
  211.  
  212. }
  213.  
  214. @Override
  215. public String toString()
  216. {
  217. return (getName() + "," + getUsername() + "," + getPassword() + "," + timesPlayed + "," + highscore);
  218. }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement