Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.reflect.Method;
- import java.util.Collections;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.Scanner;
- import java.util.Set;
- import java.util.TreeSet;
- import javax.print.DocFlavor.STRING;
- class NoSuchRoomExcpetion extends Exception {
- public NoSuchRoomExcpetion(String msg) {
- super(msg);
- }
- }
- class NoSuchUserExcpetion extends Exception {
- public NoSuchUserExcpetion(String msg) {
- super(msg);
- }
- }
- class ChatRoom implements Comparable {
- String roomName;
- Set<String> users;
- int brojac;
- public ChatRoom(String name) {
- roomName = name;
- users = new HashSet<String>();
- brojac = 0;
- }
- public void addUser(String username) {
- users.add(username);
- brojac++;
- }
- public void removeUser(String username) {
- if (users.contains(username)) {
- users.remove(username);
- brojac--;
- }
- }
- public boolean hasUser(String username) {
- if (users.contains(username)) {
- return true;
- }
- return false;
- }
- public String toString() {
- LinkedList<String> niza = new LinkedList<String>(users);
- Collections.sort(niza);
- String finalno = this.roomName;
- Iterator<String> it = niza.iterator();
- while (it.hasNext()) {
- finalno += "\n" + it.next();
- }
- if (brojac==0)
- finalno+="\nEMPTY";
- return finalno;
- }
- public int numUsers() {
- return brojac;
- }
- public boolean equals(Object o) {
- ChatRoom tmp = (ChatRoom) o;
- if (this.roomName == tmp.roomName) {
- return true;
- }
- return false;
- }
- @Override
- public int compareTo(Object o) {
- ChatRoom tmp = (ChatRoom) o;
- return (this.roomName.compareTo(tmp.roomName));
- }
- public ChatRoom union(ChatRoom other) {
- ChatRoom tmp = new ChatRoom("Union");
- Iterator<String> user = users.iterator();
- while (user.hasNext()) {
- tmp.addUser(user.next());
- }
- user = other.users.iterator();
- while (user.hasNext()) {
- String username = user.next();
- if (!tmp.hasUser(username)) {
- tmp.addUser(user.next());
- }
- }
- return tmp;
- }
- public ChatRoom intersect(ChatRoom other) {
- ChatRoom tmp = new ChatRoom("Intersect");
- Iterator<String> user = users.iterator();
- while (user.hasNext()) {
- String username = user.next();
- if(other.hasUser(username))
- tmp.addUser(username);
- }
- return tmp;
- }
- }
- class ChatSystem {
- TreeSet<ChatRoom> rooms;
- TreeSet<String> users;
- public ChatSystem() {
- rooms = new TreeSet<ChatRoom>();
- }
- public void addRoom(String roomName) {
- rooms.add(new ChatRoom(roomName));
- }
- public void removeRoom(String roomName) {
- ChatRoom tmp = new ChatRoom(roomName);
- if (rooms.contains(tmp)) {
- rooms.remove(tmp);
- }
- }
- public ChatRoom getRoom(String roomName) throws NoSuchRoomExcpetion {
- ChatRoom tmp = new ChatRoom(roomName);
- if (rooms.contains(tmp)) {
- Iterator<ChatRoom> it = rooms.iterator();
- while (it.hasNext()) {
- ChatRoom tmp2 = it.next();
- if (tmp2.equals(tmp))
- return tmp2;
- }
- } else {
- throw new NoSuchRoomExcpetion(roomName);
- }
- return null;
- }
- public void register(String username) {
- Iterator<ChatRoom> it = rooms.iterator();
- ChatRoom sea = it.next();
- int min = sea.numUsers();
- while (it.hasNext()) {
- ChatRoom tmp2 = it.next();
- if (tmp2.numUsers() < min) {
- min = tmp2.brojac;
- sea = tmp2;
- }
- }
- sea.addUser(username);
- users.add(username);
- }
- public void registerAndJoin(String username, String roomName) {
- ChatRoom tmp = new ChatRoom(roomName);
- if (rooms.contains(tmp)) {
- Iterator<ChatRoom> it = rooms.iterator();
- while (it.hasNext()) {
- ChatRoom tmp2 = it.next();
- if (tmp2.equals(tmp)) {
- users.add(username);
- tmp2.addUser(username);
- }
- }
- }
- }
- public void joinRoom(String username, String roomName)
- throws NoSuchUserExcpetion, NoSuchRoomExcpetion {
- if (!users.contains(username))
- throw new NoSuchUserExcpetion(username);
- ChatRoom tmp = new ChatRoom(roomName);
- if (rooms.contains(tmp)) {
- Iterator<ChatRoom> it = rooms.iterator();
- while (it.hasNext()) {
- ChatRoom tmp2 = it.next();
- if (tmp2.equals(tmp)) {
- tmp2.addUser(username);
- }
- }
- } else {
- throw new NoSuchRoomExcpetion(roomName);
- }
- }
- public void leaveRoom(String username, String roomName)
- throws NoSuchUserExcpetion, NoSuchRoomExcpetion {
- if (!users.contains(username))
- throw new NoSuchUserExcpetion(username);
- ChatRoom tmp = new ChatRoom(roomName);
- if (rooms.contains(tmp)) {
- Iterator<ChatRoom> it = rooms.iterator();
- while (it.hasNext()) {
- ChatRoom tmp2 = it.next();
- if (tmp2.equals(tmp)) {
- tmp2.removeUser(username);
- }
- }
- } else {
- throw new NoSuchRoomExcpetion(roomName);
- }
- }
- public void follorFriend(String username, String friend)
- throws NoSuchUserExcpetion {
- if (!users.contains(username))
- throw new NoSuchUserExcpetion(username);
- if (!users.contains(friend))
- throw new NoSuchUserExcpetion(friend);
- Iterator<ChatRoom> it = rooms.iterator();
- while (it.hasNext()) {
- ChatRoom tmp = it.next();
- if (tmp.hasUser(friend)) {
- tmp.addUser(username);
- }
- }
- }
- public void print() {
- System.out.println(rooms);
- }
- }
- public class ChatSystemTest {
- public static void main(String[] args) throws Exception {
- 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();
- System.out.println(cr2);
- System.out.println();
- System.out.println();
- System.out.println(cr.intersect(cr2));
- System.out.println();
- System.out.println();
- System.out.println(cr.union(cr2));
- System.out.println();
- }
- 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