Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.nio.charset.StandardCharsets;
- import java.util.ArrayList;
- import java.util.List;
- import java.io.*;
- import java.nio.file.*;
- import java.util.Scanner;
- /* @author Marilena Eleftheriou
- */
- public class User
- {
- private String name;
- private String surname;
- private String username;
- private String password;
- private int timesPlayed = 0;
- private int currentScore = 0;
- private int wrongAnswers = 0;
- private int questionsSkipped = 0;
- private int questionsAnswered = 0;
- private int highscore;
- private int userLine = -1;
- public static boolean Registration;
- private final static int MIN_USERNAME = 5; //sets minimum username characters to 5
- private final static int MIN_PASSWORD = 8; //sets minimum password characters to 8
- private final static int MAX_CHAR = 15; //sets maximum username and password characters to 15
- public User ()//allow user class to be called
- {
- this("","");
- }
- public User (String username, String password)//copy constructor that sets the username and password
- {
- this.username=username;
- this.password=password;
- }
- public void register()
- {
- Scanner scan = new Scanner(System.in);
- System.out.println("Enter your first name: ");
- this.name=scan.nextLine();
- System.out.println("Enter your last name: ");
- this.surname=scan.nextLine();
- setUsername();
- if (readUserData() == null) {
- setPassword();
- saveData();
- } else
- {
- this.username = "";
- System.out.println("Username already exists!");
- }
- }
- public String getName() //formats name
- {
- return (name + "," + surname);
- }
- public void setUsername()
- {
- boolean usernameError=false;
- Scanner scan = new Scanner(System.in);
- System.out.println("Enter a username between 5-15 characters:");
- username= scan.nextLine();
- while (username.length()<MIN_USERNAME)//checks if the username is too short
- {
- usernameError=true;
- System.out.println("Username too short");
- System.out.println("Try again: ");
- username= scan.nextLine();
- }
- while (username.length()>MAX_CHAR)//checks if the username is too long
- {
- usernameError=true;
- System.out.println("Username too long");
- System.out.println("Try again: ");
- username= scan.nextLine();
- }
- usernameError=false;
- }
- public void setPassword()
- {
- do
- {
- Scanner scan = new Scanner(System.in);
- System.out.println("Enter your password:");
- System.out.print("(Your password must have 8-15 characters");
- System.out.println("and must contain at least one letter and one digit)");
- password= scan.nextLine();
- }
- while ((password.length() < MIN_PASSWORD) || (password.length() > MAX_CHAR) || (!password.matches(".*[0-9]+.*")));
- System.out.println("You have now been registered!");
- }
- public String[] readUserData() //creates ArrayList of users
- {
- ArrayList<String> users = new ArrayList<String>();
- try (Scanner inputStream = new Scanner(new FileInputStream("userInfo.txt")))
- {
- while (inputStream.hasNextLine())
- {
- String line = inputStream.nextLine();
- users.add(line);
- }
- }
- catch(FileNotFoundException e)
- {
- return null;
- }
- int x = 0;
- for (String u : users)
- {
- String[] userInfo = u.split(",");
- if (userInfo[2].equals(username))
- {
- userLine = x;
- return userInfo;
- }
- x++;
- }
- return null;
- }
- public boolean isLoginCorrect()
- {
- String[] userData = readUserData();
- if (userData != null) {
- if (userData[3].equals(password))
- {
- highscore = Integer.parseInt(userData[5]);//converts String of highscore into int
- this.name = userData[0];
- this.surname = userData[1];
- this.timesPlayed = Integer.parseInt(userData[4]) + this.timesPlayed;
- return true;
- }
- return false;
- }
- return false;
- }
- public int getCurrentScore()
- {
- return this.currentScore;
- }
- public void increaseScore()//increases the current score for every correct answer
- //and updates the high score if it is exceeded
- {
- currentScore++;
- if (currentScore > highscore)
- {
- highscore = currentScore;
- }
- }
- public void increaseWrongAnswers()
- {
- wrongAnswers++;
- }
- public void increaseQuestionsSkipped()
- {
- questionsSkipped++;
- }
- public void increaseTimesPlayed() //counter denoting times played
- {
- timesPlayed++;
- }
- public void reset ()
- {
- currentScore=0;
- wrongAnswers=0;
- questionsSkipped=0;
- }
- public void printResults()//prints out current stats
- {
- System.out.println("Questions Answered: " + (wrongAnswers + currentScore) + "/10");
- System.out.println("Questions Skipped: " + questionsSkipped + "/10" + "\n");
- System.out.println("Current Score: " + getCurrentScore() + "/10");
- }
- public void saveData()
- {
- String[] userData = readUserData();
- if (userData == null)
- {
- PrintWriter outputStream = null;
- try
- {
- outputStream = new PrintWriter(new FileOutputStream("userInfo.txt", true));
- }
- catch (FileNotFoundException e)
- {
- System.out.println("Error opening the file userInfo.txt.");
- System.exit(0);
- }
- outputStream.println(toString());
- outputStream.close();
- }
- else {
- try
- {
- Path path = Paths.get("userInfo.txt");
- List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
- lines.set(userLine, toString());
- Files.write(path, lines, StandardCharsets.UTF_8);
- }
- catch (IOException e)
- {
- System.out.println("Error updating values.");
- System.exit(0);
- }
- }
- }
- public String getUsername()
- {
- return this.username;
- }
- public String getPassword()
- {
- return this.password;
- }
- @Override
- public String toString()
- {
- return (getName() + "," + getUsername() + "," + getPassword() + "," + timesPlayed + "," + highscore);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement