Write an application that will have using the data structure
Write an application that will have; using the data structure of your choice, at least 5 Contact objects that will be sorted using Comparators. These are the following requirements:
- A prompt for the sort by options (with 0 being exit):
Sort by lastname :[1]
Sort by home state :[2]
Sort by age :[3]
Enter option or 0 to end input: 0
Exiting...
- 1= Sort by last name
- 2 = Sort by Home State
- 3 = Sort by Age
- Also regonize an invalid entry
Sort by lastname :[1]
Sort by home state :[2]
Sort by age :[3]
Enter option or 0 to end input: 4
Invalid Entry
/*
 * The Contact class will have 3 properties:
 *        firstname - String
 *        lastname - String
 *        homestate - String
 *        age - Integer
 */
public class Contact {
}
import java.util.ArrayList;
public class TestSortOptions {
   public static void main(String[] args) {
        ArrayList<Contact> contacts = initializeContactsArray();
 promptForOption(contacts);
    }
/*
 * Data Initialization
 */
   
 private static ArrayList<Contact> initializeContactsArray() {
 // TODO: Initialize an array of Student objects
 return null;
 }
   
 /*
 * Prompt for the user to enter their option from the keyboard
 *
 * 1 = Sort by last name
 * 2 = Sort by Home State
 * 3 = Sort by Age
 * 0 = End input and exit/terminate the application
 *
 */
   
 private static void promptForOption(ArrayList<Contact> contacts) {
 // TODO: Prompt and accept option input
 }
/*
 * Display the Contact information sorted by using the selected option from
 * the above \"promptForOption\" method result
 */
   
 private static void displayContacts(ArrayList<Contact> contacts) {
 // TODO: Display the contents of the Contacts Array
 }
}
Solution
 //TestSortOptions.java
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Scanner;
 public class TestSortOptions {
   public static void main(String[] args)
    {
       
        //Create an instance of ArrayList of type Contact
        ArrayList<Contact> studentsList = initializeContactsArray();
       
        //Create an instance of Scanner class
        Scanner scanner = new Scanner(System.in);
        boolean repeat=true;
       
        String choice=\"\";      
        //Loop continues until user enters n to stop
        while(repeat)
        {
            promptForOption(studentsList);
            System.out.println(\"Do you want to continue [y or n :]\");
            choice=scanner.nextLine();
           
            if(choice.charAt(0)==\'n\'||choice.charAt(0)==\'N\')
                repeat=false;
        }
    }
  
    /*
    * Method to intialize the student list with contac objects
    * */
    private static ArrayList<Contact> initializeContactsArray()
    {
        ArrayList<Contact> studentsList = new ArrayList<Contact>();
        studentsList.add(new Contact(\"Bill\", \"Bose\", \"NY\", 46));
        studentsList.add(new Contact(\"Morry\", \"Apple\", \"KY\", 23));
        studentsList.add(new Contact(\"Angel\", \"Bradit\", \"NE\", 56));
        studentsList.add(new Contact(\"Murali\", \"Mutta\", \"LD\", 35));
        studentsList.add(new Contact(\"Hani\", \"Hunt\", \"IE\", 67));
       return studentsList;
    }
  
    /*
    * Method to prompt user to select a choice
    * */
    private static void promptForOption(ArrayList<Contact> contacts) {
        Scanner scanner = new Scanner(System.in);
       System.out.println(\"Options \ Sort by Last Name:[1] \");
        System.out.println(\"Sort by Home State: [2]\");
        System.out.println(\"nSort by Age:        [3]\");
        System.out.println(\"Exit Application:   [0]\");
        System.out.println(\"Please enter your choice:\");
        int answer = scanner.nextInt();
       switch(answer)
        {
        case 1:
            Collections.sort(contacts, Contact.sortByLastName);
            break;
        case 2:          
            Collections.sort(contacts, Contact.sortByStateName);
            break;
        case 3:
            Collections.sort(contacts, Contact.sortByageName);
            break;
        case 0:
            System.exit(0);
        default :
            System.out.println(\"Invalid choice\");
        }
        //calling method displayContacts
        displayContacts(contacts);
    }
   
   
    /*
    * displayContacts that display the contacts to console.
    * */
    private static void displayContacts(ArrayList<Contact> contacts) {
       for (Contact contact : contacts) {
            System.out.println(contact);
        }
   }
 }
--------------------------------------------------------------------------------------------------
/**
 * The class Contact contains methods to sort
 * by last name, state and age.
 * */
 //Contact.java
 import java.util.Comparator;
 public class Contact
 {
   
    //instance variables of class Contact
    private String firstName;
    private String lastName;
    private String state;
    private Integer age;
  
    //Constructor that takes first name, last name, state and age
    public Contact(String firstName, String lastName,
            String state, Integer age) {
       this.firstName = firstName;
        this.lastName = lastName;
        this.state = state;
        this.age=age;
    }
   public String getFirstName() {
        return firstName;
    }
   public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
   public String getLastName() {
        return lastName;
    }
   public void setLastName(String lastName) {
        this.lastName = lastName;
    }
   public String getState() {
        return state;
    }
   public void setState(String state) {
        this.state = state;
    }
   public Integer getAge() {
        return age;
    }
   public void setAge(int age) {
        this.age = age;
    }
   //Comparator that sorts the last name
    public static Comparator<Contact> sortByLastName = new Comparator<Contact>() {
       public int compare(Contact c1, Contact c2) {
            String cname1 = c1.getLastName().toUpperCase();
            String cname2 = c2.getLastName().toUpperCase();
           return cname1.compareTo(cname2);
        }
    };
   //Comparator that sort two contacts by state
    public static Comparator<Contact> sortByStateName = new Comparator<Contact>() {
       public int compare(Contact s1, Contact s2) {
            String state1 = s1.getState().toUpperCase();
            String state2 = s2.getState().toUpperCase();
           return state1.compareTo(state2);
        }
    };
    //Comparator that sort two contacts by age
    public static Comparator<Contact> sortByageName = new Comparator<Contact>() {
        public int compare(Contact s1, Contact s2) {
            int age1 = s1.getAge();
            int age2 = s2.getAge();
            return age1 - age2;
        }
    };
    //Returns the string representation of Contact
    public String toString() {
        return String.format(\"%-10s%-10s%-10s%-5d\", firstName,lastName,state,age);
    }
 }
--------------------------------------------------------------------------------------------------
Sample output:
Options
 Sort by Last Name:[1]
 Sort by Home State: [2]
 nSort by Age:        [3]
 Exit Application:   [0]
 Please enter your choice:
 1
 Morry     Apple     KY        23
 Bill      Bose      NY        46
 Angel     Bradit    NE        56
 Hani      Hunt      IE        67
 Murali    Mutta     LD        35
 Do you want to continue [y or n :]
 y
 Options
 Sort by Last Name:[1]
 Sort by Home State: [2]
 nSort by Age:        [3]
 Exit Application:   [0]
 Please enter your choice:
 2
 Hani      Hunt      IE        67
 Morry     Apple     KY        23
 Murali    Mutta     LD        35
 Angel     Bradit    NE        56
 Bill      Bose      NY        46
 Do you want to continue [y or n :]
 y
 Options
 Sort by Last Name:[1]
 Sort by Home State: [2]
 nSort by Age:        [3]
 Exit Application:   [0]
 Please enter your choice:
 3
 Morry     Apple     KY        23
 Murali    Mutta     LD        35
 Bill      Bose      NY        46
 Angel     Bradit    NE        56
 Hani      Hunt      IE        67
 Do you want to continue [y or n :]
 n






