Parcourir la source

tablenested complete

Amy Gill il y a 6 ans
Parent
révision
fd54f06bd4

+ 64
- 0
src/main/java/TableNested/TableNested.java Voir le fichier

@@ -8,4 +8,68 @@ import java.util.ArrayList;
8 8
  */
9 9
 public class TableNested<K, V> {
10 10
 
11
+    private ArrayList<Entry> entries;
12
+
13
+    public TableNested() {
14
+        entries= new ArrayList<>();
15
+    }
16
+
17
+
18
+    public class Entry<K, V> {
19
+        private K key;
20
+        private V value;
21
+
22
+        public Entry(K key, V value) {
23
+            this.key = key;
24
+            this.value = value;
25
+        }
26
+
27
+        public K getKey() {
28
+            return key;
29
+        }
30
+
31
+        public V getValue() {
32
+            return value;
33
+        }
34
+
35
+    }
36
+
37
+
38
+    public V get(K key){
39
+
40
+        V ourLocalVariable = null;
41
+
42
+
43
+        for (Entry element: entries){
44
+            if (element.getKey().equals(key)){
45
+                ourLocalVariable =(V) element.getValue();
46
+            }
47
+        }
48
+
49
+        return ourLocalVariable;
50
+
51
+    }
52
+
53
+    public void put(K foo, V i){
54
+        Entry ourEntry = new Entry(foo, i);
55
+        for (Entry element: entries){
56
+            if (element.getKey().equals(ourEntry.getKey())){
57
+                entries.set(entries.indexOf(element),ourEntry);
58
+            }
59
+        }
60
+
61
+        entries.add(ourEntry);
62
+
63
+    }
64
+
65
+    public void remove(K foo){
66
+        for (Entry element: entries){
67
+            if (element.getKey().equals(foo)){
68
+                entries.remove(element);
69
+                break;
70
+            }
71
+        }
72
+    }
73
+
74
+
11 75
 }

+ 50
- 50
src/test/java/TableNested/TableNestedTest.java Voir le fichier

@@ -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
+}