Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.util.Scanner;
- import java.util.TreeSet;
- import java.util.ArrayList;
- import java.util.Collection;
- import java.util.Collections;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.ListIterator;
- import java.util.Map;
- import java.util.TreeMap;
- import java.util.TreeSet;
- class NoSuchRoomException extends Exception
- {
- public NoSuchRoomException(String roomName){
- super(roomName);
- }
- }
- class NoSuchUserException extends Exception
- {
- public NoSuchUserException(String userName)
- {
- super(userName);
- }
- }
- class ChatRoom
- {
- String name;
- TreeSet<String> al;
- public ChatRoom(String name)
- {
- this.name = name;
- this.al = new TreeSet<>();
- }
- public void addUser(String username)
- {
- al.add(username);
- }
- public void removeUser(String username)
- {
- while(al.contains(username))
- {
- al.remove(username);
- }
- }
- public String toString()
- {
- StringBuilder sb = new StringBuilder();
- sb.append(name + "\n");
- if(al.isEmpty())
- sb.append("EMPTY\n");
- al.forEach(t -> sb.append(t + "\n"));
- return sb.toString();
- }
- public boolean hasUser(String username)
- {
- return al.contains(username);
- }
- public int numUsers()
- {
- return al.size();
- }
- }
- class ChatSystem
- {
- TreeMap<String, ChatRoom> tm;
- HashSet<String> users;
- public ChatSystem()
- {
- this.tm = new TreeMap<>();
- users = new HashSet<>();
- }
- public void addRoom(String roomName)
- {
- tm.put(roomName, new ChatRoom(roomName));
- }
- public void removeRoom(String roomName)
- {
- tm.remove(roomName);
- }
- public ChatRoom getRoom(String roomname) throws NoSuchRoomException
- {
- if(!tm.containsKey(roomname))
- throw new NoSuchRoomException(roomname);
- return tm.get(roomname);
- }
- public void register(String userName) throws NoSuchRoomException, NoSuchUserException
- {
- users.add(userName);
- ChatRoom room = tm.values().stream().min((left,right) -> {
- if(left.numUsers()==right.numUsers())
- return left.name.compareTo(right.name);
- else
- return left.numUsers()- right.numUsers();
- }).orElse(null);
- if(room == null)
- return;
- joinRoom(userName, room.name);
- }
- public void registerAndJoin(String userName, String roomName) throws NoSuchRoomException, NoSuchUserException
- {
- users.add(userName);
- joinRoom(userName, roomName);
- }
- public void joinRoom(String userName, String roomName) throws NoSuchRoomException, NoSuchUserException
- {
- check(roomName, userName);
- tm.computeIfPresent(roomName, (rname, room) -> {
- room.addUser(userName);
- return room;
- });
- }
- public void leaveRoom(String userName, String roomName) throws NoSuchRoomException, NoSuchUserException
- {
- check(roomName, userName);
- tm.computeIfPresent(roomName, (rname, room) ->
- {
- room.removeUser(userName);
- return room;
- });
- }
- public void check(String roomName, String userName) throws NoSuchRoomException, NoSuchUserException
- {
- if(!tm.containsKey(roomName))
- throw new NoSuchRoomException(roomName);
- if(!users.contains(userName))
- throw new NoSuchUserException(userName);
- }
- public void followFriend(String userName, String friend_username) throws NoSuchUserException
- {
- if(!users.contains(userName))
- throw new NoSuchUserException(userName);
- tm.values()
- .forEach(t -> {
- if(t.hasUser(friend_username))
- t.addUser(userName);
- });
- }
- }
- public class ChatSystemTest {
- public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchRoomException {
- Scanner jin = new Scanner(System.in);
- int k = jin.nextInt();
- if ( k == 0 ) {
- ChatRoom cr = new ChatRoom(jin.next());
- int n = jin.nextInt();
- for ( int i = 0 ; i < n ; ++i ) {
- k = jin.nextInt();
- if ( k == 0 ) cr.addUser(jin.next());
- if ( k == 1 ) cr.removeUser(jin.next());
- if ( k == 2 ) System.out.println(cr.hasUser(jin.next()));
- }
- System.out.println("");
- System.out.println(cr.toString());
- n = jin.nextInt();
- if ( n == 0 ) return;
- ChatRoom cr2 = new ChatRoom(jin.next());
- for ( int i = 0 ; i < n ; ++i ) {
- k = jin.nextInt();
- if ( k == 0 ) cr2.addUser(jin.next());
- if ( k == 1 ) cr2.removeUser(jin.next());
- if ( k == 2 ) cr2.hasUser(jin.next());
- }
- System.out.println(cr2.toString());
- }
- if ( k == 1 ) {
- ChatSystem cs = new ChatSystem();
- Method mts[] = cs.getClass().getMethods();
- while ( true ) {
- String cmd = jin.next();
- if ( cmd.equals("stop") ) break;
- if ( cmd.equals("print") ) {
- System.out.println(cs.getRoom(jin.next())+"\n");continue;
- }
- for ( Method m : mts ) {
- if ( m.getName().equals(cmd) ) {
- String params[] = new String[m.getParameterTypes().length];
- for ( int i = 0 ; i < params.length ; ++i ) params[i] = jin.next();
- m.invoke(cs,params);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement