Guest User

Untitled

a guest
May 25th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. public static void main(String[] args) {
  2. Scanner input = new Scanner(System.in);
  3. String username;
  4. String password;
  5. System.out.println("Enter your username");
  6. username = input.nextLine();
  7. System.out.println("Enter your password");
  8. password = input.nextLine();
  9. UserList test2 = new UserList();
  10. if (test2.authenticate(username, password) == true) {
  11. System.out.println("Hi");
  12. } else {
  13. System.out.println("Username or/and password are wrong.");
  14. }
  15. }
  16.  
  17. }
  18.  
  19. public class User {
  20.  
  21. protected String username;
  22. protected String password;
  23.  
  24. public User(String username, String password) {
  25. this.password = password;
  26. this.username = username;
  27. }
  28.  
  29. public String getUsername() {
  30. return username;
  31. }
  32.  
  33. public String getPassword() {
  34. return password;
  35. }
  36.  
  37. @Override
  38. public boolean equals(Object o) {
  39. if (o instanceof User) {
  40. return ((User) o).username.equals(username);
  41. }
  42. return false;
  43. }
  44.  
  45. @Override
  46. public int hashCode() {
  47. int hash = 7;
  48. hash = 97 * hash + Objects.hashCode(this.username);
  49. return hash;
  50. }
  51. }
  52.  
  53. public class UserList {
  54.  
  55. private HashSet<User> loginList;emphasized text
  56.  
  57. public UserList() {
  58. Scanner scan;
  59. loginList = new HashSet();
  60. try {
  61. scan = new Scanner(new File("src/boitedejeux/Logins.txt"));
  62. String ligne = scan.nextLine();
  63. while (scan.hasNext()) {
  64. ligne = scan.nextLine();
  65. String[] res = ligne.split(",");
  66. loginList.add(new User(res[0], (res[1])));
  67. }
  68. } catch (FileNotFoundException e) {
  69. System.out.println("Erreur");
  70. }
  71. }
  72.  
  73. public boolean authenticate(String username, String password) {
  74. if (null == loginList) {
  75. throw new IllegalStateException("The user list isn't initialised");
  76. }
  77.  
  78. return loginList.stream()
  79. .filter(usern -> usern.getUsername().equals(username))
  80. .filter(passw -> passw.getPassword().equals(password))
  81. .findFirst()
  82. .isPresent();
  83. }
  84. }
  85.  
  86. Test, Password
  87. Test2, Password2
Add Comment
Please, Sign In to add comment