1234567891011121314151617181920212223242526272829303132333435363738394041424344454647 |
-
-
- /**
- * Created by leon on 1/23/18.
- */
- import java.util. *;
- public class PhoneBook {
-
- TreeMap<String, String> phoneBook = new TreeMap<String, String>();
-
- public String add(String name, String number) {
- phoneBook.put(name, number);
- return name + " " + number;
- }
-
- public String remove(String name) {
- phoneBook.remove(name);
- return name;
-
- }
-
- public String lookup(String name) {
- return phoneBook.get(name);
-
-
- }
-
- public String reverseLookup(String number) {
- for(Map.Entry<String, String> entry : phoneBook.entrySet()) {
- String value = entry.getValue();
- if(value.equals(number)) {
- return entry.getKey();
- }
- }
- return "";
- }
-
- public void display() {
- for(Map.Entry<String, String> entry : phoneBook.entrySet()) {
- String value = entry.getValue();
- System.out.println(entry.getKey() + " -> " + entry.getValue());
- }
- }
-
-
- }
|