| 5 | |
| 6 | class Register { |
| 7 | public static List<String> implementAPI(List<String> logs) { |
| 8 | |
| 9 | int n = logs.size(); |
| 10 | ArrayList<String> ans = new ArrayList<>(); |
| 11 | HashMap<String, String> register = new HashMap<>(); |
| 12 | HashMap<String, String> login = new HashMap<>(); |
| 13 | |
| 14 | for (int i = 0; i < n; i++) { |
| 15 | String temp = logs.get(i); |
| 16 | String[] split = temp.split("\\s+"); |
| 17 | if (split[0].equals("register")) { |
| 18 | if (register.containsKey(split[1])) |
| 19 | |
| 20 | ans.add("Username already exists"); |
| 21 | else { |
| 22 | ans.add("Registered Successfully"); |
| 23 | register.put(split[1], split[2]); |
| 24 | } |
| 25 | } else if (split[0].equals("login")) { |
| 26 | if (register. containsKey(split[1])) { |
| 27 | if ((login.containsKey(split[1]))) { |
| 28 | ans.add("Login Unsuccessful"); |
| 29 | } else { |
| 30 | if (register.get(split[1]).equals(split[2])) { |
| 31 | ans.add("Logged In Successfully"); |
| 32 | login.put(split[1], split[2]); |
| 33 | } else { |
| 34 | ans.add("Login Unsuccessful"); |
| 35 | } |
| 36 | } |
| 37 | } else { |
| 38 | ans.add("Login Unsuccessful"); |
| 39 | } |
| 40 | } else { |
| 41 | if (register. containsKey(split[1])) { |
| 42 | if ((login.containsKey(split[1]))) { |
| 43 | ans.add("Logged Out Successfully"); |
| 44 | login.remove(split[1]); |
| 45 | } else { |
| 46 | ans.add("Logout Unsuccessful"); |
| 47 | } |
| 48 | } else { |
| 49 | ans.add("Logout Unsuccessful"); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | } |
| 54 | |
| 55 | return ans; |
| 56 | } |
| 57 | |
| 58 | public static void main(String[] args) { |
| 59 | List<String> logs = new ArrayList<>(); |