Advertisement
jovanovski

НП Лаб6 Зад2

Nov 25th, 2012
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.88 KB | None | 0 0
  1. import java.lang.reflect.Method;
  2. import java.util.Collections;
  3. import java.util.HashSet;
  4. import java.util.Iterator;
  5. import java.util.LinkedList;
  6. import java.util.Scanner;
  7. import java.util.Set;
  8. import java.util.TreeSet;
  9.  
  10. import javax.print.DocFlavor.STRING;
  11.  
  12. class NoSuchRoomExcpetion extends Exception {
  13.     public NoSuchRoomExcpetion(String msg) {
  14.         super(msg);
  15.     }
  16. }
  17.  
  18. class NoSuchUserExcpetion extends Exception {
  19.     public NoSuchUserExcpetion(String msg) {
  20.         super(msg);
  21.     }
  22. }
  23.  
  24. class ChatRoom implements Comparable {
  25.  
  26.     String roomName;
  27.     Set<String> users;
  28.     int brojac;
  29.  
  30.     public ChatRoom(String name) {
  31.         roomName = name;
  32.         users = new HashSet<String>();
  33.         brojac = 0;
  34.     }
  35.  
  36.     public void addUser(String username) {
  37.         users.add(username);
  38.         brojac++;
  39.     }
  40.  
  41.     public void removeUser(String username) {
  42.         if (users.contains(username)) {
  43.             users.remove(username);
  44.             brojac--;
  45.         }
  46.     }
  47.  
  48.     public boolean hasUser(String username) {
  49.         if (users.contains(username)) {
  50.             return true;
  51.         }
  52.         return false;
  53.     }
  54.  
  55.     public String toString() {
  56.         LinkedList<String> niza = new LinkedList<String>(users);
  57.         Collections.sort(niza);
  58.         String finalno = this.roomName;
  59.         Iterator<String> it = niza.iterator();
  60.         while (it.hasNext()) {
  61.             finalno += "\n" + it.next();
  62.         }
  63.         if (brojac==0)
  64.             finalno+="\nEMPTY";
  65.         return finalno;
  66.     }
  67.  
  68.     public int numUsers() {
  69.         return brojac;
  70.     }
  71.  
  72.     public boolean equals(Object o) {
  73.         ChatRoom tmp = (ChatRoom) o;
  74.         if (this.roomName == tmp.roomName) {
  75.             return true;
  76.         }
  77.         return false;
  78.     }
  79.  
  80.     @Override
  81.     public int compareTo(Object o) {
  82.         ChatRoom tmp = (ChatRoom) o;
  83.         return (this.roomName.compareTo(tmp.roomName));
  84.     }
  85.  
  86.     public ChatRoom union(ChatRoom other) {
  87.         ChatRoom tmp = new ChatRoom("Union");
  88.         Iterator<String> user = users.iterator();
  89.         while (user.hasNext()) {
  90.             tmp.addUser(user.next());
  91.         }
  92.         user = other.users.iterator();
  93.         while (user.hasNext()) {
  94.             String username = user.next();
  95.             if (!tmp.hasUser(username)) {
  96.                 tmp.addUser(user.next());
  97.             }
  98.         }
  99.         return tmp;
  100.     }
  101.  
  102.     public ChatRoom intersect(ChatRoom other) {
  103.         ChatRoom tmp = new ChatRoom("Intersect");
  104.         Iterator<String> user = users.iterator();
  105.         while (user.hasNext()) {
  106.             String username = user.next();
  107.             if(other.hasUser(username))
  108.             tmp.addUser(username);
  109.         }
  110.         return tmp;
  111.     }
  112. }
  113.  
  114. class ChatSystem {
  115.     TreeSet<ChatRoom> rooms;
  116.     TreeSet<String> users;
  117.  
  118.     public ChatSystem() {
  119.         rooms = new TreeSet<ChatRoom>();
  120.     }
  121.  
  122.     public void addRoom(String roomName) {
  123.         rooms.add(new ChatRoom(roomName));
  124.     }
  125.  
  126.     public void removeRoom(String roomName) {
  127.         ChatRoom tmp = new ChatRoom(roomName);
  128.         if (rooms.contains(tmp)) {
  129.             rooms.remove(tmp);
  130.         }
  131.     }
  132.  
  133.     public ChatRoom getRoom(String roomName) throws NoSuchRoomExcpetion {
  134.         ChatRoom tmp = new ChatRoom(roomName);
  135.         if (rooms.contains(tmp)) {
  136.             Iterator<ChatRoom> it = rooms.iterator();
  137.             while (it.hasNext()) {
  138.                 ChatRoom tmp2 = it.next();
  139.                 if (tmp2.equals(tmp))
  140.                     return tmp2;
  141.             }
  142.         } else {
  143.             throw new NoSuchRoomExcpetion(roomName);
  144.         }
  145.         return null;
  146.     }
  147.  
  148.     public void register(String username) {
  149.         Iterator<ChatRoom> it = rooms.iterator();
  150.         ChatRoom sea = it.next();
  151.         int min = sea.numUsers();
  152.  
  153.         while (it.hasNext()) {
  154.             ChatRoom tmp2 = it.next();
  155.             if (tmp2.numUsers() < min) {
  156.                 min = tmp2.brojac;
  157.                 sea = tmp2;
  158.             }
  159.         }
  160.  
  161.         sea.addUser(username);
  162.         users.add(username);
  163.     }
  164.  
  165.     public void registerAndJoin(String username, String roomName) {
  166.         ChatRoom tmp = new ChatRoom(roomName);
  167.         if (rooms.contains(tmp)) {
  168.             Iterator<ChatRoom> it = rooms.iterator();
  169.             while (it.hasNext()) {
  170.                 ChatRoom tmp2 = it.next();
  171.                 if (tmp2.equals(tmp)) {
  172.                     users.add(username);
  173.                     tmp2.addUser(username);
  174.                 }
  175.             }
  176.         }
  177.     }
  178.  
  179.     public void joinRoom(String username, String roomName)
  180.             throws NoSuchUserExcpetion, NoSuchRoomExcpetion {
  181.         if (!users.contains(username))
  182.             throw new NoSuchUserExcpetion(username);
  183.         ChatRoom tmp = new ChatRoom(roomName);
  184.         if (rooms.contains(tmp)) {
  185.             Iterator<ChatRoom> it = rooms.iterator();
  186.             while (it.hasNext()) {
  187.                 ChatRoom tmp2 = it.next();
  188.                 if (tmp2.equals(tmp)) {
  189.                     tmp2.addUser(username);
  190.                 }
  191.             }
  192.         } else {
  193.             throw new NoSuchRoomExcpetion(roomName);
  194.         }
  195.     }
  196.  
  197.     public void leaveRoom(String username, String roomName)
  198.             throws NoSuchUserExcpetion, NoSuchRoomExcpetion {
  199.         if (!users.contains(username))
  200.             throw new NoSuchUserExcpetion(username);
  201.         ChatRoom tmp = new ChatRoom(roomName);
  202.         if (rooms.contains(tmp)) {
  203.             Iterator<ChatRoom> it = rooms.iterator();
  204.             while (it.hasNext()) {
  205.                 ChatRoom tmp2 = it.next();
  206.                 if (tmp2.equals(tmp)) {
  207.                     tmp2.removeUser(username);
  208.                 }
  209.             }
  210.         } else {
  211.             throw new NoSuchRoomExcpetion(roomName);
  212.         }
  213.     }
  214.  
  215.     public void follorFriend(String username, String friend)
  216.             throws NoSuchUserExcpetion {
  217.         if (!users.contains(username))
  218.             throw new NoSuchUserExcpetion(username);
  219.         if (!users.contains(friend))
  220.             throw new NoSuchUserExcpetion(friend);
  221.         Iterator<ChatRoom> it = rooms.iterator();
  222.         while (it.hasNext()) {
  223.             ChatRoom tmp = it.next();
  224.             if (tmp.hasUser(friend)) {
  225.                 tmp.addUser(username);
  226.             }
  227.         }
  228.     }
  229.  
  230.     public void print() {
  231.         System.out.println(rooms);
  232.     }
  233.  
  234. }
  235.  
  236. public class ChatSystemTest {
  237.     public static void main(String[] args) throws Exception {
  238.         Scanner jin = new Scanner(System.in);
  239.         int k = jin.nextInt();
  240.         if (k == 0) {
  241.             ChatRoom cr = new ChatRoom(jin.next());
  242.             int n = jin.nextInt();
  243.             for (int i = 0; i < n; ++i) {
  244.                 k = jin.nextInt();
  245.                 if (k == 0)
  246.                     cr.addUser(jin.next());
  247.                 if (k == 1)
  248.                     cr.removeUser(jin.next());
  249.                 if (k == 2)
  250.                     System.out.println(cr.hasUser(jin.next()));
  251.             }
  252.             System.out.println("");
  253.             System.out.println(cr.toString());
  254.             n = jin.nextInt();
  255.             if (n == 0)
  256.                 return;
  257.             ChatRoom cr2 = new ChatRoom(jin.next());
  258.             for (int i = 0; i < n; ++i) {
  259.                 k = jin.nextInt();
  260.                 if (k == 0)
  261.                     cr2.addUser(jin.next());
  262.                 if (k == 1)
  263.                     cr2.removeUser(jin.next());
  264.                 if (k == 2)
  265.                     cr2.hasUser(jin.next());
  266.             }
  267.             System.out.println();
  268.             System.out.println(cr2);
  269.             System.out.println();
  270.             System.out.println();
  271.             System.out.println(cr.intersect(cr2));
  272.             System.out.println();
  273.             System.out.println();
  274.             System.out.println(cr.union(cr2));
  275.             System.out.println();
  276.         }
  277.         if (k == 1) {
  278.             ChatSystem cs = new ChatSystem();
  279.             Method mts[] = cs.getClass().getMethods();
  280.             while (true) {
  281.                 String cmd = jin.next();
  282.                 if (cmd.equals("stop"))
  283.                     break;
  284.                 if (cmd.equals("print")) {
  285.                     System.out.println(cs.getRoom(jin.next()) + "\n");
  286.                     continue;
  287.                 }
  288.                 for (Method m : mts) {
  289.                     if (m.getName().equals(cmd)) {
  290.                         String params[] = new String[m.getParameterTypes().length];
  291.                         for (int i = 0; i < params.length; ++i)
  292.                             params[i] = jin.next();
  293.                         m.invoke(cs, params);
  294.                     }
  295.                 }
  296.             }
  297.         }
  298.     }
  299. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement