package ru.matevosyan;

import java.util.*;

/**
 * User class.
 * Created on 28.04.2017.
 *
 * @author Matevosyan Vardan
 * @version 1.0
 */

public class Department implements Comparable<Department> {

    private TreeSet<String> name;

    /**
     * Department class constructor.
     *
     * @param name User name.
     */

    public Department(TreeSet<String> name) {
        this.name = name;
    }

    /**
     * Override compareTo(Department department) method to compare department.
     * @param department is Department object.
     * @return int value.
     */


    @Override
    public int compareTo(Department department) {
        checkDepartmentCode();
        Iterator iterator = department.getName().iterator();
        int check = -1;

        while (iterator.hasNext()) {
            check = this.name.iterator().next().compareTo(String.valueOf(iterator.next()));
        }
        return check;
    }



    public TreeSet<String> getName() {
        return name;
    }

    public void checkDepartmentCode() {
        Iterator<String> iteratorSetString = name.iterator();
        String departmentName;

        while (iteratorSetString.hasNext()) {
            departmentName = iteratorSetString.next();

            String departmentCode = departmentName.substring(0, departmentName.indexOf("\\"));

            if (!this.name.contains(departmentCode)) {
                this.name.add(departmentCode);
            }
        }

    }

    public void descendingSort() {
        checkDepartmentCode();
        this.name = new TreeSet<>(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o2.compareTo(o1);
            }
        });

    }

}

@@ -49,18 +49,40 @@
 %0A * 
-User class
+Department class to sort treeSet
 .%0A *
@@ -97,23 +97,20 @@
  on 
-28.04
+11.05
 .2017.%0A
- *%0A
  * @
@@ -300,15 +300,8 @@
 or.%0A
-     *%0A
     
@@ -319,12 +319,24 @@
 ame 
-User
+Department codes
  nam
@@ -664,39 +664,8 @@
 ) %7B%0A
-        checkDepartmentCode();%0A
     
@@ -918,92 +918,323 @@
  %7D%0A%0A
-%0A%0A    public TreeSet%3CString%3E getName() %7B%0A        return name;%0A    %7D%0A%0A    public void
+    /**%0A     * Getter for name.%0A     * @return name set of department code.%0A     */%0A%0A    public TreeSet%3CString%3E getName() %7B%0A        return name;%0A    %7D%0A%0A    /**%0A     * checkDepartmentCode() was created for add key value on the top of hierarchy.%0A     * @return ArrayList%3CString%3E.%0A     */%0A%0A    public ArrayList%3CString%3E
  che
@@ -1344,24 +1344,76 @@
 tmentName;%0A%0A
+        ArrayList%3CString%3E list = new ArrayList%3C%3E();%0A
         whil
@@ -1501,17 +1501,69 @@
 next();%0A
-%0A
+            if (departmentName.contains(%22%5C%5C%22)) %7B%0A    
         
@@ -1649,17 +1649,20 @@
 %22%5C%5C%22));%0A
-%0A
+    
         
@@ -1704,16 +1704,17 @@
 entCode)
+ 
 ) %7B%0A    
@@ -1725,25 +1725,24 @@
         
-this.name
+    list
 .add(dep
@@ -1768,302 +1768,61 @@
     
-   %7D%0A
        
- %7D%0A
+%7D
 %0A    
-%7D%0A%0A
     
-public void descendingSort() %7B%0A        checkDepartmentCode();%0A        this.name = new TreeSet%3C%3E(new Comparator%3CString%3E() %7B%0A            @Override%0A            public int compare(String o1, String o2) %7B%0A                return o2.compareTo(o1);%0A            %7D%0A        %7D);%0A
+    %7D%0A        %7D%0A        return list;
 %0A   

change Department.java it is work