Browse Source

refactored version

Yauheni Papko 6 years ago
parent
commit
6046ec71d5
4 changed files with 223 additions and 36 deletions
  1. 93
    4
      PhoneBook.java
  2. 84
    23
      PhoneBookTest.java
  3. 26
    9
      package.bluej
  4. 20
    0
      results.txt

+ 93
- 4
PhoneBook.java View File

@@ -1,7 +1,96 @@
1
- 
1
+import java.util.Iterator;
2
+import java.util.Map;
3
+import java.util.Set;
4
+import java.util.SortedMap;
5
+import java.util.TreeMap; 
6
+import java.util.ArrayList;
2 7
 
3
-/**
4
- * Created by leon on 1/23/18.
5
- */
6 8
 public class PhoneBook {
9
+    public TreeMap<String, ArrayList<String>> bookEntries;
10
+
11
+    public PhoneBook (){
12
+        this.bookEntries = new TreeMap<String, ArrayList<String>>();
13
+    }
14
+
15
+    public void add(String name, String phoneNumber) {
16
+        ArrayList<String> addMe = new ArrayList<String>();
17
+        if (bookEntries.containsKey(name)) {
18
+            addMe = bookEntries.get(name);
19
+        }
20
+        addMe.add(phoneNumber);
21
+        bookEntries.put(name, addMe);
22
+    }
23
+
24
+    public void remove(String name, String phoneNumber) {
25
+        ArrayList listOfNumbers = new ArrayList();
26
+        if (bookEntries.containsKey(name)) {
27
+            listOfNumbers = bookEntries.get(name);
28
+            listOfNumbers.remove(phoneNumber);
29
+        }
30
+        if (listOfNumbers.size() == 0) {
31
+            bookEntries.remove(name);
32
+        }
33
+    }
34
+
35
+    public void removeRecord(String name) {
36
+        if (bookEntries.containsKey(name)) {
37
+            bookEntries.remove(name);
38
+        }
39
+    }
40
+
41
+    public String lookup(String name) {
42
+        String phoneNumber = "This entry doesn't exist. Please enter valid entry.";
43
+        if (bookEntries.containsKey(name)) {
44
+            phoneNumber = bookEntries.get(name).toString().replace("[", "").replace("]", "");
45
+        }
46
+        return phoneNumber;
47
+    }
48
+
49
+    public String reverseLookup(String phoneNumber) {
50
+        String name = "This entry doesn't exist. Please enter valid entry.";
51
+        Set<String> keys = bookEntries.keySet();
52
+        for(String key: keys){
53
+            if(bookEntries.get(key).contains(phoneNumber) == true) {
54
+                name = key;
55
+            }
56
+        }
57
+        return name;
58
+    }
59
+
60
+    public void display() {
61
+        Set<String> keys = bookEntries.keySet();
62
+        for(String key: keys){
63
+            System.out.println(key + ": " + bookEntries.get(key).toString().replace("[", "").replace("]", ""));
64
+        }
65
+    }
66
+
67
+    public void main(String[] args) {
68
+        PhoneBook myBook = new PhoneBook();
69
+        myBook.add("Tomato", "267-945-5231");
70
+        myBook.add("Asparagus", "305-504-1159");
71
+        myBook.add("Eggplant", "123-345-6789");
72
+        myBook.add("Beet", "233-464-1289");
73
+        myBook.add("Asparagus", "454-345-4566");
74
+        myBook.add("Leeks", "194-345-3458");
75
+        myBook.add("Leeks", "345-209-1095");
76
+        myBook.display();
77
+
78
+        System.out.println("***");
79
+        myBook.remove("Asparagus", "305-504-1159");
80
+        myBook.removeRecord("Beet");
81
+        System.out.println("Phonebook after removing Asparagus' number and Beets' record");
82
+        myBook.display();
83
+
84
+        System.out.println("***");
85
+        System.out.println("Looking for Leeks' phone number");
86
+        System.out.println(myBook.lookup("Leeks"));
87
+
88
+        System.out.println("***");
89
+        System.out.println("Looking for the owner of number 454-345-4566");
90
+        System.out.println(myBook.reverseLookup("454-345-4566"));
91
+
92
+        System.out.println("***");
93
+        System.out.println("Looking for Banana's phone number");
94
+        System.out.println(myBook.lookup("Banana"));
95
+    }
7 96
 }

+ 84
- 23
PhoneBookTest.java View File

@@ -1,10 +1,14 @@
1 1
 
2
-
3
-import static org.junit.Assert.*;
2
+import org.junit.Assert;
4 3
 import org.junit.After;
5 4
 import org.junit.Before;
6 5
 import org.junit.Test;
7
-
6
+import java.util.Iterator;
7
+import java.util.Map;
8
+import java.util.Set;
9
+import java.util.SortedMap;
10
+import java.util.TreeMap; 
11
+import java.util.ArrayList;
8 12
 /**
9 13
  * The test class PhoneBookTest.
10 14
  *
@@ -13,30 +17,87 @@ import org.junit.Test;
13 17
  */
14 18
 public class PhoneBookTest
15 19
 {
16
-    /**
17
-     * Default constructor for test class PhoneBookTest
18
-     */
19
-    public PhoneBookTest()
20
-    {
21
-    }
22
-
23
-    /**
24
-     * Sets up the test fixture.
25
-     *
26
-     * Called before every test case method.
27
-     */
20
+    PhoneBook book;
28 21
     @Before
29 22
     public void setUp()
30 23
     {
24
+        this.book = new PhoneBook();
31 25
     }
32 26
 
33
-    /**
34
-     * Tears down the test fixture.
35
-     *
36
-     * Called after every test case method.
37
-     */
38
-    @After
39
-    public void tearDown()
40
-    {
27
+    @Test
28
+    public void addTest() {
29
+        //Given
30
+
31
+        //When
32
+        book.add("Leeks", "194-345-3458");
33
+        String lookup = book.lookup("Leeks");
34
+        //Then
35
+        Assert.assertEquals("194-345-3458", lookup);
36
+    }
37
+
38
+    @Test
39
+    public void lookupTest() {
40
+        //Given
41
+        book.add("Leeks", "194-345-3458");
42
+        book.add("Leeks", "345-234-4643");
43
+        String phoneNumber = book.lookup("Leeks");
44
+        //Then
45
+        Assert.assertEquals("194-345-3458, 345-234-4643", phoneNumber);
46
+    }
47
+
48
+    @Test
49
+    public void lookupTest1() {
50
+        //Given
51
+        book.add("Leeks", "194-345-3458");
52
+        book.add("Leeks", "345-234-4643");
53
+        String phoneNumber = book.lookup("Banana");
54
+        //Then
55
+        Assert.assertEquals("This entry doesn't exist. Please enter valid entry.", phoneNumber);
56
+    }
57
+
58
+    @Test
59
+    public void removeTest() {
60
+        //Given
61
+        book.add("Leeks", "194-345-3458");
62
+        book.add("Leeks", "345-234-4643");
63
+        book.remove("Leeks", "194-345-3458");
64
+        //When
65
+        String phoneNumber = book.lookup("Leeks");
66
+        //Then
67
+        Assert.assertEquals("345-234-4643", phoneNumber);
68
+    }
69
+
70
+    @Test
71
+    public void removeTest1() {
72
+        //Given
73
+        //When
74
+        book.add("Leeks", "194-345-3458");
75
+        book.add("Leeks", "345-234-4643");
76
+        book.remove("Leeks", "194-345-3458");
77
+        book.remove("Leeks", "345-234-4643");
78
+        //Then
79
+        Assert.assertEquals("This entry doesn't exist. Please enter valid entry.", book.lookup("Leeks"));
80
+    }
81
+
82
+    @Test
83
+    public void reverseLookupTest() {
84
+        //Given
85
+        book.add("Leeks", "194-345-3458");
86
+        //When
87
+        String name = book.reverseLookup("194-345-3458");
88
+        //Then
89
+        Assert.assertEquals("Leeks", name);
90
+    }
91
+
92
+    @Test
93
+    public void reverseLookupTest1() {
94
+        //Given
95
+        book.add("Leeks", "194-345-3458");
96
+        book.remove("Leeks", "194-345-3458");
97
+        //When
98
+        String name = book.reverseLookup("194-345-3458");
99
+        //Then
100
+        Assert.assertEquals("This entry doesn't exist. Please enter valid entry.", name);
41 101
     }
42 102
 }
103
+

+ 26
- 9
package.bluej View File

@@ -1,20 +1,23 @@
1 1
 #BlueJ package file
2
-editor.fx.0.height=0
3
-editor.fx.0.width=0
4
-editor.fx.0.x=0
5
-editor.fx.0.y=0
2
+dependency1.from=PhoneBookTest
3
+dependency1.to=PhoneBook
4
+dependency1.type=UsesDependency
5
+editor.fx.0.height=816
6
+editor.fx.0.width=854
7
+editor.fx.0.x=422
8
+editor.fx.0.y=31
6 9
 objectbench.height=101
7 10
 objectbench.width=461
8 11
 package.divider.horizontal=0.6
9 12
 package.divider.vertical=0.8007380073800738
10 13
 package.editor.height=427
11 14
 package.editor.width=674
12
-package.editor.x=427
13
-package.editor.y=143
15
+package.editor.x=23
16
+package.editor.y=89
14 17
 package.frame.height=600
15 18
 package.frame.width=800
16
-package.numDependencies=0
17
-package.numTargets=1
19
+package.numDependencies=1
20
+package.numTargets=2
18 21
 package.showExtends=true
19 22
 package.showUses=true
20 23
 project.charset=UTF-8
@@ -23,4 +26,18 @@ readme.name=@README
23 26
 readme.width=47
24 27
 readme.x=10
25 28
 readme.y=10
26
-
29
+target1.height=50
30
+target1.name=PhoneBookTest
31
+target1.showInterface=false
32
+target1.type=UnitTestTargetJunit4
33
+target1.width=100
34
+target1.x=100
35
+target1.y=40
36
+target2.association=PhoneBookTest
37
+target2.height=50
38
+target2.name=PhoneBook
39
+target2.showInterface=false
40
+target2.type=ClassTarget
41
+target2.width=100
42
+target2.x=70
43
+target2.y=70

+ 20
- 0
results.txt View File

@@ -0,0 +1,20 @@
1
+Asparagus: 305-504-1159, 454-345-4566
2
+Beet: 233-464-1289
3
+Eggplant: 123-345-6789
4
+Leeks: 194-345-3458, 345-209-1095
5
+Tomato: 267-945-5231
6
+***
7
+Phonebook after removing Asparagus' number and Beets' record
8
+Asparagus: 454-345-4566
9
+Eggplant: 123-345-6789
10
+Leeks: 194-345-3458, 345-209-1095
11
+Tomato: 267-945-5231
12
+***
13
+Looking for Leeks' phone number
14
+194-345-3458, 345-209-1095
15
+***
16
+Looking for the owner of number 454-345-4566
17
+Asparagus
18
+***
19
+Looking for Banana's phone number
20
+This entry doesn't exist. Please enter valid entry.