123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- import java.util.HashMap;
- import java.util.Map;
- import java.util.TreeMap;
-
- public class PhoneBook {
-
- Map<String, String> phoneBook = new HashMap<String, String>();
-
- public String add(String name, String number) {
-
- phoneBook.put(name, number);
-
- return (name + " " + phoneBook.get(name));
-
- // phoneBook.put("Albert", "302-123-4567");
- // phoneBook.put("Bobby", "302-111-1111");
- // phoneBook.put("Clyde", "302-222-2222");
- // phoneBook.put("Drew", "302-333-3333");
- //
- // System.out.println(phoneBook.size());
- //
- // System.out.println(phoneBook.get("Clyde" + "\n"));
- //
- // for(Map.Entry<String, String> list : phoneBook.entrySet()){
- //
- // String key = list.getKey();
- // String value = list.getValue();
- // System.out.println(key + " " + value);
- //
- // }
-
- }
-
- public String remove(String name) {
-
- phoneBook.remove(name);
-
- return name;
- }
-
- public String lookUp(String name) {
- phoneBook.get(name);
- return phoneBook.get(name);
- }
-
- public String reverseLookUp(String number) {
- String result = "";
- //need to import Entry class
- for (Map.Entry<String, String> entry : phoneBook.entrySet()) {
- if (entry.getValue().equals(number)) {
- result = (entry.getKey());
- }
- }
-
- return result;
- }
-
- public String displayPhoneBook(){
-
- String result = "";
- //this converts a Hash Map into a sorted Tree Map
- TreeMap<String, String> treePhoneBook = new TreeMap<String, String>(phoneBook);
-
- for (Map.Entry<String, String> entry : treePhoneBook.entrySet()){
-
- //must append onto the string
- result += (entry.getKey() + " " + entry.getValue() + "\n");
-
- }
-
- return result;
- }
-
- }
-
-
-
|