Advertisement
Guest User

Untitled

a guest
Nov 28th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.22 KB | None | 0 0
  1. import java.util.List;
  2. import java.util.ArrayList;
  3. import java.io.File;
  4. import java.io.BufferedReader;
  5. import java.io.FileReader;
  6. import java.io.FileNotFoundException;
  7. import java.io.IOException;
  8. import java.lang.NumberFormatException;
  9. import java.io.Writer;
  10. import java.io.BufferedWriter;
  11. import java.io.OutputStreamWriter;
  12. import java.io.FileOutputStream;
  13.  
  14. //This class defines a player for the game.
  15. public class Player
  16. {
  17. private String name, surname, username, password; //encapsulated data
  18. private int gamesPlayed, totalScore;
  19.  
  20. private static final String PLAYER_FILE_NAME = "player_data.csv"; // name of file storing player data
  21.  
  22. public Player() //no-argument constructor method
  23. {
  24. this.name = "NOT SET";
  25. this.surname = "NOT SET";
  26. this.username = "NOT SET";
  27. this.password = "NOT SET";
  28. this.gamesPlayed = -1;
  29. this.totalScore = -1; //INITIAL VALUE FOR A PROPERLY DEFINED VALUE IS 0. SO FOR A NON-DEFINED I WILL SET IT AS -1.
  30. }
  31.  
  32. //constructor with all arguments
  33. public Player (String name, String surname, String username, String password, int gamesPlayed, int totalScore)
  34. {
  35. this.name = name;
  36. this.surname = surname;
  37. this.username = username;
  38. this.password= password;
  39. this.gamesPlayed = gamesPlayed;
  40. this.totalScore = totalScore;
  41. }
  42.  
  43. public Player (Player aPlayer) //copy constructor method
  44. {
  45. this.name = aPlayer.name;
  46. this.surname = aPlayer.surname;
  47. this.username = aPlayer.username;
  48. this.password= aPlayer.password;
  49. this.gamesPlayed = aPlayer.gamesPlayed;
  50. this.totalScore = aPlayer.totalScore;
  51. }
  52.  
  53. //A series of getter methods
  54. public String getName()
  55. {
  56. return name; //Returns the player's name
  57. }
  58.  
  59. public String getSurname()
  60. {
  61. return surname; //Returns the player's surname
  62. }
  63.  
  64. public String getUsername()
  65. {
  66. return username; //Returns the player's unique username
  67. }
  68.  
  69. public String getPassword()
  70. {
  71. return password; //Returns the player's password
  72. }
  73.  
  74. public int getGamesPlayed()
  75. {
  76. return gamesPlayed; //Returns the number of games played of the player
  77. }
  78.  
  79. public int getTotalScore()
  80. {
  81. return totalScore; //Returns the total score of the player
  82. }
  83.  
  84. // set gamesPlayed to a new value
  85. public void setGamesPlayed(int gamesPlayed)
  86. {
  87. this.gamesPlayed = gamesPlayed;
  88. }
  89.  
  90. // set totalScore to a new value
  91. public void setTotalScore(int totalScore)
  92. {
  93. this.totalScore = totalScore;
  94. }
  95.  
  96. //a series of setter methods for debugging use
  97. //(game does not allow user to amend these values)
  98. public void setName(String theName)
  99. {
  100. if (!InputValidation.nameValidation(theName))
  101. {
  102. System.out.print("\n The name has not been changed.");
  103. }
  104. else
  105. {
  106. this.name = theName;
  107. }
  108. }
  109.  
  110. public void setSurname(String theSurname)
  111. {
  112. if (!InputValidation.nameValidation(theSurname))
  113. {
  114. System.out.print("\n The surname has not been changed.");
  115. }
  116. else
  117. {
  118. this.surname = theSurname;
  119. }
  120. }
  121.  
  122. public void setUsername(String theUsername)
  123. {
  124. if (!InputValidation.usernameValidation(theUsername))
  125. {
  126. System.out.print("\n The username has not been changed.");
  127. }
  128. else
  129. {
  130. this.username = theUsername;
  131. }
  132. }
  133.  
  134. public void setPassword(String thePassword)
  135. {
  136. if (!InputValidation.passwordValidation(thePassword))
  137. {
  138. System.out.print("\n The password has not been changed.");
  139. }
  140. else
  141. {
  142. this.password = thePassword;
  143. }
  144. }
  145.  
  146. // string representation of player, useful for writing to file and for debugging
  147. public String toString()
  148. {
  149. return name + "," + surname + "," + username + "," + password + "," + gamesPlayed + "," + totalScore;
  150. }
  151.  
  152. // parses players file, constructs a Player from each line and returns constructed Players
  153. public static List<Player> readPlayersFromFile()
  154. {
  155. // create player file if it doesn't exist
  156. File playerFile = new File(Player.PLAYER_FILE_NAME);
  157. try
  158. {
  159. playerFile.createNewFile(); // does nothing if file already exists
  160. } catch (IOException e)
  161. {
  162. e.printStackTrace();
  163. System.exit(1); // exit
  164. }
  165.  
  166. BufferedReader br = null;
  167. try
  168. {
  169. br = new BufferedReader(new FileReader(Player.PLAYER_FILE_NAME));
  170. List<Player> players = new ArrayList<Player>();
  171. String line = "";
  172. while ((line = br.readLine()) != null) // read lines until end of file
  173. {
  174. String[] fields = line.split(","); // split line by ","
  175. int parsedGamesPlayed = Integer.parseInt(fields[4]); // parse gamesPlayed from string
  176. int parsedTotalScore = Integer.parseInt(fields[5]); // parse totalScore from string
  177. Player p = new Player(fields[0], fields[1], fields[2], fields[3], parsedGamesPlayed, parsedTotalScore);
  178. players.add(p);
  179. }
  180. return players;
  181. } catch (FileNotFoundException e)
  182. {
  183. e.printStackTrace();
  184. System.exit(1); // exit
  185. } catch (IOException e)
  186. {
  187. e.printStackTrace();
  188. System.exit(1); // exit
  189. } catch (NumberFormatException e)
  190. {
  191. e.printStackTrace();
  192. System.exit(1); // exit
  193. } finally
  194. {
  195. if (br != null)
  196. {
  197. try
  198. {
  199. br.close(); // close file if it was opened
  200. } catch (IOException e)
  201. {
  202. e.printStackTrace();
  203. }
  204. }
  205. }
  206. return null; // unreachable
  207. }
  208.  
  209. // writes list of players to file, one line per player
  210. public static void writePlayersToFile(List<Player> players)
  211. {
  212. Writer writer = null;
  213. try
  214. {
  215. writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(Player.PLAYER_FILE_NAME), "utf-8"));
  216. for (Player p: players)
  217. {
  218. writer.write(p.toString()); // write string representation of player, which contains all player attributes
  219. writer.write("\n"); // write new line to move cursor to the next line
  220. }
  221. } catch (IOException e)
  222. {
  223. e.printStackTrace();
  224. } finally
  225. {
  226. if (writer != null)
  227. {
  228. try
  229. {
  230. writer.close(); // close file if it was opened
  231. } catch (IOException e)
  232. {
  233. e.printStackTrace();
  234. }
  235. }
  236. }
  237. }
  238.  
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement