Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- public class ContactApp {
- private MyArrayList<Contact> array;
- public ContactApp(MyArrayList<Contact> array) {
- this.array = array;
- }
- public void contactLoading() {
- Scanner lineScanner = null;
- try {
- lineScanner = new Scanner(new File("FacultyStaff.txt"));
- } catch (FileNotFoundException e) {
- System.exit(1);
- }
- MyArrayList<Contact> facultyStaff = new MyArrayList<Contact>();
- while (lineScanner.hasNextLine()) {
- String line = lineScanner.nextLine();
- String[] splitLine = line.split("\t");
- facultyStaff.add(new Contact(splitLine[0], splitLine[1]));
- }
- lineScanner.close();
- this.array = facultyStaff;
- }
- public void alphabetSort() {
- for (int i = 0; i < this.array.size(); i++) {
- int index = 0;
- String earliestName = this.array.get(i).getName().toUpperCase();
- for (int k = i; k < this.array.size(); k++) {
- String currentName = this.array.get(k).getName().toUpperCase();
- if (currentName.compareTo(earliestName) < 0) {
- earliestName = currentName;
- index = k;
- }
- }
- Contact newLowest = this.array.set(i, this.array.get(index));
- this.array.set(index, newLowest);
- }
- for (int i = 0; i < this.array.size(); i++) {
- System.out.println(this.array.get(i).getName());
- }
- }
- public void lookup() {
- boolean methodState = true;
- Scanner userInput = new Scanner(System.in);
- while (methodState == true) {
- System.out.println("What name would you like to search for? (Type exit to quit)");
- String name = userInput.nextLine();
- if (name.equalsIgnoreCase("exit")) {
- methodState = false;
- userInput.close();
- }
- boolean searchState = true;
- int min = 0;
- int max = this.array.size() - 1;
- while (searchState = true) {
- if (min >= max) {
- System.out.println("The requested name could not be located");
- searchState = false;
- }
- int middle = this.array.size() / 2;
- Contact current = this.array.get(middle);
- if (current.getName().startsWith(name) == true) {
- System.out.println("The office found is: " + current.getOffice() + " (" + current.getName() + ")");
- searchState = false;
- }
- else if (name.compareTo(current.getName()) < 0) {
- max = middle - 1;
- }
- else {
- min = middle + 1;
- }
- }
- userInput.close();
- }
- }
- public static void main(String[] args) {
- ContactApp system = new ContactApp(null);
- system.contactLoading();
- system.alphabetSort();
- system.lookup();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment