1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- import java.util.*;
-
- /**
- * Created by leon on 1/23/18.
- */
- public class PhoneBook {
- String name;
- String phoneNumber;
- TreeMap<String, String> phoneBook = new TreeMap<>();
-
- public PhoneBook(TreeMap<String, String> phoneBook) {
- this.phoneBook = phoneBook;
- }
-
- public PhoneBook(){}
-
- public void add(String name, String phoneNumber){
- phoneBook.put(name, phoneNumber);
- }
-
- public void remove(String name){
- phoneBook.remove(name);
- }
-
- public String lookup(String name){
- return phoneBook.get(name);
- }
-
- public String reverseLookup(String phoneNumber) {
- for (Map.Entry<String, String> e : phoneBook.entrySet()) {
- String name = e.getKey();
- String thisNumber = e.getValue();
-
- if (thisNumber == phoneNumber) {
- return name;
- }
- }
- return "Name not found";
- }
-
- public String display() {
- StringBuilder sb = new StringBuilder();
-
- for (Map.Entry<String, String> e : phoneBook.entrySet()){
- String name = e.getKey();
- String thisNumber = e.getValue();
-
- sb.append(name).append(" ").append(thisNumber).append("\n");
- }
- return sb.toString();
- }
- }
|