fork(2) download
  1. import java.util.Scanner;
  2. import java.util.StringTokenizer;
  3. import java.util.Vector;
  4. import java.util.TreeMap;
  5. import java.lang.NumberFormatException;
  6.  
  7. public class Main
  8. {
  9. //структура, описывающая атрибуты учетной записи
  10. public class AccountInfo
  11. {
  12. public String password;
  13. public boolean loggedIn;
  14.  
  15. public AccountInfo(String argPassword, boolean argLoggedIn)
  16. {
  17. password = argPassword;
  18. loggedIn = argLoggedIn;
  19. }
  20. }
  21.  
  22.  
  23. //база данных учетной записи
  24. public class AccountDatabase
  25. {
  26. public static final int OK = 0;
  27. public static final int NOT_REGISTERED = 1;
  28. public static final int WRONG_PASSWORD = 2;
  29. public static final int ALREADY_LOGGED_IN = 3;
  30. public static final int ALREADY_LOGGED_OUT = 4;
  31.  
  32. private TreeMap<String, AccountInfo> map;
  33.  
  34. //конструктор
  35. public AccountDatabase()
  36. {
  37. map = new TreeMap<String, AccountInfo>();
  38. }
  39.  
  40. //регистрация новой учетной записи
  41. public boolean register(String name, String password)
  42. {
  43. AccountInfo info = map.get(name);
  44.  
  45. if (info != null)
  46. {
  47. return false;
  48. }
  49.  
  50. map.put(name, new AccountInfo(password, false));
  51.  
  52. return true;
  53. }
  54.  
  55. //вход в систему
  56. public int login(String name, String password)
  57. {
  58. int result = NOT_REGISTERED;
  59.  
  60. AccountInfo info = map.get(name);
  61.  
  62. if (info != null)
  63. {
  64. if (!info.password.equals(password))
  65. {
  66. result = WRONG_PASSWORD;
  67. }
  68. else if (info.loggedIn)
  69. {
  70. result = ALREADY_LOGGED_IN;
  71. }
  72. else
  73. {
  74. map.put(name, new AccountInfo(info.password, true));
  75. result = OK;
  76. }
  77. }
  78.  
  79. return result;
  80. }
  81.  
  82. //выход из системы
  83. public int logout(String name)
  84. {
  85. int result = NOT_REGISTERED;
  86.  
  87. AccountInfo info = map.get(name);
  88.  
  89. if ( info != null )
  90. {
  91. if ( info.loggedIn )
  92. {
  93. map.put(name, new AccountInfo(info.password, false));
  94. result = OK;
  95. }
  96. else
  97. {
  98. result = ALREADY_LOGGED_OUT;
  99. }
  100. }
  101.  
  102. return result;
  103. }
  104.  
  105. }
  106.  
  107. public static void main(String[] args)
  108. {
  109. int loginCount = 0;
  110. String line = null;
  111.  
  112. Scanner in = new Scanner(System.in);
  113. //System.out.println("please enter number of logins: ");
  114. line = ( in.hasNextLine( ) ) ? in.nextLine() : null;
  115. try
  116. {
  117. loginCount = Integer.parseInt(line.trim());
  118. }
  119. {
  120. }
  121.  
  122. if ( loginCount < 1 || loginCount > 100 )
  123. {
  124. System.out.println("**** ERROR: wrong number of logins ****");
  125. in.close();
  126. return;
  127. }
  128.  
  129. Main mainObj = new Main();
  130. AccountDatabase accounts = mainObj.new AccountDatabase();
  131.  
  132. for( int i = 0; i < loginCount; i++ )
  133. {
  134. //System.out.println("please enter command:");
  135.  
  136. line = ( in.hasNextLine( ) ) ? in.nextLine() : null;
  137. if ( line == null || line.isEmpty() )
  138. {
  139. System.out.println("**** ERROR: empty input, abort processing ****");
  140. break;
  141. }
  142.  
  143. //превращение введенной строки текста в вектор лексем
  144. StringTokenizer st = new StringTokenizer(line, " ");
  145. Vector<String> tokens = new Vector<String>();
  146. while (st.hasMoreTokens())
  147. {
  148. tokens.add( st.nextToken() );
  149. }
  150.  
  151. //превращение вектора лексем в команду с аргументами
  152. String command = ( tokens.size() > 0 ) ? tokens.get(0) : null;
  153. String name = ( tokens.size() > 1 ) ? tokens.get(1) : null;
  154. String password = ( tokens.size() > 2 ) ? tokens.get(2) : null;
  155. String message = null;
  156.  
  157. //выполнение команд
  158. if ( command.equals("register"))
  159. {
  160. if ( name != null || password != null )
  161. {
  162. if ( accounts.register(name, password) )
  163. {
  164. message = "success: new user added";
  165. }
  166. else
  167. {
  168. message = "fail: user already exists";
  169. }
  170. }
  171. else
  172. {
  173. message = "fail: missing user name or password";
  174. }
  175. }
  176. else if ( command.equals("login"))
  177. {
  178. if ( name != null || password != null )
  179. {
  180. switch( accounts.login(name, password) )
  181. {
  182. case AccountDatabase.NOT_REGISTERED:
  183. message = "fail: no such user";
  184. break;
  185. case AccountDatabase.WRONG_PASSWORD:
  186. message = "fail: incorrect password";
  187. break;
  188. case AccountDatabase.ALREADY_LOGGED_IN:
  189. message = "fail: already logged in";
  190. break;
  191. default:
  192. message = "success: user logged in";
  193. break;
  194. }
  195. }
  196. else
  197. {
  198. message = "fail: missing user name or password";
  199. }
  200. }
  201. else if ( command.equals("logout"))
  202. {
  203. if ( name != null )
  204. {
  205. switch( accounts.logout(name) )
  206. {
  207. case AccountDatabase.ALREADY_LOGGED_OUT:
  208. message = "fail: already logged out";
  209. break;
  210. case AccountDatabase.NOT_REGISTERED:
  211. message = "fail: no such user";
  212. break;
  213. default:
  214. message = "success: user logged out";
  215. break;
  216. }
  217. }
  218. else
  219. {
  220. message = "fail: missing user name";
  221. }
  222. }
  223.  
  224. if ( message != null )
  225. {
  226. System.out.println(message);
  227. }
  228. }
  229. in.close();
  230. }
  231. }
  232.  
Runtime error #stdin #stdout #stderr 0.14s 321280KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Exception in thread "main" java.lang.NullPointerException
	at Main.main(Main.java:117)