Build a simple PhoneBook program.

PhoneBook.java 951B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import java.util.*;
  2. /**
  3. * Created by leon on 1/23/18.
  4. */
  5. public class PhoneBook {
  6. private String phoneNumber;
  7. private String name;
  8. private Map<String,String> input;
  9. public PhoneBook(){
  10. input = new TreeMap<String,String>();
  11. }
  12. public void display(){
  13. for(Object keyValue:input.keySet()){
  14. System.out.println("Key: " + keyValue + ". Value: " + input.get(keyValue));
  15. }
  16. }
  17. public String remove(String name){
  18. return input.remove(name);
  19. }
  20. public void AddInput(String name,String phoneNumber){
  21. input.put(name,phoneNumber);
  22. }
  23. public String lookup(String name){
  24. String value = input.get(name);
  25. return value;
  26. }
  27. public String reverseLookup(String name,String phoneNumber){
  28. String value = "";
  29. if(input.containsValue(phoneNumber)){
  30. value = phoneNumber;
  31. }
  32. return value;
  33. }
  34. }