Explorar el Código

group and sorted group passing

Lauren Green hace 6 años
padre
commit
f6d5de57e8

+ 24
- 10
src/main/java/rocks/zipcode/io/quiz4/collections/ComparableTreeSet.java Ver fichero

@@ -5,31 +5,45 @@ import java.util.TreeSet;
5 5
 /**
6 6
  * @author leon on 11/12/2018.
7 7
  */
8
-public class ComparableTreeSet<T> implements Comparable<T> {
8
+public class ComparableTreeSet<T> extends TreeSet<T> implements Comparable<ComparableTreeSet<T>> {
9 9
 
10 10
     TreeSet<T> treeSet;
11 11
 
12 12
     public ComparableTreeSet(T... arr) {
13
-        for(T item: arr){
14
-            treeSet.add(item);
13
+        this.treeSet = new TreeSet<>();
14
+        for(T item: arr) {
15
+            this.treeSet.add(item);
15 16
         }
16 17
     }
17 18
 
18 19
     public ComparableTreeSet() {
19
-    }
20
-
21
-    public Integer compareTo(ComparableTreeSet<T> o) {
22
-
23
-        return 0;
20
+        this.treeSet = new TreeSet<>();
24 21
     }
25 22
 
26 23
     public TreeSet<T> getTreeSet() {
27 24
         return treeSet;
28 25
     }
29 26
 
30
-    @Override
31
-    public int compareTo(T o) {
27
+    public int compareTo(ComparableTreeSet<T> o) {
32 28
 
29
+//        return this.treeSet.first().compareTo(o.getTreeSet().first());
33 30
         return 0;
34 31
     }
32
+
33
+    @Override
34
+    public String toString() {
35
+        StringBuilder str = new StringBuilder();
36
+        str.append("[");
37
+
38
+        for(T item: this.treeSet) {
39
+            str.append(item.toString());
40
+            str.append(", ");
41
+        }
42
+        str.deleteCharAt(str.length() - 1);
43
+        str.deleteCharAt(str.length() - 1);
44
+
45
+        return str + "]";
46
+    }
47
+
48
+
35 49
 }

+ 38
- 11
src/main/java/rocks/zipcode/io/quiz4/generics/Group.java Ver fichero

@@ -1,37 +1,64 @@
1 1
 package rocks.zipcode.io.quiz4.generics;
2 2
 
3
+import java.util.ArrayList;
3 4
 import java.util.Iterator;
5
+import java.util.List;
4 6
 
5 7
 /**
6 8
  * @author leon on 18/12/2018.
7 9
  */
8
-public class Group<_> {
10
+public class Group<T> implements Iterable, GroupInterface {
11
+
12
+    List<T> groupList;
13
+
9 14
     public Group() {
10
-        throw new UnsupportedOperationException("Method not yet implemented");
15
+        this.groupList = new ArrayList<>();
11 16
     }
12 17
 
13 18
     public Integer count() {
14
-        return null;
19
+        return this.groupList.size();
15 20
     }
16 21
 
17
-    public void insert(_ value) {
22
+    @Override
23
+    public Boolean has(Object valueToInsert) {
24
+        return this.groupList.contains(valueToInsert);
18 25
     }
19 26
 
20
-    public Boolean has(_ value) {
21
-        return null;
27
+
28
+    public T fetch(int indexOfValue) {
29
+        return this.groupList.get(indexOfValue);
22 30
     }
23 31
 
24
-    public _ fetch(int indexOfValue) {
25
-        return null;
32
+    @Override
33
+    public void insert(Object string) {
34
+        this.groupList.add((T) string);
26 35
     }
27 36
 
28
-    public void delete(_ value) {
37
+    @Override
38
+    public void delete(Object valueToInsert) {
39
+        this.groupList.remove(valueToInsert);
29 40
     }
30 41
 
31 42
     public void clear() {
43
+        this.groupList.clear();
32 44
     }
33 45
 
34
-    public Iterator<_> iterator() {
35
-        return null;
46
+
47
+    @Override
48
+    public Iterator iterator() {
49
+        return this.groupList.iterator();
50
+    }
51
+
52
+    @Override
53
+    public String toString() {
54
+        StringBuilder str = new StringBuilder();
55
+        str.append("[");
56
+        for(T item: this.groupList){
57
+            str.append(item.toString() + ", ");
58
+        }
59
+        str.delete(str.length() - 2, str.length());
60
+        str.append("]");
61
+
62
+        return str.toString();
36 63
     }
37 64
 }

+ 14
- 5
src/main/java/rocks/zipcode/io/quiz4/generics/SortedGroup.java Ver fichero

@@ -1,18 +1,27 @@
1 1
 package rocks.zipcode.io.quiz4.generics;
2 2
 
3
+import java.util.Collections;
4
+
3 5
 /**
4 6
  * @author leon on 18/12/2018.
5 7
  */
6
-public class SortedGroup<_> extends Group<_> {
8
+public class SortedGroup<T extends Comparable> extends Group<T> {
9
+
7 10
     @Override
8
-    public void insert(_ value) {
11
+    public void insert(Object value) {
12
+        this.groupList.add((T)value);
13
+        Collections.sort(this.groupList);
9 14
     }
10 15
 
11 16
     @Override
12
-    public void delete(_ value) {
17
+    public void delete(Object value) {
18
+        this.groupList.remove(value);
19
+        Collections.sort(this.groupList);
20
+
13 21
     }
14 22
 
15
-    public Integer indexOf(_ firstValue) {
16
-        return null;
23
+    public Integer indexOf(T firstValue) {
24
+
25
+        return this.groupList.indexOf(firstValue);
17 26
     }
18 27
 }