Advertisement
Guest User

Untitled

a guest
Feb 20th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. package Sorter;
  2.  
  3. import java.util.Comparator;
  4.  
  5. public class Contact {
  6. private String firstName;
  7. private String lastName;
  8. private String state;
  9. private Integer age;
  10.  
  11. public Contact(String firstName, String lastName, String state, Integer age) {
  12.  
  13. this.firstName = firstName;
  14. this.lastName = lastName;
  15. this.state = state;
  16. }
  17.  
  18. public String getFirstName() {
  19. return firstName;
  20. }
  21.  
  22. public void setFirstName(String firstName) {
  23. this.firstName = firstName;
  24. }
  25.  
  26. public String getLastName() {
  27. return lastName;
  28. }
  29.  
  30. public void setLastName(String lastName) {
  31. this.lastName = lastName;
  32. }
  33.  
  34. public String getState() {
  35. return state;
  36. }
  37.  
  38. public void setState(String state) {
  39. this.state = state;
  40. }
  41.  
  42. public Integer getAge() {
  43. return age;
  44. }
  45.  
  46. public void setAge(int age) {
  47. this.age = age;
  48. }
  49.  
  50. public static Comparator<Contact> lastNameComparator = new Comparator<Contact>() {
  51.  
  52. public int compare(Contact s1, Contact s2) {
  53. String contactLastName1 = s1.getLastName().toUpperCase();
  54. String contactLastName2 = s2.getLastName().toUpperCase();
  55.  
  56. return contactLastName1.compareTo(contactLastName2);
  57. }
  58. };
  59.  
  60. public static Comparator<Contact> stateComparator = new Comparator<Contact>() {
  61.  
  62. public int compare(Contact s1, Contact s2) {
  63. String state1 = s1.getState().toUpperCase();
  64. String state2 = s2.getState().toUpperCase();
  65.  
  66. return state1.compareTo(state2);
  67. }
  68. };
  69.  
  70. public static Comparator<Contact> ageComparator = new Comparator<Contact>() {
  71.  
  72. public int compare(Contact s1, Contact s2) {
  73. int age1 = s1.getAge();
  74. int age2 = s2.getAge();
  75.  
  76. return age1 - age2;
  77. }
  78. };
  79.  
  80. @Override
  81. public String toString() {
  82. return ("First Name: " + firstName + ", Last Name: " + lastName + ", State: " + state + ", Age: " + age);
  83. }
  84. }
  85.  
  86. import java.util.ArrayList;
  87. import java.util.Collections;
  88. import java.util.Scanner;
  89.  
  90. public class TestSortOptions {
  91.  
  92. public static void main(String[] args) {
  93. ArrayList<Contact> contacts = initializeContactsArray();
  94. promptForOption(contacts);
  95. }
  96.  
  97. private static ArrayList<Contact> initializeContactsArray() {
  98. ArrayList<Contact> contacts = new ArrayList<Contact>();
  99. contacts.add(new Contact("Joe", "Jones", "IL", 35));
  100. contacts.add(new Contact("Bill", "Barnes", "OH", 62));
  101. contacts.add(new Contact("Ida", "Know", "FL", 23));
  102. contacts.add(new Contact("Adam", "Ant", "MI", 14));
  103. contacts.add(new Contact("Jane", "Doe", "CA", 41));
  104.  
  105. return contacts;
  106. }
  107.  
  108. private static void promptForOption(ArrayList<Contact> contacts) {
  109. Scanner input = new Scanner(System.in);
  110.  
  111. System.out.println("Options nSort by Last Name: [1] " + "nSort by Home State: [2] "
  112. + "nSort by Age: [3] " + "nExit Application: [0] " + "nnPlease enter your choice: ");
  113. String answer = input.next();
  114.  
  115. if (answer == "1") {
  116. Collections.sort(contacts, Contact.lastNameComparator);
  117. for (Contact contact : contacts) {
  118. System.out.println(contact);
  119. }
  120.  
  121. if (answer == "2") {
  122. Collections.sort(contacts, Contact.stateComparator);
  123. for (Contact contact : contacts) {
  124. System.out.println(contact);
  125. }
  126.  
  127. if (answer == "3") {
  128. Collections.sort(contacts, Contact.ageComparator);
  129. for (Contact contact : contacts) {
  130. System.out.println(contact);
  131. }
  132.  
  133. if (answer == "0") {
  134. System.exit(0);
  135. }
  136.  
  137. else {
  138. System.out.println("Invalid Entry");
  139. }
  140. }
  141. }
  142. }
  143. }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement