Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PhoneBook.java
- ========================================================================
- package PhonePkg;
- import java.util.Map;
- import java.util.Set;
- import java.util.TreeMap;
- import java.util.TreeSet;
- public class PhoneBook {
- protected TreeMap<Character, TreeMap<String, Set<String>>> thePhoneBook;
- // get the first letter in the name in upper case
- // if no letters in the name returns *
- private char firstLetter(String name) {
- for (int i = 0; i < name.length(); i += 1) {
- char ch = name.charAt(i);
- if (ch >= 'a' && ch <= 'z') {
- return Character.toUpperCase(ch);
- } else if (ch >= 'A' && ch <= 'Z') {
- return ch;
- }
- }
- // no letters in the name
- return '*';
- }
- // format the name as a key
- // remove leading and trailing spaces and convert to lower case
- private String keyFormat(String name) {
- return name.trim().toLowerCase();
- }
- // build an empty phone book
- public PhoneBook() {
- this.thePhoneBook = new TreeMap<Character, TreeMap<String, Set<String>>>();
- // every letter has no records
- for (char letter = 'A'; letter <= 'Z'; letter += 1) {
- thePhoneBook.put(letter, new TreeMap<String, Set<String>>());
- }
- // for names with no letters
- thePhoneBook.put('*', new TreeMap<String, Set<String>>());
- }
- // add a phone number to a person
- public boolean addPhone(String name, String phone) {
- name = keyFormat(name);
- // get all records starting with the same letter
- TreeMap<String, Set<String>> letterRecords = thePhoneBook
- .get(firstLetter(name));
- // if name is not exist as a key
- if (!letterRecords.containsKey(name)) {
- // add new name (key) with no phone numbers
- letterRecords.put(name, new TreeSet<String>());
- }
- // add the number to person phones set
- return letterRecords.get(name).add(phone);
- }
- // remove a number of person
- public boolean removePhone(String name, String phone) {
- name = keyFormat(name);
- // get all records starting with the same letter
- TreeMap<String, Set<String>> letterRecords = thePhoneBook
- .get(firstLetter(name));
- // if no such name (key)
- if (!letterRecords.containsKey(name)) {
- // return true
- return true;
- }
- // remove number from person numbers
- letterRecords.get(name).remove(phone);
- // if person is without numbers
- if (letterRecords.get(name).isEmpty()) {
- // remove it and return true
- letterRecords.remove(name);
- return true;
- }
- // return true if person or phone does not exist
- return !letterRecords.get(name).contains(phone);
- }
- // return true is person exists (also has numbers)
- public boolean isExist(String name) {
- name = keyFormat(name);
- TreeMap<String, Set<String>> letterRecords = thePhoneBook
- .get(firstLetter(name));
- return letterRecords.containsKey(name);
- }
- // return the phone numbers of an person
- // if not exist returns null
- public Set<String> getPhonesOf(String name) {
- name = keyFormat(name);
- TreeMap<String, Set<String>> letterRecords = thePhoneBook
- .get(firstLetter(name));
- return new TreeSet<String>(letterRecords.get(name));
- }
- @Override
- public String toString() {
- String res = "";
- for (Map.Entry<Character, TreeMap<String, Set<String>>> entry : thePhoneBook
- .entrySet()) {
- if (!entry.getValue().isEmpty()) {
- res += "\n" + entry.getKey() + "\n";
- for (Map.Entry<String, Set<String>> itr : entry.getValue()
- .entrySet()) {
- res += itr.getKey() + " --> " + itr.getValue() + "\n";
- }
- }
- }
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement