Advertisement
mmayoub

collections, Phone Book class

Aug 17th, 2017
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.51 KB | None | 0 0
  1. PhoneBook.java
  2. ========================================================================
  3.  
  4. package PhonePkg;
  5.  
  6. import java.util.Map;
  7. import java.util.Set;
  8. import java.util.TreeMap;
  9. import java.util.TreeSet;
  10.  
  11. public class PhoneBook {
  12.     protected TreeMap<Character, TreeMap<String, Set<String>>> thePhoneBook;
  13.  
  14.     // get the first letter in the name in upper case
  15.     // if no letters in the name returns *
  16.     private char firstLetter(String name) {
  17.         for (int i = 0; i < name.length(); i += 1) {
  18.             char ch = name.charAt(i);
  19.  
  20.             if (ch >= 'a' && ch <= 'z') {
  21.                 return Character.toUpperCase(ch);
  22.             } else if (ch >= 'A' && ch <= 'Z') {
  23.                 return ch;
  24.             }
  25.         }
  26.  
  27.         // no letters in the name
  28.         return '*';
  29.     }
  30.  
  31.     // format the name as a key
  32.     // remove leading and trailing spaces and convert to lower case
  33.     private String keyFormat(String name) {
  34.         return name.trim().toLowerCase();
  35.     }
  36.  
  37.     // build an empty phone book
  38.     public PhoneBook() {
  39.         this.thePhoneBook = new TreeMap<Character, TreeMap<String, Set<String>>>();
  40.  
  41.         // every letter has no records
  42.         for (char letter = 'A'; letter <= 'Z'; letter += 1) {
  43.             thePhoneBook.put(letter, new TreeMap<String, Set<String>>());
  44.         }
  45.  
  46.         // for names with no letters
  47.         thePhoneBook.put('*', new TreeMap<String, Set<String>>());
  48.     }
  49.  
  50.     // add a phone number to a person
  51.     public boolean addPhone(String name, String phone) {
  52.         name = keyFormat(name);
  53.  
  54.         // get all records starting with the same letter
  55.         TreeMap<String, Set<String>> letterRecords = thePhoneBook
  56.                 .get(firstLetter(name));
  57.  
  58.         // if name is not exist as a key
  59.         if (!letterRecords.containsKey(name)) {
  60.             // add new name (key) with no phone numbers
  61.             letterRecords.put(name, new TreeSet<String>());
  62.         }
  63.  
  64.         // add the number to person phones set
  65.         return letterRecords.get(name).add(phone);
  66.     }
  67.  
  68.     // remove a number of person
  69.     public boolean removePhone(String name, String phone) {
  70.         name = keyFormat(name);
  71.  
  72.         // get all records starting with the same letter
  73.         TreeMap<String, Set<String>> letterRecords = thePhoneBook
  74.                 .get(firstLetter(name));
  75.  
  76.         // if no such name (key)
  77.         if (!letterRecords.containsKey(name)) {
  78.             // return true
  79.             return true;
  80.         }
  81.  
  82.         // remove number from person numbers
  83.         letterRecords.get(name).remove(phone);
  84.  
  85.         // if person is without numbers
  86.         if (letterRecords.get(name).isEmpty()) {
  87.             // remove it and return true
  88.             letterRecords.remove(name);
  89.             return true;
  90.         }
  91.  
  92.         // return true if person or phone does not exist
  93.         return !letterRecords.get(name).contains(phone);
  94.     }
  95.  
  96.     // return true is person exists (also has numbers)
  97.     public boolean isExist(String name) {
  98.         name = keyFormat(name);
  99.  
  100.         TreeMap<String, Set<String>> letterRecords = thePhoneBook
  101.                 .get(firstLetter(name));
  102.  
  103.         return letterRecords.containsKey(name);
  104.     }
  105.  
  106.     // return the phone numbers of an person
  107.     // if not exist returns null
  108.     public Set<String> getPhonesOf(String name) {
  109.         name = keyFormat(name);
  110.  
  111.         TreeMap<String, Set<String>> letterRecords = thePhoneBook
  112.                 .get(firstLetter(name));
  113.  
  114.         return new TreeSet<String>(letterRecords.get(name));
  115.     }
  116.  
  117.     @Override
  118.     public String toString() {
  119.         String res = "";
  120.  
  121.         for (Map.Entry<Character, TreeMap<String, Set<String>>> entry : thePhoneBook
  122.                 .entrySet()) {
  123.             if (!entry.getValue().isEmpty()) {
  124.                 res += "\n" + entry.getKey() + "\n";
  125.  
  126.                 for (Map.Entry<String, Set<String>> itr : entry.getValue()
  127.                         .entrySet()) {
  128.                     res += itr.getKey() + " --> " + itr.getValue() + "\n";
  129.                 }
  130.  
  131.             }
  132.         }
  133.  
  134.         return res;
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement