Simple Phonebook

PhoneBook.java 892B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import java.util.TreeMap;
  2. import java.util.Scanner;
  3. import java.util.*;
  4. /**
  5. * Created by leon on 1/23/18.
  6. */
  7. public class PhoneBook {
  8. // private String name;
  9. // private String phoneNumber;
  10. private Map<String, String> phoneBook;
  11. public PhoneBook() {
  12. // Create a TreeMap
  13. phoneBook = new TreeMap<>();
  14. }
  15. public void addListings(String name, String phoneNumber) {
  16. phoneBook.put(name, phoneNumber);
  17. }
  18. public int size() {
  19. return phoneBook.size();
  20. }
  21. public void removeListings(String name, String phoneNumber) {
  22. phoneBook.remove(name, phoneNumber);
  23. }
  24. public void lookupByKey(String name, String phoneNumber) {
  25. System.out.println(phoneBook.get(name));
  26. }
  27. public void lookupByValue(String name, String phoneNumber) {
  28. System.out.println(phoneBook.get(phoneNumber));
  29. }
  30. public void display() {
  31. System.out.println(phoneBook);
  32. }
  33. }