123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- import java.util.*;
-
- /**
- * Created by leon on 1/23/18.
- */
- public class PhoneBook {
- String name;
- String phoneNumber;
-
- // create treemap(hashmap), has key String, value arrayList of Strings
- TreeMap<String, ArrayList<String>> phoneBook = new TreeMap<>();
-
- // construct an instance of phoneBook
- public PhoneBook(TreeMap<String, ArrayList<String>> phoneBook) {
- this.phoneBook = phoneBook;
- }
-
- // default constructor
- public PhoneBook(){}
-
- public void add(String name, String phoneNumber){
- // if phonebook does NOT contain key(name), put name on list
- if (!phoneBook.containsKey(name)) {
- ArrayList<String> list = new ArrayList<>();
- list.add(phoneNumber);
- phoneBook.put(name, list);
- // or else, create tempoary arraylist and add phonenumber
- } else {
- ArrayList<String> temp = phoneBook.get(name);
- temp.add(phoneNumber);
-
- //(key,old key,new key)
- phoneBook.replace(name, phoneBook.get(name), temp);
- }
- }
-
- public void remove(String name, String phoneNumber){
-
- ArrayList<String> list = phoneBook.get(name);
- // if only one number, remove name
- if(list.size() == 1) {
- phoneBook.remove(name);
-
- // or else create temp arraylist and remove phonenumber/replace
- // in list with parameters (name, list, temp)
- } else {
- ArrayList<String> temp = phoneBook.get(name);
- temp.remove(phoneNumber);
- phoneBook.replace(name,list,temp);
- }
- }
-
- public void removeRecord(String name) {
- phoneBook.remove(name);
- }
-
- // lookup returns list of phonenumbers for given name
- public ArrayList<String> lookup(String name){
- return phoneBook.get(name);
- }
-
-
- public String reverseLookup(String phoneNumber) {
- // for each entry in phonebook, lookup array of name/number
- for (Map.Entry<String, ArrayList<String>> entry : phoneBook.entrySet()) {
- // gets name
- String name = entry.getKey();
- // gets list of numbers
- ArrayList<String> list = entry.getValue();
- // if number equals the number we are looking for, return name
- for (String number: list) {
- if (number.equals(phoneNumber)) {
- return name;
- }
- }
- }
- return "Not found";
- }
-
- public String display() {
- // build a string to display
- StringBuilder sb = new StringBuilder();
-
- // For (string, phone number) entry, iterate thru phoneBook.entrySet
- for (Map.Entry<String, ArrayList<String>> entry : phoneBook.entrySet()){
-
- String name = entry.getKey();
- ArrayList<String> numbers = entry.getValue();
-
- // when key got, add name to space " "
- sb.append(name).append(" ");
-
- // for each number in element, append to name
- for (int i = 0; i < numbers.size(); i++) {
- sb.append(numbers.get(i));
-
- // append a space if i is not the last number in list
- if (i != numbers.size()-1) {
- sb.append(" ");
- }
- }
- sb.append("\n");
- }
- return sb.toString();
- }
- }
|