Browse Source

got alot done still need to finish the Many to one Relationship

Nicholas Maidanos 6 years ago
parent
commit
f246546a64
1 changed files with 63 additions and 1 deletions
  1. 63
    1
      PhoneBook.java

+ 63
- 1
PhoneBook.java View File

@@ -1,7 +1,69 @@
1 1
  
2
-
2
+import java.util.*;
3 3
 /**
4 4
  * Created by leon on 1/23/18.
5 5
  */
6 6
 public class PhoneBook {
7
+    
8
+    Map<String, ArrayList<String> > entries = new HashMap<String, ArrayList<String>>();
9
+    
10
+    public void add (String name, String phoneNumber){
11
+        ArrayList<String> pn = new ArrayList<String>();
12
+        pn.add(phoneNumber);
13
+        this.entries.put(name, pn);
14
+    }
15
+    
16
+    public void addNumber(String name, String phoneNumber){
17
+        for(Map.Entry<String, ArrayList<String>> entry: this.entries.entrySet()){
18
+            if(name.equals(entry.getKey())){
19
+                entry.getValue().add(phoneNumber);
20
+                break;
21
+            }
22
+        }    
23
+    }
24
+    
25
+    public void remove(String name, String phoneNumber){
26
+    }
27
+    
28
+    public void removeRecord(String name){
29
+        this.entries.remove(name);
30
+    }
31
+    
32
+    
33
+    
34
+    public void lookUp(String name){
35
+        System.out.print(this.entries.get(name));
36
+    }
37
+    
38
+    public String reverseLoopup(String phoneNumber){
39
+        String name = "No Records...";
40
+        for(Map.Entry<String, ArrayList<String>> entry: this.entries.entrySet()){
41
+            
42
+            for(String pn : entry.getValue()){
43
+                if(pn.equals(phoneNumber)){
44
+                    name = entry.getKey();
45
+                }
46
+            }
47
+            
48
+        }
49
+        return name;
50
+        
51
+    }
52
+    
53
+    
54
+    public void display() {
55
+        StringBuilder sb = new StringBuilder();
56
+        
57
+        for(Map.Entry<String, ArrayList<String>> entry :this.entries.entrySet()){
58
+            sb.append(entry.getKey());
59
+            sb.append(" - ");
60
+            sb.append(entry.getValue());
61
+            sb.append("\n");
62
+        }
63
+        
64
+        System.out.print(sb.toString());
65
+    }
66
+   
67
+    
68
+    
7 69
 }