Advertisement
lameski

Chat system

Aug 20th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. import java.lang.reflect.InvocationTargetException;
  2. import java.lang.reflect.Method;
  3. import java.util.Scanner;
  4. import java.util.TreeSet;
  5. import java.util.ArrayList;
  6. import java.util.Collection;
  7. import java.util.Collections;
  8. import java.util.HashSet;
  9. import java.util.Iterator;
  10. import java.util.LinkedList;
  11. import java.util.List;
  12. import java.util.ListIterator;
  13. import java.util.Map;
  14. import java.util.TreeMap;
  15. import java.util.TreeSet;
  16. class NoSuchRoomException extends Exception
  17. {
  18. public NoSuchRoomException(String roomName){
  19. super(roomName);
  20. }
  21. }
  22. class NoSuchUserException extends Exception
  23. {
  24. public NoSuchUserException(String userName)
  25. {
  26. super(userName);
  27. }
  28. }
  29.  
  30. class ChatRoom
  31. {
  32. String name;
  33. TreeSet<String> al;
  34. public ChatRoom(String name)
  35. {
  36. this.name = name;
  37. this.al = new TreeSet<>();
  38. }
  39. public void addUser(String username)
  40. {
  41. al.add(username);
  42. }
  43. public void removeUser(String username)
  44. {
  45. while(al.contains(username))
  46. {
  47. al.remove(username);
  48. }
  49. }
  50. public String toString()
  51. {
  52. StringBuilder sb = new StringBuilder();
  53. sb.append(name + "\n");
  54.  
  55. if(al.isEmpty())
  56. sb.append("EMPTY\n");
  57. al.forEach(t -> sb.append(t + "\n"));
  58. return sb.toString();
  59. }
  60. public boolean hasUser(String username)
  61. {
  62. return al.contains(username);
  63. }
  64.  
  65. public int numUsers()
  66. {
  67. return al.size();
  68. }
  69. }
  70.  
  71. class ChatSystem
  72. {
  73. TreeMap<String, ChatRoom> tm;
  74. HashSet<String> users;
  75. public ChatSystem()
  76. {
  77. this.tm = new TreeMap<>();
  78. users = new HashSet<>();
  79. }
  80. public void addRoom(String roomName)
  81. {
  82. tm.put(roomName, new ChatRoom(roomName));
  83. }
  84. public void removeRoom(String roomName)
  85. {
  86. tm.remove(roomName);
  87. }
  88.  
  89. public ChatRoom getRoom(String roomname) throws NoSuchRoomException
  90. {
  91. if(!tm.containsKey(roomname))
  92. throw new NoSuchRoomException(roomname);
  93.  
  94. return tm.get(roomname);
  95.  
  96. }
  97. public void register(String userName) throws NoSuchRoomException, NoSuchUserException
  98. {
  99. users.add(userName);
  100. ChatRoom room = tm.values().stream().min((left,right) -> {
  101. if(left.numUsers()==right.numUsers())
  102. return left.name.compareTo(right.name);
  103. else
  104. return left.numUsers()- right.numUsers();
  105. }).orElse(null);
  106. if(room == null)
  107. return;
  108. joinRoom(userName, room.name);
  109. }
  110. public void registerAndJoin(String userName, String roomName) throws NoSuchRoomException, NoSuchUserException
  111. {
  112. users.add(userName);
  113. joinRoom(userName, roomName);
  114. }
  115.  
  116. public void joinRoom(String userName, String roomName) throws NoSuchRoomException, NoSuchUserException
  117. {
  118. check(roomName, userName);
  119. tm.computeIfPresent(roomName, (rname, room) -> {
  120. room.addUser(userName);
  121. return room;
  122. });
  123. }
  124. public void leaveRoom(String userName, String roomName) throws NoSuchRoomException, NoSuchUserException
  125. {
  126. check(roomName, userName);
  127. tm.computeIfPresent(roomName, (rname, room) ->
  128. {
  129. room.removeUser(userName);
  130. return room;
  131. });
  132. }
  133. public void check(String roomName, String userName) throws NoSuchRoomException, NoSuchUserException
  134. {
  135. if(!tm.containsKey(roomName))
  136. throw new NoSuchRoomException(roomName);
  137. if(!users.contains(userName))
  138. throw new NoSuchUserException(userName);
  139.  
  140. }
  141.  
  142. public void followFriend(String userName, String friend_username) throws NoSuchUserException
  143. {
  144. if(!users.contains(userName))
  145. throw new NoSuchUserException(userName);
  146.  
  147. tm.values()
  148. .forEach(t -> {
  149. if(t.hasUser(friend_username))
  150. t.addUser(userName);
  151. });
  152.  
  153. }
  154.  
  155. }
  156.  
  157. public class ChatSystemTest {
  158.  
  159. public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchRoomException {
  160. Scanner jin = new Scanner(System.in);
  161. int k = jin.nextInt();
  162. if ( k == 0 ) {
  163. ChatRoom cr = new ChatRoom(jin.next());
  164. int n = jin.nextInt();
  165. for ( int i = 0 ; i < n ; ++i ) {
  166. k = jin.nextInt();
  167. if ( k == 0 ) cr.addUser(jin.next());
  168. if ( k == 1 ) cr.removeUser(jin.next());
  169. if ( k == 2 ) System.out.println(cr.hasUser(jin.next()));
  170. }
  171. System.out.println("");
  172. System.out.println(cr.toString());
  173. n = jin.nextInt();
  174. if ( n == 0 ) return;
  175. ChatRoom cr2 = new ChatRoom(jin.next());
  176. for ( int i = 0 ; i < n ; ++i ) {
  177. k = jin.nextInt();
  178. if ( k == 0 ) cr2.addUser(jin.next());
  179. if ( k == 1 ) cr2.removeUser(jin.next());
  180. if ( k == 2 ) cr2.hasUser(jin.next());
  181. }
  182. System.out.println(cr2.toString());
  183. }
  184. if ( k == 1 ) {
  185. ChatSystem cs = new ChatSystem();
  186. Method mts[] = cs.getClass().getMethods();
  187. while ( true ) {
  188. String cmd = jin.next();
  189. if ( cmd.equals("stop") ) break;
  190. if ( cmd.equals("print") ) {
  191. System.out.println(cs.getRoom(jin.next())+"\n");continue;
  192. }
  193. for ( Method m : mts ) {
  194. if ( m.getName().equals(cmd) ) {
  195. String params[] = new String[m.getParameterTypes().length];
  196. for ( int i = 0 ; i < params.length ; ++i ) params[i] = jin.next();
  197. m.invoke(cs,params);
  198. }
  199. }
  200. }
  201. }
  202. }
  203.  
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement