import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; class AuthService { private Map userList = new HashMap<>(); private List lastLogin = new ArrayList<>(); static AuthService obj = new AuthService(); private AuthService() { } public static AuthService getObject() { return obj; } public String check(String password) { if (password.length() < 8) { return "Password Not Valid!"; } char[] b = password.toCharArray(); for (int i = 0; i < b.length; i++) { if (Character.isUpperCase(b[i]) == true) { break; } else if (i == b.length - 1) { return "Password Not Valid!"; } } for (int j = 0; j < b.length; j++) { if (Character.isLowerCase(b[j]) == true) { break; } else if (j == b.length - 1) { return "Password Not Valid!"; } } for (int k = 0; k < b.length; k++) { if (Character.isDigit(b[k]) == true) { break; } else if (k == b.length - 1) { return "Password Not Valid!"; } } for (int l = 0; l < b.length; l++) { if (b[l] == '@' || b[l] == '#' || b[l] == '$' || b[l] == '%' || b[l] == '^' || b[l] == '&' || b[l] == '+' || b[l] == '=') { break; } else if (l == b.length - 1) { return "Password Not Valid!"; } } return ""; } public void signup(String username, String password) { if (userList.size() == 0) { if (check(password).equals("Password Not Valid!")) { System.out.println("Password Not Valid!"); return; } else { System.out.println("SignUp Successful!"); User u = new User(username, password, 0); userList.put(u.getUsername(), u); Leaderboard.getObject().addUser(u); return; } } else { if (userList.keySet().contains(username)) { System.out.println("Username Exists!"); return; } else { if (check(password).equals("Password Not Valid!")) { System.out.println("Password Not Valid!"); return; } else { System.out.println("SignUp Successful!"); User u = new User(username, password, 0); Leaderboard.getObject().addUser(u); userList.put(u.getUsername(), u); return; } } } } public String login(String username, String password) { if (userList.size() == 0) { System.out.println("Invalid Username or Password!"); return ("Invalid Username or Password!"); } else { if (userList.keySet().contains(username)) { if ((userList.get(username).getEncryptedPassword().equals(password))) { System.out.println("Login Successful!"); lastLogin.add(userList.get(username)); return ("Login Successful!"); } else { System.out.println("Invalid Username or Password!"); return ("Invalid Username or Password!"); } } System.out.println("Invalid Username or Password!"); return ("Invalid Username or Password!"); } } public Set getUsers() { return (userList.keySet()); } public User getlastlogin() { return (lastLogin.get(lastLogin.size() - 1)); } }