import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.TreeMap;
import java.lang.NumberFormatException;
public class Main
{
//структура, описывающая атрибуты учетной записи
public class AccountInfo
{
public boolean loggedIn;
public AccountInfo
(String argPassword,
boolean argLoggedIn
) {
password = argPassword;
loggedIn = argLoggedIn;
}
}
//база данных учетной записи
public class AccountDatabase
{
public static final int OK = 0;
public static final int NOT_REGISTERED = 1;
public static final int WRONG_PASSWORD = 2;
public static final int ALREADY_LOGGED_IN = 3;
public static final int ALREADY_LOGGED_OUT = 4;
private TreeMap
<String, AccountInfo
> map
;
//конструктор
public AccountDatabase()
{
map
= new TreeMap
<String, AccountInfo
>(); }
//регистрация новой учетной записи
{
AccountInfo info = map.get(name);
if (info != null)
{
return false;
}
map.put(name, new AccountInfo(password, false));
return true;
}
//вход в систему
{
int result = NOT_REGISTERED;
AccountInfo info = map.get(name);
if (info != null)
{
if (!info.password.equals(password))
{
result = WRONG_PASSWORD;
}
else if (info.loggedIn)
{
result = ALREADY_LOGGED_IN;
}
else
{
map.put(name, new AccountInfo(info.password, true));
result = OK;
}
}
return result;
}
//выход из системы
public int logout
(String name
) {
int result = NOT_REGISTERED;
AccountInfo info = map.get(name);
if ( info != null )
{
if ( info.loggedIn )
{
map.put(name, new AccountInfo(info.password, false));
result = OK;
}
else
{
result = ALREADY_LOGGED_OUT;
}
}
return result;
}
}
public static void main
(String[] args
) {
int loginCount = 0;
Scanner in
= new Scanner
(System.
in); //System.out.println("please enter number of logins: ");
line = ( in.hasNextLine( ) ) ? in.nextLine() : null;
try
{
loginCount
= Integer.
parseInt(line.
trim()); }
{
}
if ( loginCount < 1 || loginCount > 100 )
{
System.
out.
println("**** ERROR: wrong number of logins ****"); in.close();
return;
}
Main mainObj = new Main();
AccountDatabase accounts = mainObj.new AccountDatabase();
for( int i = 0; i < loginCount; i++ )
{
//System.out.println("please enter command:");
line = ( in.hasNextLine( ) ) ? in.nextLine() : null;
if ( line == null || line.isEmpty() )
{
System.
out.
println("**** ERROR: empty input, abort processing ****"); break;
}
//превращение введенной строки текста в вектор лексем
Vector<String> tokens = new Vector<String>();
while (st.hasMoreTokens())
{
tokens.add( st.nextToken() );
}
//превращение вектора лексем в команду с аргументами
String command
= ( tokens.
size() > 0 ) ? tokens.
get(0) : null; String name
= ( tokens.
size() > 1 ) ? tokens.
get(1) : null; String password
= ( tokens.
size() > 2 ) ? tokens.
get(2) : null;
//выполнение команд
if ( command.equals("register"))
{
if ( name != null || password != null )
{
if ( accounts.register(name, password) )
{
message = "success: new user added";
}
else
{
message = "fail: user already exists";
}
}
else
{
message = "fail: missing user name or password";
}
}
else if ( command.equals("login"))
{
if ( name != null || password != null )
{
switch( accounts.login(name, password) )
{
case AccountDatabase.NOT_REGISTERED:
message = "fail: no such user";
break;
case AccountDatabase.WRONG_PASSWORD:
message = "fail: incorrect password";
break;
case AccountDatabase.ALREADY_LOGGED_IN:
message = "fail: already logged in";
break;
default:
message = "success: user logged in";
break;
}
}
else
{
message = "fail: missing user name or password";
}
}
else if ( command.equals("logout"))
{
if ( name != null )
{
switch( accounts.logout(name) )
{
case AccountDatabase.ALREADY_LOGGED_OUT:
message = "fail: already logged out";
break;
case AccountDatabase.NOT_REGISTERED:
message = "fail: no such user";
break;
default:
message = "success: user logged out";
break;
}
}
else
{
message = "fail: missing user name";
}
}
if ( message != null )
{
}
}
in.close();
}
}