Bladeren bron

Finished my map

Trinh Tong 6 jaren geleden
bovenliggende
commit
0f7fb365c9
4 gewijzigde bestanden met toevoegingen van 165 en 0 verwijderingen
  1. 21
    0
      src/main/java/MyEntry.java
  2. 50
    0
      src/main/java/MyMap.java
  3. 26
    0
      src/test/java/EntryTest.java
  4. 68
    0
      src/test/java/MyMapTest.java

+ 21
- 0
src/main/java/MyEntry.java Bestand weergeven

@@ -0,0 +1,21 @@
1
+public class MyEntry<K, V> {
2
+    private final K key;
3
+    private V value;
4
+
5
+    public MyEntry(K key, V value) {
6
+        this.key = key;
7
+        this.value = value;
8
+    }
9
+
10
+    public K getKey() {
11
+        return key;
12
+    }
13
+
14
+    public V getValue() {
15
+        return value;
16
+    }
17
+
18
+    public void setValue(V value) {
19
+        this.value = value;
20
+    }
21
+}

+ 50
- 0
src/main/java/MyMap.java Bestand weergeven

@@ -0,0 +1,50 @@
1
+public class MyMap<K, V> {
2
+    private MyArrayList<MyEntry> myEntries = new MyArrayList<>();
3
+
4
+    public MyMap() {
5
+
6
+    }
7
+
8
+    public void put(int i, String s) {
9
+        myEntries.add(new MyEntry(i, s));
10
+    }
11
+
12
+    public MyArrayList<K> keySet() {
13
+
14
+        MyArrayList<K> keys = new MyArrayList<>();
15
+        Object[] entries = myEntries.getMyArrayList();
16
+        for (int i = 0; i < myEntries.getSize(); i++) {
17
+            MyEntry entry = (MyEntry) entries[i];
18
+            keys.add((K)entry.getKey());
19
+        }
20
+
21
+        return keys;
22
+    }
23
+
24
+    public MyArrayList<V> valueSet() {
25
+        MyArrayList<V> values = new MyArrayList<>();
26
+        Object[] entries = myEntries.getMyArrayList();
27
+        for (int i = 0; i < myEntries.getSize(); i++) {
28
+            MyEntry entry = (MyEntry) entries[i];
29
+            values.add((V)entry.getValue());
30
+        }
31
+
32
+        return values;
33
+    }
34
+
35
+    public V get(K key) {
36
+        Object[] entries = myEntries.getMyArrayList();
37
+        for (int i = 0; i < myEntries.getSize(); i++) {
38
+            MyEntry entry = (MyEntry) entries[i];
39
+            if (entry.getKey().equals(key)) {
40
+                return (V) entry.getValue();
41
+            }
42
+        }
43
+        return null;
44
+    }
45
+
46
+    public MyArrayList<MyEntry> getMyEntries() {
47
+        return myEntries;
48
+    }
49
+
50
+}

+ 26
- 0
src/test/java/EntryTest.java Bestand weergeven

@@ -0,0 +1,26 @@
1
+import org.junit.Assert;
2
+import org.junit.Before;
3
+import org.junit.Test;
4
+
5
+public class EntryTest {
6
+    MyEntry testEntry;
7
+
8
+    @Before
9
+    public void setUp() {
10
+        testEntry = new MyEntry(1, "Tea");
11
+
12
+    }
13
+
14
+    @Test
15
+    public void testGet() {
16
+        String expValue = "Tea";
17
+        Assert.assertEquals(expValue, testEntry.getValue());
18
+    }
19
+
20
+    @Test
21
+    public void testSet() {
22
+        String expValie = "Moolah";
23
+        testEntry.setValue("Moolah");
24
+        Assert.assertEquals(expValie, testEntry.getValue());
25
+    }
26
+}

+ 68
- 0
src/test/java/MyMapTest.java Bestand weergeven

@@ -0,0 +1,68 @@
1
+import org.junit.Assert;
2
+import org.junit.Before;
3
+import org.junit.Test;
4
+
5
+public class MyMapTest {
6
+    MyMap testMap;
7
+
8
+    @Before
9
+    public void setUp() throws Exception {
10
+        testMap = new MyMap<Integer, String>();
11
+    }
12
+
13
+    @Test
14
+    public void testPut() {
15
+        int expectedSize = 2;
16
+        testMap.put(100, "T-Shirt");
17
+        testMap.put(100, "T-Shirt");
18
+
19
+        Assert.assertEquals(expectedSize, testMap.getMyEntries().getSize());
20
+    }
21
+
22
+    @Test
23
+    public void testGetValue() {
24
+        testMap.put(100, "T-Shirt");
25
+        String expectedValue = "T-Shirt";
26
+
27
+        Assert.assertEquals(expectedValue, testMap.get(100));
28
+    }
29
+
30
+    @Test
31
+    public void testKeySet() {
32
+        // adds a new entry to the map
33
+        testMap.put(100, "T-Shirt");
34
+        testMap.put(100, "T-Shirt");
35
+        testMap.put(100, "T-Shirt");
36
+
37
+        testMap.getMyEntries().getMyArrayList();
38
+
39
+        MyArrayList<Integer> keys = testMap.keySet();
40
+        for (int i = 0; i < testMap.getMyEntries().getSize(); i++) {
41
+            System.out.println(keys.get(i));
42
+        }
43
+
44
+        Assert.assertEquals(testMap.getMyEntries().getSize(), keys.getSize());
45
+
46
+        }
47
+
48
+    @Test
49
+    public void testValues() {
50
+
51
+        testMap.put(100, "T-Shirt M");
52
+        testMap.put(200, "T-Shirt XL");
53
+        testMap.put(300, "T-Shirt S");
54
+        testMap.put(400, "T-Shirt SM");
55
+        testMap.put(500, "T-Shirt LL");
56
+        testMap.put(600, "T-Shirt OO");
57
+
58
+        MyArrayList<String> values = testMap.valueSet();
59
+        for (int i = 0; i < testMap.getMyEntries().getSize(); i++) {
60
+            System.out.println(values.get(i));
61
+        }
62
+
63
+        Assert.assertEquals(testMap.getMyEntries().getSize(), values.getSize());
64
+    }
65
+
66
+
67
+}
68
+