Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- String username;
- String password;
- System.out.println("Enter your username");
- username = input.nextLine();
- System.out.println("Enter your password");
- password = input.nextLine();
- UserList test2 = new UserList();
- if (test2.authenticate(username, password) == true) {
- System.out.println("Hi");
- } else {
- System.out.println("Username or/and password are wrong.");
- }
- }
- }
- public class User {
- protected String username;
- protected String password;
- public User(String username, String password) {
- this.password = password;
- this.username = username;
- }
- public String getUsername() {
- return username;
- }
- public String getPassword() {
- return password;
- }
- @Override
- public boolean equals(Object o) {
- if (o instanceof User) {
- return ((User) o).username.equals(username);
- }
- return false;
- }
- @Override
- public int hashCode() {
- int hash = 7;
- hash = 97 * hash + Objects.hashCode(this.username);
- return hash;
- }
- }
- public class UserList {
- private HashSet<User> loginList;emphasized text
- public UserList() {
- Scanner scan;
- loginList = new HashSet();
- try {
- scan = new Scanner(new File("src/boitedejeux/Logins.txt"));
- String ligne = scan.nextLine();
- while (scan.hasNext()) {
- ligne = scan.nextLine();
- String[] res = ligne.split(",");
- loginList.add(new User(res[0], (res[1])));
- }
- } catch (FileNotFoundException e) {
- System.out.println("Erreur");
- }
- }
- public boolean authenticate(String username, String password) {
- if (null == loginList) {
- throw new IllegalStateException("The user list isn't initialised");
- }
- return loginList.stream()
- .filter(usern -> usern.getUsername().equals(username))
- .filter(passw -> passw.getPassword().equals(password))
- .findFirst()
- .isPresent();
- }
- }
- Test, Password
- Test2, Password2
Add Comment
Please, Sign In to add comment