Programming Resources
For Fun and Learning
Charles Cusack
Computer Science
Hope College
main

Python
C++

JAVA


PHP
SQL
Assignments

CSCI125Code


BetterPhoneBook.java

import java.util.HashMap;
import java.util.Set;
import java.util.Map;

/**
 *
 */
public class BetterPhoneBook
{
    /* 
     * A map from names to collections of numbers
     * Each name is mapped to a HashMap of contact type (e.g. "cell" or "home")
     * to the phone number.
     */
   private HashMap<String,HashMap<String,String>> contactMap;
   
   public BetterPhoneBook() {
       contactMap = new HashMap<>();
   }
   
   public int phonebookSize() {
       return contactMap.size();
   }
   
   /**
    * Given a name and a contact type (e.g. "cell"), return the phone number
    * @param name the contact name
    * @param type the type of the number (e.g. "cell", "home", etc.)
    * @return the number associated the name and type, or null if either the
    * name is not present or the contact type for that name is not present.
    */
   public String getNumber(String name,String type) {
       HashMap<String,String> numbers = contactMap.get(name);
       if(numbers!=null) {
           return numbers.get(type);
       } else {
            return null;    
       }
   }
   
   /**
    * Add the number number of type type for person name
    * @param name the person whose number we want to add
    * @param type the contact type (e.g. "Cell" or "Home")
    * @param number the actual phone number
    */
   public void addNumber(String name, String type, String number) {
       HashMap<String,String> numbers = contactMap.get(name);
       if(numbers==null) {
           numbers = new HashMap<>();
           contactMap.put(name,numbers);
       }
       numbers.put(type,number);
   }
   
   /**
    * Print all of the numbers associated with the person name.
    * @param name the person whose numbers we want
    */
   public void printNumbers(String name) {
       HashMap<String,String> numbers = contactMap.get(name);
       if(numbers==null) {
           System.out.println(name+" is not in the phone book"); 
        } else {
            if(numbers.size()==0) {
               System.out.println(name+" has no numbers listed");
            } else {
                
            }
            System.out.println("Numbers associated with "+name);
            Set<Map.Entry<String,String>> entries = numbers.entrySet();
            for(Map.Entry<String,String> entry : entries) {
                System.out.println("  "+entry.getKey()+": "+entry.getValue());
            }
        }
    }
        
    /**
     * Print a list of all contacts and the number of different phone numbers for each.
     */
    public void printNumberOfNumbers() {
        System.out.println("List of contacts with number of phone numbers for each");
        Set<String> names = contactMap.keySet();
        for(String name : names) {
            HashMap<String,String> numbers = contactMap.get(name);
            System.out.println(name+": "+numbers.size());
        }
    }
    
        /**
     * Print a list of all contacts and the number of different phone numbers for each.
     */
    public void printNumberOfNumbersV2() {
        System.out.println("List of contacts with number of phone numbers for each");
        Set<Map.Entry<String,HashMap<String,String>>> entries = contactMap.entrySet();
        for(Map.Entry<String,HashMap<String,String>> entry  : entries) {
            System.out.println(entry.getKey()+": "+entry.getValue().size());
        }
 
    }
}