123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
-
- 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 addEntry(String name, String phoneNumber){
- ArrayList<String> pn = new ArrayList<String>();
- pn.add(phoneNumber);
- this.entries.put(name, pn);
- }
-
- public void addNumberTo(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 removePhoneNumber(String name, String phoneNumber){
- for(Map.Entry<String, ArrayList<String>> entry: this.entries.entrySet()){
- if(name.equals(entry.getKey())){
- int indexOf = entry.getValue().indexOf(phoneNumber);
- if(indexOf >= 0){
- entry.getValue().remove(indexOf);
- break;
- }
- }
- }
- }
-
- public void removeRecord(String name){
- this.entries.remove(name);
- }
-
- public String lookUp(String name){
- StringBuilder sb = new StringBuilder();
- for(Map.Entry<String, ArrayList<String>> entry: this.entries.entrySet()){
- if(entry.getKey().equals(name)){
- for(String pn : entry.getValue()){
- sb.append(pn);
- }
- }
- }
- return sb.toString();
-
- }
-
- public String reverseLoopup(String phoneNumber){
- String name = null;
- 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 String display() {
- StringBuilder sb = new StringBuilder();
-
- for(Map.Entry<String, ArrayList<String>> entry :this.entries.entrySet()){
- sb.append(entry.getKey());
- sb.append(": ");
-
- for(String pn : entry.getValue()){
- sb.append(pn);
- //sb.append(" ");
- }
-
- }
-
- return sb.toString();
- }
-
-
-
- }
|