Advertisement
spiny94

Untitled

Sep 3rd, 2015
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.27 KB | None | 0 0
  1. package facebook;
  2.  
  3. import java.util.Collection;
  4. import java.util.HashMap;
  5. import java.util.HashSet;
  6. import java.util.ArrayList;
  7. import java.util.Iterator;
  8.  
  9.  
  10. public class Facebook {
  11.     HashMap<String, Person> people = new HashMap<String, Person>();
  12.     HashMap<String, Group> groups = new HashMap<String, Group>();
  13.  
  14.     public void addPerson(String code, String name, String surname)
  15.             throws PersonExistsException {
  16.         Person p = new Person(code, name, surname);
  17.         if (people.containsKey(code))
  18.             throw new PersonExistsException();
  19.         people.put(code, p);
  20.     }
  21.  
  22.     public String getPerson(String code) throws NoSuchCodeException {
  23.         Person p = people.get(code);
  24.         if (p == null)
  25.             throw new NoSuchCodeException();
  26.         return p.toString();
  27.     }
  28.  
  29.     public void addFriendship(String codePerson1, String codePerson2)
  30.             throws NoSuchCodeException {
  31.         Person p1 = people.get(codePerson1);
  32.         Person p2 = people.get(codePerson2);
  33.         if (p1 == null || p2 == null)
  34.             throw new NoSuchCodeException();
  35.         p1.addFriend(codePerson2);
  36.         p2.addFriend(codePerson1);
  37.     }
  38.  
  39.     public Collection<String> listOfFriends(String codePerson)
  40.             throws NoSuchCodeException {
  41.         if (people.containsKey(codePerson)) {
  42.             if (people.get(codePerson).getFriends().size() == 0)
  43.                 return null;
  44.             else
  45.                 return people.get(codePerson).getFriends();
  46.         } else
  47.             throw new NoSuchCodeException();   
  48.     }
  49.  
  50.     public Collection<String> friendsOfFriends(String codePerson)
  51.             throws NoSuchCodeException {
  52.         if (people.size() == 0)
  53.             return null;
  54.         Person p1 = people.get(codePerson);
  55.         if (p1 == null)
  56.             throw new NoSuchCodeException();
  57.         Collection<String> friends = p1.getFriends();
  58.         Iterator<String> it = friends.iterator();
  59.         ArrayList<String> friends2 = new ArrayList<String>();
  60.         while (it.hasNext())
  61.             friends2.addAll(listOfFriends(it.next()));
  62.         if (friends2.size() == 0)
  63.             return null;
  64.         while(friends2.contains(codePerson))
  65.             friends2.remove(codePerson);
  66.         return friends2;
  67.     }
  68.  
  69.     public Collection<String> friendsOfFriendsNoRepetition(String codePerson)
  70.             throws NoSuchCodeException {
  71.         return new HashSet<String>(friendsOfFriends(codePerson));
  72.     }
  73.  
  74.     public void addGroup(String groupName) {
  75.         groups.put(groupName, new Group(groupName));
  76.     }
  77.  
  78.     public Collection<String> listOfGroups() {
  79.         if (groups.size() == 0)
  80.             return null;
  81.         return groups.keySet();
  82.     }
  83.  
  84.     public void addPersonToGroup(String codePerson, String groupName) {
  85.         Group g = groups.get(groupName);
  86.         Person p = people.get(codePerson);
  87.         if (p == null || g == null)
  88.             return;
  89.         g.addPerson(codePerson);
  90.         p.addGroup(groupName);
  91.     }
  92.  
  93.     public Collection<String> listOfPeopleInGroup(String groupName) {
  94.         Group g = groups.get(groupName);
  95.         if (g.getMembers().size() == 0)
  96.             return null;
  97.         return g.getMembers();
  98.     }
  99.  
  100.     public String personWithLargestNumberOfFriends() {
  101.         Collection<Person> pp = people.values();
  102.         Iterator<Person> it = pp.iterator();
  103.         int max = 0;
  104.         String pmax = null;
  105.         while (it.hasNext()) {
  106.             Person p = it.next();
  107.             int s = p.getFriends().size();
  108.             if (s > max) {
  109.                 max = s;
  110.                 pmax = p.getCode();
  111.             }
  112.         }
  113.         return pmax;
  114.     }
  115.  
  116.     public String personWithMostFriendsOfFriends() {
  117.         Collection<Person> pp = people.values();
  118.         Iterator<Person> it = pp.iterator();
  119.         int max = 0;
  120.         String pmax = null;
  121.         while (it.hasNext()) {
  122.             Person p = it.next();
  123.             int s=0;
  124.             try {
  125.                 if (friendsOfFriends(p.getCode())!=null)
  126.                     s = friendsOfFriendsNoRepetition(p.getCode()).size();
  127.             } catch (NoSuchCodeException e) {
  128.                 return null;
  129.             }
  130.             if (s > max) {
  131.                 max = s;
  132.                 pmax = p.getCode();
  133.             }
  134.         }
  135.         return pmax;
  136.     }
  137.  
  138.     public String largestGroup() {
  139.         Collection<Group> pp = groups.values();
  140.         Iterator<Group> it = pp.iterator();
  141.         int max = 0;
  142.         String gmax = null;
  143.         while (it.hasNext()) {
  144.             Group p = it.next();
  145.             int s = p.getMembers().size();
  146.             if (s > max) {
  147.                 max = s;
  148.                 gmax = p.getName();
  149.             }
  150.         }
  151.         return gmax;
  152.     }
  153.  
  154.     public String personInLargestNumberOfGroups() {
  155.         Collection<Person> pp = people.values();
  156.         Iterator<Person> it = pp.iterator();
  157.         int max = 0;
  158.         String pmax = null;
  159.         while (it.hasNext()) {
  160.             Person p = it.next();
  161.             int s = p.getGroups().size();
  162.             if (s > max) {
  163.                 max = s;
  164.                 pmax = p.getCode();
  165.             }
  166.         }
  167.         return pmax;
  168.     }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement