Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.List;
- import java.util.ArrayList;
- import java.io.File;
- import java.io.BufferedReader;
- import java.io.FileReader;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.lang.NumberFormatException;
- import java.io.Writer;
- import java.io.BufferedWriter;
- import java.io.OutputStreamWriter;
- import java.io.FileOutputStream;
- //This class defines a player for the game.
- public class Player
- {
- private String name, surname, username, password; //encapsulated data
- private int gamesPlayed, totalScore;
- private static final String PLAYER_FILE_NAME = "player_data.csv"; // name of file storing player data
- public Player() //no-argument constructor method
- {
- this.name = "NOT SET";
- this.surname = "NOT SET";
- this.username = "NOT SET";
- this.password = "NOT SET";
- this.gamesPlayed = -1;
- this.totalScore = -1; //INITIAL VALUE FOR A PROPERLY DEFINED VALUE IS 0. SO FOR A NON-DEFINED I WILL SET IT AS -1.
- }
- //constructor with all arguments
- public Player (String name, String surname, String username, String password, int gamesPlayed, int totalScore)
- {
- this.name = name;
- this.surname = surname;
- this.username = username;
- this.password= password;
- this.gamesPlayed = gamesPlayed;
- this.totalScore = totalScore;
- }
- public Player (Player aPlayer) //copy constructor method
- {
- this.name = aPlayer.name;
- this.surname = aPlayer.surname;
- this.username = aPlayer.username;
- this.password= aPlayer.password;
- this.gamesPlayed = aPlayer.gamesPlayed;
- this.totalScore = aPlayer.totalScore;
- }
- //A series of getter methods
- public String getName()
- {
- return name; //Returns the player's name
- }
- public String getSurname()
- {
- return surname; //Returns the player's surname
- }
- public String getUsername()
- {
- return username; //Returns the player's unique username
- }
- public String getPassword()
- {
- return password; //Returns the player's password
- }
- public int getGamesPlayed()
- {
- return gamesPlayed; //Returns the number of games played of the player
- }
- public int getTotalScore()
- {
- return totalScore; //Returns the total score of the player
- }
- // set gamesPlayed to a new value
- public void setGamesPlayed(int gamesPlayed)
- {
- this.gamesPlayed = gamesPlayed;
- }
- // set totalScore to a new value
- public void setTotalScore(int totalScore)
- {
- this.totalScore = totalScore;
- }
- //a series of setter methods for debugging use
- //(game does not allow user to amend these values)
- public void setName(String theName)
- {
- if (!InputValidation.nameValidation(theName))
- {
- System.out.print("\n The name has not been changed.");
- }
- else
- {
- this.name = theName;
- }
- }
- public void setSurname(String theSurname)
- {
- if (!InputValidation.nameValidation(theSurname))
- {
- System.out.print("\n The surname has not been changed.");
- }
- else
- {
- this.surname = theSurname;
- }
- }
- public void setUsername(String theUsername)
- {
- if (!InputValidation.usernameValidation(theUsername))
- {
- System.out.print("\n The username has not been changed.");
- }
- else
- {
- this.username = theUsername;
- }
- }
- public void setPassword(String thePassword)
- {
- if (!InputValidation.passwordValidation(thePassword))
- {
- System.out.print("\n The password has not been changed.");
- }
- else
- {
- this.password = thePassword;
- }
- }
- // string representation of player, useful for writing to file and for debugging
- public String toString()
- {
- return name + "," + surname + "," + username + "," + password + "," + gamesPlayed + "," + totalScore;
- }
- // parses players file, constructs a Player from each line and returns constructed Players
- public static List<Player> readPlayersFromFile()
- {
- // create player file if it doesn't exist
- File playerFile = new File(Player.PLAYER_FILE_NAME);
- try
- {
- playerFile.createNewFile(); // does nothing if file already exists
- } catch (IOException e)
- {
- e.printStackTrace();
- System.exit(1); // exit
- }
- BufferedReader br = null;
- try
- {
- br = new BufferedReader(new FileReader(Player.PLAYER_FILE_NAME));
- List<Player> players = new ArrayList<Player>();
- String line = "";
- while ((line = br.readLine()) != null) // read lines until end of file
- {
- String[] fields = line.split(","); // split line by ","
- int parsedGamesPlayed = Integer.parseInt(fields[4]); // parse gamesPlayed from string
- int parsedTotalScore = Integer.parseInt(fields[5]); // parse totalScore from string
- Player p = new Player(fields[0], fields[1], fields[2], fields[3], parsedGamesPlayed, parsedTotalScore);
- players.add(p);
- }
- return players;
- } catch (FileNotFoundException e)
- {
- e.printStackTrace();
- System.exit(1); // exit
- } catch (IOException e)
- {
- e.printStackTrace();
- System.exit(1); // exit
- } catch (NumberFormatException e)
- {
- e.printStackTrace();
- System.exit(1); // exit
- } finally
- {
- if (br != null)
- {
- try
- {
- br.close(); // close file if it was opened
- } catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- return null; // unreachable
- }
- // writes list of players to file, one line per player
- public static void writePlayersToFile(List<Player> players)
- {
- Writer writer = null;
- try
- {
- writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(Player.PLAYER_FILE_NAME), "utf-8"));
- for (Player p: players)
- {
- writer.write(p.toString()); // write string representation of player, which contains all player attributes
- writer.write("\n"); // write new line to move cursor to the next line
- }
- } catch (IOException e)
- {
- e.printStackTrace();
- } finally
- {
- if (writer != null)
- {
- try
- {
- writer.close(); // close file if it was opened
- } catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement