12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- //JN 2018
- import java.util.*;
- public class PhoneBook {
- private TreeMap<String, ArrayList<String>> phoneBook;
- public PhoneBook() {
- phoneBook = new TreeMap<String, ArrayList<String>>();
- }
-
- public void add(String name, String phoneNumber) {
- ArrayList<String> input = phoneBook.containsKey(name) ?
- phoneBook.get(name)
- : new ArrayList<String>();
- input.add(phoneNumber);
- phoneBook.put(name, input);
- }
-
- public void remove (String phoneNumber) {
- ArrayList<String> edit = new ArrayList<String>();
- Set<String> keys = phoneBook.keySet();
-
- for (String key : keys) {
- if (phoneBook.get(key).contains(phoneNumber)) {
- edit = phoneBook.get(key);
- edit.remove(edit.indexOf(phoneNumber));
- phoneBook.put(key, edit);
- }
- }
- }
-
- public void removeEntry(String name) {
- phoneBook.remove(name);
- }
-
- public String lookup(String name) {
- return phoneBook.containsKey(name) ? format(phoneBook.get(name)): "N/A";
- }
-
- public String format(ArrayList<String> numbers) {
- return numbers.toString().replaceAll("[\\[\\]]", "");
- }
-
- public String reverseLookup(String phoneNumber) {
- String output = "";
- Set<String> keys = phoneBook.keySet();
- for (String key : keys) {
- if (phoneBook.get(key).contains(phoneNumber)) {
- output = key;
- }
- }
- return output.equals("") ? "N/A" : output;
- }
-
- public String display() {
- return phoneBook.toString().replaceAll("[{}=,]", "")
- .replaceAll("\\[", " - ")
- .replaceAll("\\]", "\n");
-
- }
-
- public TreeMap getPhoneBook() {
- return phoneBook;
- }
- }
|