Build a simple PhoneBook program.

PhoneBook1.java 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * Write a description of class PhoneBook1 here.
  3. *
  4. * @author (your name)
  5. * @version (a version number or a date)
  6. */
  7. import java.util.TreeMap;
  8. import java.util.*;
  9. import java.util.Scanner;
  10. public class PhoneBook1
  11. {
  12. // instance variables - replace the example below with your own
  13. private String name, number;
  14. private Map<String, String> entries ;
  15. /**
  16. * Constructor for objects of class PhoneBook1
  17. */
  18. public PhoneBook1(){
  19. this.entries = new TreeMap<String, String>();
  20. }
  21. /**
  22. * An example of a method - replace this comment with your own
  23. *
  24. * @param y a sample parameter for a method
  25. * @return the sum of x and y
  26. */
  27. public void addNumber(String name, String number){
  28. this.entries.put(name, number);
  29. }
  30. public void remove(String name){
  31. if (this.entries.containsKey(name))
  32. System.out.println("found it");
  33. this.entries.remove(name);
  34. }
  35. public void display(){
  36. if (this.entries.isEmpty()) System.out.println("Empty");
  37. for (Map.Entry<String, String> entry : entries.entrySet()) {
  38. String value = entry.getValue();
  39. String key = entry.getKey();
  40. System.out.println(key + " " + value);
  41. }
  42. }
  43. }