Build a simple PhoneBook program.

PhoneBook.java 1.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * Created by leon on 1/23/18.
  3. */
  4. import java.util.TreeMap;
  5. import java.util.Map;
  6. public class PhoneBook {
  7. String name;
  8. String phoneNumber;
  9. Map<String, String> entries = new TreeMap<String, String>();
  10. public void add(String name, String phoneNumber) {
  11. entries.put(name, phoneNumber);
  12. }
  13. public void remove(String name) {
  14. entries.remove(name);
  15. }
  16. public String lookUp(String name) {
  17. return entries.get(name);
  18. }
  19. public String reverseLookUp(String phoneNumber) {
  20. return entries.get(phoneNumber);
  21. }
  22. public void phoneNumberDisplay() {
  23. System.out.println(entries.values().toString());
  24. }
  25. public static void main(String[] args) {
  26. PhoneBook phonebook = new PhoneBook();
  27. phonebook.add("Jason", "302-444-6789");
  28. phonebook.add("Bill", "302-555-6543");
  29. phonebook.add("Johnny", "302-345-8901");
  30. phonebook.add("Andre", "302-231-4738");
  31. phonebook.add("Jerry", "302-565-5778");
  32. System.out.println(phonebook.lookUp("Jason"));
  33. phonebook.phoneNumberDisplay();
  34. }
  35. }