瀏覽代碼

done part 4

Katrice Williams-Dredden 6 年之前
父節點
當前提交
8f9590d49a

+ 0
- 2
src/main/java/Table/Entry.java 查看文件

@@ -10,12 +10,10 @@ public class Entry<K, V> {
10 10
     }
11 11
 
12 12
     public K getKey() {
13
-
14 13
         return key;
15 14
     }
16 15
 
17 16
     public V getValue() {
18
-
19 17
         return value;
20 18
     }
21 19
 

+ 0
- 2
src/main/java/Table/Table.java 查看文件

@@ -53,5 +53,3 @@ public class Table<K, V> {
53 53
 }
54 54
 
55 55
 
56
-//Steps
57
-//made the ArrayList Manage the Entry entries

+ 57
- 0
src/main/java/TableNested/TableNested.java 查看文件

@@ -7,5 +7,62 @@ import java.util.ArrayList;
7 7
  * Think about how nested classes should work with generics.
8 8
  */
9 9
 public class TableNested<K, V> {
10
+    private ArrayList<Table.Entry> entries;
10 11
 
12
+    public TableNested() {
13
+        this.entries = new ArrayList<>();
14
+    }
15
+
16
+    public Object get(K key) {
17
+        for (Table.Entry e : entries) {
18
+            if (e.getKey().equals(key)) {
19
+                return e.getValue();
20
+            }
21
+        }
22
+        return null;
23
+    }
24
+
25
+    public void put(K key, V value) {
26
+        //need to enter value and key into entry
27
+        int counter = 0;
28
+        for (Table.Entry e : entries) {
29
+            if (e.getKey().equals(key)) {
30
+                entries.set(counter, new Table.Entry<>(key, value));
31
+                return;
32
+            }
33
+        }
34
+        entries.add(new Table.Entry<>(key, value));
35
+    }
36
+
37
+    public void remove(K key) {
38
+        //needs to take a key, and if that key exists in the arraylist, remove the item
39
+        for (Table.Entry e : entries) {
40
+            if (e.getKey().equals(key)) {
41
+                entries.remove(e);
42
+                return;
43
+            }
44
+        }
45
+
46
+
47
+    }
48
+
49
+
50
+    public class Entry<K, V> {
51
+        private K key;
52
+        private V value;
53
+
54
+        public Entry(K key, V value) {
55
+            this.key = key;
56
+            this.value = value;
57
+        }
58
+
59
+        public K getKey() {
60
+            return key;
61
+        }
62
+
63
+        public V getValue() {
64
+            return value;
65
+        }
66
+
67
+    }
11 68
 }

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