Keith Brinker 6 лет назад
Родитель
Сommit
dfff8188ac
2 измененных файлов: 114 добавлений и 51 удалений
  1. 64
    1
      src/main/java/TableNested/TableNested.java
  2. 50
    50
      src/test/java/TableNested/TableNestedTest.java

+ 64
- 1
src/main/java/TableNested/TableNested.java Просмотреть файл

@@ -1,5 +1,4 @@
1 1
 package TableNested;
2
-
3 2
 import java.util.ArrayList;
4 3
 
5 4
 /**
@@ -7,5 +6,69 @@ import java.util.ArrayList;
7 6
  * Think about how nested classes should work with generics.
8 7
  */
9 8
 public class TableNested<K, V> {
9
+    private ArrayList<Entry> entries;
10
+
11
+    public TableNested() {
12
+        this.entries = new ArrayList<Entry>();
13
+    }
14
+
15
+    public class Entry<K, V> {
16
+        private K key;
17
+        private V value;
18
+
19
+        public Entry(K key, V value) {
20
+            this.key = key;
21
+            this.value = value;
22
+        }
23
+
24
+        public K getKey() {
25
+            return key;
26
+        }
27
+
28
+        public V getValue() {
29
+            return value;
30
+        }
31
+
32
+        public void setKey(K key){
33
+            this.key = key;
34
+        }
35
+
36
+        public void setValue(V value){
37
+            this.value = value;
38
+        }
39
+
40
+    }
41
+
42
+
43
+
44
+
45
+
46
+    public V get(K key) {
47
+        for (int i = 0; i < entries.size(); i++){
48
+            if (entries.get(i).getKey().equals(key)){
49
+                return (V)entries.get(i).getValue();
50
+            }
51
+
52
+        }
53
+        return null;
54
+    }
55
+
56
+    public void put(K key, V value) {
57
+        for (int i =0; i < entries.size(); i++){
58
+            if (entries.get(i).getKey().equals(key)){
59
+                System.out.println("Error, already exists");
60
+            }
61
+            entries.get(i).setKey(key);
62
+            entries.get(i).setValue(value);
63
+        }
64
+        entries.add(new Entry(key, value));
65
+    }
10 66
 
67
+    public void remove(K key) {
68
+        for (int i = 0; i < entries.size(); i++){
69
+            if (entries.get(i).getKey().equals(key)){
70
+                entries.remove(i);
71
+            }
72
+        }
73
+    }
11 74
 }

+ 50
- 50
src/test/java/TableNested/TableNestedTest.java Просмотреть файл

@@ -1,50 +1,50 @@
1
-//package TableNested;
2
-//
3
-//import org.junit.Assert;
4
-//import org.junit.Test;
5
-//
6
-//public class TableNestedTest {
7
-//    @Test
8
-//    public void testGetWithoutAnItemReturnsNull() throws Exception {
9
-//        // Given an empty table
10
-//        TableNested<String, Integer> table = new TableNested<String, Integer>();
11
-//        // When we try and get an item then it returns null
12
-//        Assert.assertEquals(table.get("foo"), null);
13
-//    }
14
-//
15
-//    @Test
16
-//    public void testPutAnItemReturnsAndDoesNotDelete() throws Exception {
17
-//        //Given an empty table
18
-//        TableNested<String, Integer> table = new TableNested<String, Integer>();
19
-//        // When we put an item in it
20
-//        table.put("foo", 1);
21
-//        // Then we should be able to get it's value
22
-//        Assert.assertEquals(table.get("foo"), new Integer(1));
23
-//        // And then we should be able to get it again as it wasn't removed
24
-//        Assert.assertEquals(table.get("foo"), new Integer(1));
25
-//    }
26
-//
27
-//    @Test
28
-//    public void testOverwritingAnItem() throws Exception {
29
-//        //Given an empty table
30
-//        TableNested<String, Integer> table = new TableNested<String, Integer>();
31
-//        // When we put an item in it
32
-//        table.put("foo", 1);
33
-//        // And we put a new value with the same key
34
-//        table.put("foo", 2);
35
-//        // Then we should get back the new value
36
-//        Assert.assertEquals(table.get("foo"), new Integer(2));
37
-//    }
38
-//
39
-//    @Test
40
-//    public void testRemoveAnItem() throws Exception {
41
-//        //Given an empty table
42
-//        TableNested<String, Integer> table = new TableNested<String, Integer>();
43
-//        // When we put an item in it
44
-//        table.put("foo", 1);
45
-//        // And we remove that item
46
-//        table.remove("foo");
47
-//        // Then we should get back null for that balue
48
-//        Assert.assertEquals(table.get("foo"), null);
49
-//    }
50
-//}
1
+package TableNested;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class TableNestedTest {
7
+    @Test
8
+    public void testGetWithoutAnItemReturnsNull() throws Exception {
9
+        // Given an empty table
10
+        TableNested<String, Integer> table = new TableNested<String, Integer>();
11
+        // When we try and get an item then it returns null
12
+        Assert.assertEquals(table.get("foo"), null);
13
+    }
14
+
15
+    @Test
16
+    public void testPutAnItemReturnsAndDoesNotDelete() throws Exception {
17
+        //Given an empty table
18
+        TableNested<String, Integer> table = new TableNested<String, Integer>();
19
+        // When we put an item in it
20
+        table.put("foo", 1);
21
+        // Then we should be able to get it's value
22
+        Assert.assertEquals(table.get("foo"), new Integer(1));
23
+        // And then we should be able to get it again as it wasn't removed
24
+        Assert.assertEquals(table.get("foo"), new Integer(1));
25
+    }
26
+
27
+    @Test
28
+    public void testOverwritingAnItem() throws Exception {
29
+        //Given an empty table
30
+        TableNested<String, Integer> table = new TableNested<String, Integer>();
31
+        // When we put an item in it
32
+        table.put("foo", 1);
33
+        // And we put a new value with the same key
34
+        table.put("foo", 2);
35
+        // Then we should get back the new value
36
+        Assert.assertEquals(table.get("foo"), new Integer(2));
37
+    }
38
+
39
+    @Test
40
+    public void testRemoveAnItem() throws Exception {
41
+        //Given an empty table
42
+        TableNested<String, Integer> table = new TableNested<String, Integer>();
43
+        // When we put an item in it
44
+        table.put("foo", 1);
45
+        // And we remove that item
46
+        table.remove("foo");
47
+        // Then we should get back null for that balue
48
+        Assert.assertEquals(table.get("foo"), null);
49
+    }
50
+}