Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2017
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1. import java.nio.charset.StandardCharsets;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import java.io.*;
  5. import java.nio.file.*;
  6. import java.util.Scanner;
  7.  
  8. /* @author Marilena Eleftheriou
  9. */
  10. public class User
  11. {
  12. private String name;
  13. private String surname;
  14. private String username;
  15. private String password;
  16. private int timesPlayed = 0;
  17. private int currentScore = 0;
  18. private int wrongAnswers = 0;
  19. private int questionsSkipped = 0;
  20. private int questionsAnswered = 0;
  21. private int highscore;
  22. private int userLine = -1;
  23. public static boolean Registration;
  24.  
  25. private final static int MIN_USERNAME = 5; //sets minimum username characters to 5
  26. private final static int MIN_PASSWORD = 8; //sets minimum password characters to 8
  27. private final static int MAX_CHAR = 15; //sets maximum username and password characters to 15
  28.  
  29. public User ()//allow user class to be called
  30. {
  31. this("","");
  32. }
  33.  
  34. public User (String username, String password)//copy constructor that sets the username and password
  35. {
  36. this.username=username;
  37. this.password=password;
  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. saveData();
  53. } else
  54. {
  55. this.username = "";
  56. System.out.println("Username already exists!");
  57. }
  58.  
  59. }
  60.  
  61. public String getName() //formats name
  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)//checks 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)//checks 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. usernameError=false;
  88. }
  89.  
  90. public void setPassword()
  91. {
  92. do
  93. {
  94. Scanner scan = new Scanner(System.in);
  95. System.out.println("Enter your password:");
  96. System.out.print("(Your password must have 8-15 characters");
  97. System.out.println("and must contain at least one letter and one digit)");
  98. password= scan.nextLine();
  99. }
  100. while ((password.length() < MIN_PASSWORD) || (password.length() > MAX_CHAR) || (!password.matches(".*[0-9]+.*")));
  101.  
  102. System.out.println("You have now been registered!");
  103.  
  104. }
  105.  
  106. public String[] readUserData() //creates ArrayList of users
  107. {
  108. ArrayList<String> users = new ArrayList<String>();
  109. try (Scanner inputStream = new Scanner(new FileInputStream("userInfo.txt")))
  110. {
  111. while (inputStream.hasNextLine())
  112. {
  113. String line = inputStream.nextLine();
  114. users.add(line);
  115. }
  116. }
  117. catch(FileNotFoundException e)
  118. {
  119. return null;
  120. }
  121.  
  122. int x = 0;
  123. for (String u : users)
  124. {
  125. String[] userInfo = u.split(",");
  126. if (userInfo[2].equals(username))
  127. {
  128. userLine = x;
  129. return userInfo;
  130. }
  131. x++;
  132. }
  133.  
  134. return null;
  135. }
  136.  
  137. public boolean isLoginCorrect()
  138. {
  139. String[] userData = readUserData();
  140. if (userData != null) {
  141. if (userData[3].equals(password))
  142. {
  143. highscore = Integer.parseInt(userData[5]);//converts String of highscore into int
  144. this.name = userData[0];
  145. this.surname = userData[1];
  146. this.timesPlayed = Integer.parseInt(userData[4]) + this.timesPlayed;
  147. return true;
  148. }
  149. return false;
  150. }
  151. return false;
  152. }
  153.  
  154. public int getCurrentScore()
  155. {
  156. return this.currentScore;
  157. }
  158.  
  159. public void increaseScore()//increases the current score for every correct answer
  160. //and updates the high score if it is exceeded
  161. {
  162. currentScore++;
  163. if (currentScore > highscore)
  164. {
  165. highscore = currentScore;
  166. }
  167. }
  168.  
  169. public void increaseWrongAnswers()
  170. {
  171. wrongAnswers++;
  172. }
  173.  
  174. public void increaseQuestionsSkipped()
  175. {
  176. questionsSkipped++;
  177. }
  178.  
  179. public void increaseTimesPlayed() //counter denoting times played
  180. {
  181. timesPlayed++;
  182. }
  183.  
  184. public void reset ()
  185. {
  186. currentScore=0;
  187. wrongAnswers=0;
  188. questionsSkipped=0;
  189. }
  190.  
  191. public void printResults()//prints out current stats
  192. {
  193. System.out.println("Questions Answered: " + (wrongAnswers + currentScore) + "/10");
  194. System.out.println("Questions Skipped: " + questionsSkipped + "/10" + "\n");
  195. System.out.println("Current Score: " + getCurrentScore() + "/10");
  196. }
  197.  
  198. public void saveData()
  199. {
  200. String[] userData = readUserData();
  201. if (userData == null)
  202. {
  203. PrintWriter outputStream = null;
  204. try
  205. {
  206. outputStream = new PrintWriter(new FileOutputStream("userInfo.txt", true));
  207. }
  208. catch (FileNotFoundException e)
  209. {
  210. System.out.println("Error opening the file userInfo.txt.");
  211. System.exit(0);
  212. }
  213. outputStream.println(toString());
  214.  
  215. outputStream.close();
  216. }
  217. else {
  218. try
  219. {
  220. Path path = Paths.get("userInfo.txt");
  221. List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
  222. lines.set(userLine, toString());
  223. Files.write(path, lines, StandardCharsets.UTF_8);
  224. }
  225. catch (IOException e)
  226. {
  227. System.out.println("Error updating values.");
  228. System.exit(0);
  229. }
  230. }
  231. }
  232.  
  233. public String getUsername()
  234. {
  235. return this.username;
  236. }
  237.  
  238. public String getPassword()
  239. {
  240. return this.password;
  241. }
  242.  
  243. @Override
  244. public String toString()
  245. {
  246. return (getName() + "," + getUsername() + "," + getPassword() + "," + timesPlayed + "," + highscore);
  247. }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement