Explorar el Código

finished part 3

Kaitrina High hace 6 años
padre
commit
adc10bf47d
Se han modificado 3 ficheros con 107 adiciones y 54 borrados
  1. 4
    0
      src/main/java/Table/Entry.java
  2. 53
    4
      src/main/java/Table/Table.java
  3. 50
    50
      src/test/java/Table/TableTest.java

+ 4
- 0
src/main/java/Table/Entry.java Ver fichero

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

+ 53
- 4
src/main/java/Table/Table.java Ver fichero

@@ -3,15 +3,64 @@ package Table;
3 3
 import java.util.ArrayList;
4 4
 
5 5
 /**
6
- * This class needs to manage an ArrayList of Entry objects.  It needs a get method that takes a key and returns
7
- * its corresponding value, or null of the key is not in the arraylist.  It needs a put method that takes a key and value
8
- * and makes an entry with key, value.  NOTE: There cannot be two entries with the same key.
6
+ * This class needs to manage an ArrayList of Entry objects.
7
+ * It needs a get method that takes a key and returns its corresponding value,
8
+ * or null of the key is not in the arraylist.
9
+ * It needs a put method that takes a key and value and makes an entry with key, value.
10
+ * NOTE: There cannot be two entries with the same key.
9 11
  * It also needs a remove method which takes a key and, if that key exists in the arraylist, removes that item.
10 12
  * Void return on `remove`.
11 13
  */
12 14
 public class Table<K, V> {
13
-    private ArrayList entries;
15
+    //private ArrayList entries;
16
+    private ArrayList<Entry<K, V>> entries;
14 17
 
15 18
     public Table() {
19
+        this.entries = new ArrayList<>();
20
+    }
21
+
22
+    /*
23
+    takes a key and returns either the entry from the arraylist with that key or null if none is found
24
+     */
25
+    public V get(K key) {
26
+        for (int i = 0; i < entries.size(); i++) {
27
+            if (entries.get(i).getKey().equals(key)) {
28
+                return (V) entries.get(i).getValue();
29
+            }
30
+        }
31
+        return null;
32
+    }
33
+
34
+    /*
35
+    put takes a key value and sets the value in the Arraylist with that key or null if none is found
36
+    note keys are unique
37
+     */
38
+    public void put(K key, V value) {
39
+        //check to see if key exists, if so replace value, if not add both key and value
40
+        for (int i = 0; i < entries.size(); i++) {
41
+            if (entries.get(i).getKey().equals(key)) {
42
+                entries.remove(i);
43
+                entries.add(new Entry<>(key, value));
44
+                break;
45
+            }
46
+        }
47
+        entries.add(new Entry<>(key, value));
48
+    }
49
+
50
+
51
+    /*
52
+    remove which takes a key and removes it from the arraylist if its in there. Its a void method no return type
53
+     */
54
+    public void remove(K key) {
55
+        for (int i = 0; i < entries.size(); i++) {
56
+            if (entries.get(i).getKey().equals(key)) {
57
+                entries.remove(i);
58
+                break;
59
+            }
60
+
61
+        }
62
+
16 63
     }
17 64
 }
65
+
66
+

+ 50
- 50
src/test/java/Table/TableTest.java Ver fichero

@@ -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(table.get("foo"), null);
49
+    }
50
+}