PhoneBook (Sorting) Background: You will be modifying your PhoneBook class to sort by different fields in a contact. Assignment: 1. Create a project called PhoneBookSort. Copy your PhoneBook class and Contact class from the PhoneBook project you completed earlier. 2. Add an additional constructor to the PhoneBook class. It should take a single parameter: a String which is the name of a file that holds saved contact information. In this method, you are to open the file and read in all of the contacts and added them to your ArrayList. Each contact in the file is on a single line. The contacts are in two forms: Name PhoneNumber Name PhoneNumber SpeedDial 3. You should add 3 public static final variables to the PhoneBook class: public static final int SORT_BY_NAME = 100; public static final int SORT_BY_PHONE_NUMBER = 200; public static final int SORT_BY_SPEED_DIAL = 300; 4. You will need to create 3 new classes that implement the Comparator interface for sorting Contacts: a. For SORT_BY_NAME, you will sort by the names in the phone book. b. For SORT_BY_PHONE_NUMBER, you will sort by the phone numbers in the phone book. c. Sorting for SORT_BY_SPEED_DIAL is more complicated. The first items in the sort should be the contacts with a speed dial, sorted by the speed dial number. Following the contacts with a speed dial should be the rest of the contacts sorted by name. 5. Modify the printphonebook method to accept one of the above 3 values. You should then change the printphonebook code to sort prior to outputting the data in the phone book. // // Loop through the ArrayList and prints the name and phone number for all // contacts. // If the phone book is empty, output // // The phone book is empty // public void printphonebook(int howtosort) { } a. For SORT_BY_NAME, use the comparator class you created to sort by name, and the output will look as it did before: // Name, Phone (SpeedDial) // or // Name, Phone b. For SORT_BY_PHONE_NUMBER, use the comparator class you created to sort by phone number. The output will change so that phone number comes first with a speed dial in parentheses if it exists: // Phone, Name (SpeedDial) // or // Phone, Name
c. For SORT_BY_SPEED_DIAL use the comparator class you created to sort by speed dial. The output changes as well: // SpeedDial Name, Phone // or // Name, Phone d. For any other value, throw an exception. What would be a good exception to throw? Driver: The PhoneBook.txt file should be downloaded and placed in the PhoneBookSort project before running the program. public class PhoneBookDriver { public static void main(string[] args) { PhoneBook pb = new PhoneBook("PhoneBook.txt"); System.out.println("SORT BY NAME"); pb.printphonebook(phonebook.sort_by_name); System.out.println("SORT BY PHONE NUMBER"); pb.printphonebook(phonebook.sort_by_phone_number); } } System.out.println("SORT BY SPEED DIAL"); pb.printphonebook(phonebook.sort_by_speed_dial); Output: SORT BY NAME Albert, 213-554-7612 Alex, 213-890-4589 Amy, 714-237-8945 Annabelle, 714-489-7812 Ashley, 213-890-9632 Avery, 678-890-4523 (9) Barbara, 450-891-5632 Billy, 480-690-1208 Bob, 718-890-5600 Brian, 562-480-5585 (2) Chad, 562-890-0045 Christine, 780-890-1236 Christy, 450-138-4567 Cindy, 480-602-7812 David, 714-890-1289 Debbie, 703-456-8974 Dennis, 562-480-1569 Diane, 714-892-7830 Donna, 562-745-5568 Edward, 562-879-1275 Elizabeth, 213-856-9885
Eric, 245-874-8912 Erick, 245-785-9612 Evelyn, 213-890-4562 Fawn, 609-213-8845 Frank, 213-890-7812 Gary, 213-785-8899 Gayle, 568-890-0056 Glenda, 562-781-8823 Hank, 714-890-7889 Helen, 213-562-7812 Henry, 412-450-8900 Irene, 562-412-3789 Isabel, 213-890-4562 Ivy-Joan, 450-780-0045 Jack, 562-562-1900 Jackie, 714-892-4566 Jackson, 213-489-7812 James, 450-456-7889 Jane, 714-562-7800 Janet, 213-781-7823 Jeanne, 457-891-5645 Jennifer, 213-785-4563 Jenny, 213-895-7813 Jenzi, 562-480-7787 (1) Jessica, 403-745-8002 Jim, 714-230-8023 Joan, 256-879-4569 Joe, 562-480-1243 (7) John, 323-567-4589 Jordan, 645-756-7812 Joseph, 323-678-1256 (8) Josh, 412-812-8896 Josie, 323-678-1290 Julia, 602-412-5784 (0) Katharine, 602-812-6478 Kathy, 602-412-5213 Kaye, 278-189-4588 Keith, 245-712-8002 Ken, 602-789-1256 (3) Kenny, 310-234-7890 Kimberly, 213-889-6530 Larry, 450-789-4899 LeeAnn, 450-789-1278 Leila, 213-745-7836 Leslie, 602-745-7123 Lily, 714-562-7845 Lisa, 213-745-1277 Lynn, 602-960-7845 (4) Margaret, 602-589-1275 Maria, 562-480-4512 Marilyn, 403-478-5837 Mark, 562-789-1890 Marshay, 323-545-8900 Martin, 562-745-1278 Melody, 213-278-5003 Mercedes, 410-222-5678 Michael, 213-555-1289 Nancy, 245-875-8127 Nathan, 323-555-3456
Nicholas, 310-345-8923 Pamela, 562-480-7158 Patrick, 480-478-7879 Patty, 562-781-2323 Paul, 714-879-1278 Peter, 714-820-7312 (5) Richard, 562-478-8900 Robert, 480-562-7812 Rodney, 310-555-6897 Ronald, 562-741-1890 Sandra, 478-822-7349 Scott, 578-820-7319 Sharon, 562-712-7304 Sheryl, 450-712-8173 Stan, 609-719-5736 Steve, 703-731-8570 Susan, 639-450-7318 Ted, 639-457-5873 Thomas, 714-562-2346 Tim, 703-480-2971 Tony, 562-718-9170 Victor, 563-717-1597 (6) William, 563-735-8104 Zachary, 274-847-9610 SORT BY PHONE NUMBER 213-278-5003, Melody 213-489-7812, Jackson 213-554-7612, Albert 213-555-1289, Michael 213-562-7812, Helen 213-745-1277, Lisa 213-745-7836, Leila 213-781-7823, Janet 213-785-4563, Jennifer 213-785-8899, Gary 213-856-9885, Elizabeth 213-889-6530, Kimberly 213-890-4562, Evelyn 213-890-4562, Isabel 213-890-4589, Alex 213-890-7812, Frank 213-890-9632, Ashley 213-895-7813, Jenny 245-712-8002, Keith 245-785-9612, Erick 245-874-8912, Eric 245-875-8127, Nancy 256-879-4569, Joan 274-847-9610, Zachary 278-189-4588, Kaye 310-234-7890, Kenny 310-345-8923, Nicholas 310-555-6897, Rodney 323-545-8900, Marshay 323-555-3456, Nathan 323-567-4589, John 323-678-1256, Joseph (8)
323-678-1290, Josie 403-478-5837, Marilyn 403-745-8002, Jessica 410-222-5678, Mercedes 412-450-8900, Henry 412-812-8896, Josh 450-138-4567, Christy 450-456-7889, James 450-712-8173, Sheryl 450-780-0045, Ivy-Joan 450-789-1278, LeeAnn 450-789-4899, Larry 450-891-5632, Barbara 457-891-5645, Jeanne 478-822-7349, Sandra 480-478-7879, Patrick 480-562-7812, Robert 480-602-7812, Cindy 480-690-1208, Billy 562-412-3789, Irene 562-478-8900, Richard 562-480-1243, Joe (7) 562-480-1569, Dennis 562-480-4512, Maria 562-480-5585, Brian (2) 562-480-7158, Pamela 562-480-7787, Jenzi (1) 562-562-1900, Jack 562-712-7304, Sharon 562-718-9170, Tony 562-741-1890, Ronald 562-745-1278, Martin 562-745-5568, Donna 562-781-2323, Patty 562-781-8823, Glenda 562-789-1890, Mark 562-879-1275, Edward 562-890-0045, Chad 563-717-1597, Victor (6) 563-735-8104, William 568-890-0056, Gayle 578-820-7319, Scott 602-412-5213, Kathy 602-412-5784, Julia (0) 602-589-1275, Margaret 602-745-7123, Leslie 602-789-1256, Ken (3) 602-812-6478, Katharine 602-960-7845, Lynn (4) 609-213-8845, Fawn 609-719-5736, Stan 639-450-7318, Susan 639-457-5873, Ted 645-756-7812, Jordan 678-890-4523, Avery (9) 703-456-8974, Debbie 703-480-2971, Tim 703-731-8570, Steve 714-230-8023, Jim
714-237-8945, Amy 714-489-7812, Annabelle 714-562-2346, Thomas 714-562-7800, Jane 714-562-7845, Lily 714-820-7312, Peter (5) 714-879-1278, Paul 714-890-1289, David 714-890-7889, Hank 714-892-4566, Jackie 714-892-7830, Diane 718-890-5600, Bob 780-890-1236, Christine SORT BY SPEED DIAL 0 - Julia, 602-412-5784 1 - Jenzi, 562-480-7787 2 - Brian, 562-480-5585 3 - Ken, 602-789-1256 4 - Lynn, 602-960-7845 5 - Peter, 714-820-7312 6 - Victor, 563-717-1597 7 - Joe, 562-480-1243 8 - Joseph, 323-678-1256 9 - Avery, 678-890-4523 Albert, 213-554-7612 Alex, 213-890-4589 Amy, 714-237-8945 Annabelle, 714-489-7812 Ashley, 213-890-9632 Barbara, 450-891-5632 Billy, 480-690-1208 Bob, 718-890-5600 Chad, 562-890-0045 Christine, 780-890-1236 Christy, 450-138-4567 Cindy, 480-602-7812 David, 714-890-1289 Debbie, 703-456-8974 Dennis, 562-480-1569 Diane, 714-892-7830 Donna, 562-745-5568 Edward, 562-879-1275 Elizabeth, 213-856-9885 Eric, 245-874-8912 Erick, 245-785-9612 Evelyn, 213-890-4562 Fawn, 609-213-8845 Frank, 213-890-7812 Gary, 213-785-8899 Gayle, 568-890-0056 Glenda, 562-781-8823 Hank, 714-890-7889 Helen, 213-562-7812 Henry, 412-450-8900 Irene, 562-412-3789 Isabel, 213-890-4562 Ivy-Joan, 450-780-0045
Jack, 562-562-1900 Jackie, 714-892-4566 Jackson, 213-489-7812 James, 450-456-7889 Jane, 714-562-7800 Janet, 213-781-7823 Jeanne, 457-891-5645 Jennifer, 213-785-4563 Jenny, 213-895-7813 Jessica, 403-745-8002 Jim, 714-230-8023 Joan, 256-879-4569 John, 323-567-4589 Jordan, 645-756-7812 Josh, 412-812-8896 Josie, 323-678-1290 Katharine, 602-812-6478 Kathy, 602-412-5213 Kaye, 278-189-4588 Keith, 245-712-8002 Kenny, 310-234-7890 Kimberly, 213-889-6530 Larry, 450-789-4899 LeeAnn, 450-789-1278 Leila, 213-745-7836 Leslie, 602-745-7123 Lily, 714-562-7845 Lisa, 213-745-1277 Margaret, 602-589-1275 Maria, 562-480-4512 Marilyn, 403-478-5837 Mark, 562-789-1890 Marshay, 323-545-8900 Martin, 562-745-1278 Melody, 213-278-5003 Mercedes, 410-222-5678 Michael, 213-555-1289 Nancy, 245-875-8127 Nathan, 323-555-3456 Nicholas, 310-345-8923 Pamela, 562-480-7158 Patrick, 480-478-7879 Patty, 562-781-2323 Paul, 714-879-1278 Richard, 562-478-8900 Robert, 480-562-7812 Rodney, 310-555-6897 Ronald, 562-741-1890 Sandra, 478-822-7349 Scott, 578-820-7319 Sharon, 562-712-7304 Sheryl, 450-712-8173 Stan, 609-719-5736 Steve, 703-731-8570 Susan, 639-450-7318 Ted, 639-457-5873 Thomas, 714-562-2346 Tim, 703-480-2971 Tony, 562-718-9170
William, 563-735-8104 Zachary, 274-847-9610