Guest User

Untitled

a guest
Mar 4th, 2024
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class ContactApp {
  5.  
  6. private MyArrayList<Contact> array;
  7.  
  8. public ContactApp(MyArrayList<Contact> array) {
  9. this.array = array;
  10. }
  11.  
  12.  
  13. public void contactLoading() {
  14. Scanner lineScanner = null;
  15. try {
  16. lineScanner = new Scanner(new File("FacultyStaff.txt"));
  17. } catch (FileNotFoundException e) {
  18. System.exit(1);
  19. }
  20.  
  21. MyArrayList<Contact> facultyStaff = new MyArrayList<Contact>();
  22.  
  23. while (lineScanner.hasNextLine()) {
  24. String line = lineScanner.nextLine();
  25. String[] splitLine = line.split("\t");
  26.  
  27. facultyStaff.add(new Contact(splitLine[0], splitLine[1]));
  28. }
  29. lineScanner.close();
  30. this.array = facultyStaff;
  31. }
  32.  
  33. public void alphabetSort() {
  34. for (int i = 0; i < this.array.size(); i++) {
  35. int index = 0;
  36. String earliestName = this.array.get(i).getName().toUpperCase();
  37. for (int k = i; k < this.array.size(); k++) {
  38. String currentName = this.array.get(k).getName().toUpperCase();
  39. if (currentName.compareTo(earliestName) < 0) {
  40. earliestName = currentName;
  41. index = k;
  42. }
  43. }
  44. Contact newLowest = this.array.set(i, this.array.get(index));
  45. this.array.set(index, newLowest);
  46. }
  47.  
  48. for (int i = 0; i < this.array.size(); i++) {
  49. System.out.println(this.array.get(i).getName());
  50. }
  51. }
  52.  
  53. public void lookup() {
  54.  
  55. boolean methodState = true;
  56.  
  57. Scanner userInput = new Scanner(System.in);
  58.  
  59. while (methodState == true) {
  60.  
  61. System.out.println("What name would you like to search for? (Type exit to quit)");
  62.  
  63. String name = userInput.nextLine();
  64.  
  65. if (name.equalsIgnoreCase("exit")) {
  66. methodState = false;
  67. userInput.close();
  68. }
  69.  
  70. boolean searchState = true;
  71. int min = 0;
  72. int max = this.array.size() - 1;
  73.  
  74. while (searchState = true) {
  75.  
  76. if (min >= max) {
  77. System.out.println("The requested name could not be located");
  78. searchState = false;
  79. }
  80.  
  81. int middle = this.array.size() / 2;
  82. Contact current = this.array.get(middle);
  83.  
  84. if (current.getName().startsWith(name) == true) {
  85. System.out.println("The office found is: " + current.getOffice() + " (" + current.getName() + ")");
  86. searchState = false;
  87. }
  88.  
  89. else if (name.compareTo(current.getName()) < 0) {
  90. max = middle - 1;
  91. }
  92. else {
  93. min = middle + 1;
  94. }
  95. }
  96. userInput.close();
  97. }
  98. }
  99.  
  100. public static void main(String[] args) {
  101.  
  102. ContactApp system = new ContactApp(null);
  103.  
  104. system.contactLoading();
  105.  
  106. system.alphabetSort();
  107.  
  108. system.lookup();
  109.  
  110.  
  111.  
  112. }
  113.  
  114. }
Advertisement
Add Comment
Please, Sign In to add comment