瀏覽代碼

table tests passing

Amy Gill 6 年之前
父節點
當前提交
309375dd0e
共有 2 個檔案被更改,包括 102 行新增51 行删除
  1. 52
    1
      src/main/java/Table/Table.java
  2. 50
    50
      src/test/java/Table/TableTest.java

+ 52
- 1
src/main/java/Table/Table.java 查看文件

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

+ 50
- 50
src/test/java/Table/TableTest.java 查看文件

@@ -1,50 +1,50 @@
1
-//package Table;
2
-//
3
-//import org.junit.Assert;
4
-//import org.junit.Test;
5
-//
6
-//public class TableTest {
7
-//    @Test
8
-//    public void testGetWithoutAnItemReturnsNull() throws Exception {
9
-//        // Given an empty table
10
-//        Table<String, Integer> table = new Table<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
-//        Table<String, Integer> table = new Table<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
-//        Table<String, Integer> table = new Table<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
-//        Table<String, Integer> table = new Table<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 Table;
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+public class TableTest {
7
+    @Test
8
+    public void testGetWithoutAnItemReturnsNull() throws Exception {
9
+        // Given an empty table
10
+        Table<String, Integer> table = new Table<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
+        Table<String, Integer> table = new Table<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
+        Table<String, Integer> table = new Table<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
+        Table<String, Integer> table = new Table<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(null, table.get("foo"));
49
+    }
50
+}