Advertisement
Guest User

StudentManager

a guest
Jul 28th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.38 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.List;
  4.  
  5. public class StudentManager {
  6.     private List<Student> students;
  7.     private List<Relationship> friends;
  8.  
  9.     public StudentManager() {
  10.         students = new ArrayList<Student>();
  11.         friends = new ArrayList<Relationship>();
  12.     }
  13.  
  14.     /**
  15.      * This method is used to add students in the list of students
  16.      *
  17.      * @param student
  18.      */
  19.     public void addStudent(Student student) {
  20.         students.add(student);
  21.     }
  22.  
  23.     /**
  24.      * This method is used to make two students friends.
  25.      *
  26.      * @return true if they became friends. False if they does not exist in list
  27.      *         of students
  28.      */
  29.     public boolean makeThisStudentsFriends(Student firstFriend, Student secondFriend) {
  30.         if (students.contains(firstFriend) && students.contains(secondFriend)) {
  31.             friends.add(new Relationship(firstFriend, secondFriend));
  32.             return true;
  33.         }
  34.         return false;
  35.     }
  36.  
  37.     /**
  38.      * This method returns all friends of a student(First exercise!)
  39.      *
  40.      * @param student
  41.      * @return List<Student>
  42.      */
  43.     public List<Student> getFriendsOfStudent(Student student) {
  44.         List<Student> friendsOfStudent = new ArrayList<Student>();
  45.         for (Iterator<Relationship> iterator = friends.iterator(); iterator.hasNext();) {
  46.             Relationship relationship = (Relationship) iterator.next();
  47.  
  48.             if (relationship.getFirstStudent().equals(student)) {
  49.                 friendsOfStudent.add(relationship.getSecondStudent());
  50.             } else if (relationship.getSecondStudent().equals(student)) {
  51.                 friendsOfStudent.add(relationship.getFirstStudent());
  52.             } else {
  53.                 continue;
  54.             }
  55.         }
  56.         return friendsOfStudent;
  57.     }
  58.  
  59.     /**
  60.      * This method returns all friends of friends of a student(Second exercise!)
  61.      *
  62.      * @param student
  63.      * @return List<Student>
  64.      */
  65.     public List<Student> getFriendsOfFriendsOfStudent(Student student) {
  66.         List<Student> friendsOfFriendsOfStudent = new ArrayList<Student>();
  67.         List<Student> friendsOfStudent = getFriendsOfStudent(student);
  68.  
  69.         for (Iterator<Student> iterator = friendsOfStudent.iterator(); iterator.hasNext();) {
  70.             Student friendOfStudent = (Student) iterator.next();
  71.             friendsOfFriendsOfStudent.addAll(getFriendsOfStudent(friendOfStudent));
  72.         }
  73.         return friendsOfFriendsOfStudent;
  74.     }
  75.  
  76.     /**
  77.      * This method returns all friends of friends of a student, that are not
  78.      * friends to the passed student(Third exercise!)
  79.      *
  80.      * @param student
  81.      * @return List<Student>
  82.      */
  83.     public List<Student> getFriendsThatAreNotMyFriends(Student student) {
  84.         List<Student> notMyFriends = new ArrayList<Student>();
  85.         List<Student> friendsOfStudent = getFriendsOfStudent(student);
  86.         List<Student> friendsOfFriendsOfStudent = getFriendsOfFriendsOfStudent(student);
  87.  
  88.         for (Iterator<Student> iterator = friendsOfFriendsOfStudent.iterator(); iterator.hasNext();) {
  89.             Student friendOfFriend = (Student) iterator.next();
  90.             if (!friendsOfStudent.contains(friendOfFriend)) {
  91.                 notMyFriends.add(friendOfFriend);
  92.             }
  93.  
  94.         }
  95.  
  96.         return notMyFriends;
  97.     }
  98.  
  99.     private class Relationship {
  100.         private Student firstStudent;
  101.         private Student secondStudent;
  102.  
  103.         public Relationship(Student firstStudent, Student secondStudent) {
  104.             super();
  105.             this.firstStudent = firstStudent;
  106.             this.secondStudent = secondStudent;
  107.         }
  108.  
  109.         public Student getFirstStudent() {
  110.             return firstStudent;
  111.         }
  112.  
  113.         public Student getSecondStudent() {
  114.             return secondStudent;
  115.         }
  116.  
  117.     }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement