Pārlūkot izejas kodu

finished part 3 and 4

Luis Romero 6 gadus atpakaļ
vecāks
revīzija
ff51faeb62

+ 52
- 0
src/main/java/TableNested/TableNested.java Parādīt failu

@@ -1,11 +1,63 @@
1 1
 package TableNested;
2 2
 
3 3
 import java.util.ArrayList;
4
+import java.util.Map.Entry;
5
+
4 6
 
5 7
 /**
6 8
  * All you need to do for this microlab is take the Table and Entry from the last one and make Entry a nested class.
7 9
  * Think about how nested classes should work with generics.
8 10
  */
9 11
 public class TableNested<K, V> {
12
+    private ArrayList<Entry> entries;
13
+
14
+    public TableNested() {
15
+        this.entries = new ArrayList<>();
16
+    }
17
+
18
+    public void put(K key, V value) {
19
+        for (Entry entry : this.entries) {
20
+            if (entry.getKey().equals(key)) {
21
+                entries.set(entries.indexOf(entry), new Entry<>(key, value));
22
+                return;
23
+            }
24
+        }
25
+        entries.add(new Entry<>(key, value));
26
+    }
27
+
28
+    public Object get(K key) {
29
+        for (Entry entry : this.entries) {
30
+            if (entry.getKey().equals(key)) {
31
+                return entry.getValue();
32
+            }
33
+        }
34
+        return null;
35
+    }
36
+
37
+    public void remove(K key) {
38
+        for (int i = 0; i < this.entries.size(); i++) {
39
+            if (this.entries.get(i).getKey().equals(key)) {
40
+                this.entries.remove(i);
41
+            }
42
+        }
43
+    }
44
+
45
+    private class Entry<K, V> {
46
+        private K key;
47
+        private V value;
48
+
49
+        public Entry(K key, V value) {
50
+            this.key = key;
51
+            this.value = value;
52
+        }
53
+
54
+        public K getKey() {
55
+            return key;
56
+        }
57
+
58
+        public V getValue() {
59
+            return value;
60
+        }
10 61
 
62
+    }
11 63
 }

+ 15
- 15
src/test/java/Swap/SwapTest.java Parādīt failu

@@ -1,16 +1,16 @@
1 1
 package Swap;
2
-//
3
-//import org.junit.Assert;
4
-//import org.junit.Test;
5
-//
6
-///**
7
-// * Get the tests passing.
8
-// */
9
-//public class SwapTest {
10
-//    @Test
11
-//    public void testSwap() throws Exception {
12
-//        Double[] result = Swap.swap(0,1, 1.5, 2,3);
13
-//        Double[] expected = {2.0, 1.5, 3.0};
14
-//        Assert.assertArrayEquals(expected, result);
15
-//    }
16
-//}
2
+
3
+import org.junit.Assert;
4
+import org.junit.Test;
5
+
6
+/**
7
+ * Get the tests passing.
8
+ */
9
+public class SwapTest {
10
+    @Test
11
+    public void testSwap() throws Exception {
12
+        Double[] result = Swap.swap(0,1, 1.5, 2,3);
13
+        Double[] expected = {2.0, 1.5, 3.0};
14
+        Assert.assertArrayEquals(expected, result);
15
+    }
16
+}

+ 50
- 50
src/test/java/TableNested/TableNestedTest.java Parādīt failu

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