Build a simple PhoneBook program.

PhoneBook.java 762B

123456789101112131415161718192021222324252627282930313233343536373839
  1. import java.util.*;
  2. /**
  3. * Created by leon on 1/23/18.
  4. */
  5. public class PhoneBook {
  6. private String name, phoneNumber;
  7. private Map<String, String> entries;
  8. public PhoneBook(){
  9. this.entries = new TreeMap<String, String> ();
  10. }
  11. public void add(String name, String phoneNumber){
  12. this.entries.put(name, phoneNumber);
  13. }
  14. public void remove(String name){
  15. this.entries.remove(name);
  16. }
  17. public void display(){
  18. if (this.entries.isEmpty()) System.out.println("Empty");
  19. for (Map.Entry<String,String> entry : entries.entrySet()){
  20. String key = entry.getKey();
  21. String value = entry.getValue();
  22. System.out.println(key + " " + value);
  23. }
  24. }
  25. }