Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Iterator;
- import java.util.List;
- public class StudentManager {
- private List<Student> students;
- private List<Relationship> friends;
- public StudentManager() {
- students = new ArrayList<Student>();
- friends = new ArrayList<Relationship>();
- }
- /**
- * This method is used to add students in the list of students
- *
- * @param student
- */
- public void addStudent(Student student) {
- students.add(student);
- }
- /**
- * This method is used to make two students friends.
- *
- * @return true if they became friends. False if they does not exist in list
- * of students
- */
- public boolean makeThisStudentsFriends(Student firstFriend, Student secondFriend) {
- if (students.contains(firstFriend) && students.contains(secondFriend)) {
- friends.add(new Relationship(firstFriend, secondFriend));
- return true;
- }
- return false;
- }
- /**
- * This method returns all friends of a student(First exercise!)
- *
- * @param student
- * @return List<Student>
- */
- public List<Student> getFriendsOfStudent(Student student) {
- List<Student> friendsOfStudent = new ArrayList<Student>();
- for (Iterator<Relationship> iterator = friends.iterator(); iterator.hasNext();) {
- Relationship relationship = (Relationship) iterator.next();
- if (relationship.getFirstStudent().equals(student)) {
- friendsOfStudent.add(relationship.getSecondStudent());
- } else if (relationship.getSecondStudent().equals(student)) {
- friendsOfStudent.add(relationship.getFirstStudent());
- } else {
- continue;
- }
- }
- return friendsOfStudent;
- }
- /**
- * This method returns all friends of friends of a student(Second exercise!)
- *
- * @param student
- * @return List<Student>
- */
- public List<Student> getFriendsOfFriendsOfStudent(Student student) {
- List<Student> friendsOfFriendsOfStudent = new ArrayList<Student>();
- List<Student> friendsOfStudent = getFriendsOfStudent(student);
- for (Iterator<Student> iterator = friendsOfStudent.iterator(); iterator.hasNext();) {
- Student friendOfStudent = (Student) iterator.next();
- friendsOfFriendsOfStudent.addAll(getFriendsOfStudent(friendOfStudent));
- }
- return friendsOfFriendsOfStudent;
- }
- /**
- * This method returns all friends of friends of a student, that are not
- * friends to the passed student(Third exercise!)
- *
- * @param student
- * @return List<Student>
- */
- public List<Student> getFriendsThatAreNotMyFriends(Student student) {
- List<Student> notMyFriends = new ArrayList<Student>();
- List<Student> friendsOfStudent = getFriendsOfStudent(student);
- List<Student> friendsOfFriendsOfStudent = getFriendsOfFriendsOfStudent(student);
- for (Iterator<Student> iterator = friendsOfFriendsOfStudent.iterator(); iterator.hasNext();) {
- Student friendOfFriend = (Student) iterator.next();
- if (!friendsOfStudent.contains(friendOfFriend)) {
- notMyFriends.add(friendOfFriend);
- }
- }
- return notMyFriends;
- }
- private class Relationship {
- private Student firstStudent;
- private Student secondStudent;
- public Relationship(Student firstStudent, Student secondStudent) {
- super();
- this.firstStudent = firstStudent;
- this.secondStudent = secondStudent;
- }
- public Student getFirstStudent() {
- return firstStudent;
- }
- public Student getSecondStudent() {
- return secondStudent;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement