Eric Cordell пре 6 година
родитељ
комит
f071766793
3 измењених фајлова са 118 додато и 54 уклоњено
  1. 14
    0
      src/main/java/Table/Entry.java
  2. 54
    4
      src/main/java/Table/Table.java
  3. 50
    50
      src/test/java/Table/TableTest.java

+ 14
- 0
src/main/java/Table/Entry.java Прегледај датотеку

18
     }
18
     }
19
 
19
 
20
 }
20
 }
21
+
22
+
23
+//Table -- Implemented for you is Entry<K, V>.
24
+//        Implement Table<K,V> so that it manages an ArrayList of Entry<K,V>.
25
+
26
+//         You must implement get which takes a key and returns either the entry
27
+//        from the ArrayList with that key, or null if none is found.
28
+
29
+//        put which takes a key and value and sets the value in the ArrayList to Entry(key, value);
30
+
31
+//        Remember, a key point to exactly one value
32
+//        remove which takes a key and removes it from the ArrayList if it's in there.
33
+
34
+//        It's a void method; no return type.

+ 54
- 4
src/main/java/Table/Table.java Прегледај датотеку

3
 import java.util.ArrayList;
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
+ * <p>
8
+ * It needs a get method that takes a key and returns
9
+ * its corresponding value, or null of the key is not in the arraylist.
10
+ * <p>
11
+ * It needs a put method that takes a key and value and makes an entry with key, value.
12
+ * <p>
13
+ * NOTE: There cannot be two entries with the same key.
14
+ * <p>
9
  * It also needs a remove method which takes a key and, if that key exists in the arraylist, removes that item.
15
  * It also needs a remove method which takes a key and, if that key exists in the arraylist, removes that item.
10
  * Void return on `remove`.
16
  * Void return on `remove`.
11
  */
17
  */
12
 public class Table<K, V> {
18
 public class Table<K, V> {
13
-    private ArrayList entries;
19
+    private ArrayList<Entry> entries;
14
 
20
 
15
     public Table() {
21
     public Table() {
22
+        entries = new ArrayList<>();
23
+    }
24
+
25
+    public Object get(K key) {
26
+        for (Entry entry : entries) {
27
+            if (entry.getKey().equals(key)) {
28
+                return entry.getValue();
29
+            }
30
+        }
31
+        return null;
32
+    }
33
+
34
+
35
+    public void put(K key, V value) {
36
+        int count = 0;
37
+        for (Entry entry : entries) {
38
+            if (entry.getKey().equals(key)) {
39
+                entries.set(count, new Entry(key, value));
40
+                count++;
41
+            }
42
+        }
43
+        entries.add(new Entry(key, value));
44
+    }
45
+
46
+    public void remove(K key) {
47
+        for (int i = 0; i < entries.size(); i++) {
48
+            if (entries.get(i).getKey().equals(key)) {
49
+                entries.remove(i);
50
+            }
51
+        }
16
     }
52
     }
17
 }
53
 }
54
+
55
+
56
+//Table -- Implemented for you is Entry<K, V>.
57
+//        Implement Table<K,V> so that it manages an ArrayList of Entry<K,V>.
58
+
59
+//         You must implement get which takes a key and returns either the entry
60
+//        from the ArrayList with that key, or null if none is found.
61
+
62
+//        put which takes a key and value and sets the value in the ArrayList to Entry(key, value);
63
+
64
+//        Remember, a key point to exactly one value
65
+//        remove which takes a key and removes it from the ArrayList if it's in there.
66
+
67
+//        It's a void method; no return type.

+ 50
- 50
src/test/java/Table/TableTest.java Прегледај датотеку

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