fork(1) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Ideone {
  6. public static void main (String[] args) {
  7.  
  8. //create an DB for online and reged users
  9. Map DB = new HashMap<String, String> ();
  10. HashSet <String> DBin = new HashSet <String> ();
  11.  
  12. Scanner in = new Scanner (System.in);
  13. PrintWriter out = new PrintWriter(System.out);
  14.  
  15. //a number of queries
  16. int n = in.nextInt();
  17.  
  18.  
  19. for (int i = 0; i < n; i++){
  20. String s, log, pass;
  21.  
  22. //command
  23. s = in.next();
  24.  
  25. if (s.equals("register")){
  26. log = in.next(); //login
  27. pass = in.next(); //password
  28. if (DB.containsKey(log)){ //login in DB?
  29. out.println("fail: user already exists");
  30. out.flush();
  31. }
  32. else {
  33. DB.put(log, pass);
  34. out.println("success: new user added");
  35. out.flush();
  36. }
  37. }
  38.  
  39. if (s.equals("login")){
  40. log = in.next();
  41. pass = in.next();
  42. if (DB.containsKey(log)){ //if user is reg
  43. if (pass.equals(DB.get(log))) { //if the password is well
  44. if (!DBin.contains(log)) { //online?
  45. DBin.add(log);
  46. out.println("success: user logged in");
  47. out.flush();
  48. }
  49. //Errors
  50. else {
  51. out.println("fail: already logged in");
  52. out.flush();
  53. }
  54. }
  55. else {
  56. out.println("fail: incorrect password");
  57. out.flush();
  58. }
  59. }
  60. else {
  61. out.println("fail: no such user");
  62. out.flush();
  63. }
  64. }
  65. if (s.equals("logout")){
  66. log = in.next();
  67. if (!DB.containsKey(log)) { //unreg user can't logout... unfortunately.
  68. out.println("fail: no such user");
  69. out.flush();
  70. }
  71. else {
  72. if (!DBin.contains(log)){ //so offline user does
  73. out.println("fail: already logged out");
  74. out.flush();
  75. }
  76. else {
  77. DBin.remove(log);
  78. out.println("success: user logged out");
  79. out.flush();
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }
Success #stdin #stdout 0.14s 321344KB
stdin
6
register vasya 12345
login vasya 1234
login vasya 12345
login anakin C-3PO
logout vasya
logout vasya
stdout
success: new user added
fail: incorrect password
success: user logged in
fail: no such user
success: user logged out
fail: already logged out