12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
-
- import java.util.*;
- /**
- * Created by leon on 1/23/18.
- */
- public class PhoneBook {
-
- Map<String, ArrayList<String> > entries = new HashMap<String, ArrayList<String>>();
-
- public void add (String name, String phoneNumber){
- ArrayList<String> pn = new ArrayList<String>();
- pn.add(phoneNumber);
- this.entries.put(name, pn);
- }
-
- public void addNumber(String name, String phoneNumber){
- for(Map.Entry<String, ArrayList<String>> entry: this.entries.entrySet()){
- if(name.equals(entry.getKey())){
- entry.getValue().add(phoneNumber);
- break;
- }
- }
- }
-
- public void remove(String name, String phoneNumber){
- }
-
- public void removeRecord(String name){
- this.entries.remove(name);
- }
-
-
-
- public void lookUp(String name){
- System.out.print(this.entries.get(name));
- }
-
- public String reverseLoopup(String phoneNumber){
- String name = "No Records...";
- for(Map.Entry<String, ArrayList<String>> entry: this.entries.entrySet()){
-
- for(String pn : entry.getValue()){
- if(pn.equals(phoneNumber)){
- name = entry.getKey();
- }
- }
-
- }
- return name;
-
- }
-
-
- public void display() {
- StringBuilder sb = new StringBuilder();
-
- for(Map.Entry<String, ArrayList<String>> entry :this.entries.entrySet()){
- sb.append(entry.getKey());
- sb.append(" - ");
- sb.append(entry.getValue());
- sb.append("\n");
- }
-
- System.out.print(sb.toString());
- }
-
-
-
- }
|