I'm writing a program that lets you add, delete, and search for people in an address book. I have to implement my own linked list instead of using the class in the java.util package. I am having A LOT of trouble on searching through the list. For my method searchPerson() in the Controller class, I have to search by last name. I managed to load the information from a text file (name, address, phone number, etc) into the list but I do not know how to search for their entry. I've tried to implement an iterator but I do not see how that would work. My attempt at searchPerson() is shown below. I didn't include the other classes because they were just get and set functions.
addressBookTypeController
extPersonType
OrderedLinkedList
addressBookTypeController
import java.util.Scanner; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.io.FileOutputStream; public class addressBookTypeController extends TNOrderedLinkedList{ public TNOrderedLinkedList<extPersonType> linkedList = new TNOrderedLinkedList<extPersonType>(); public void loadData(){ Scanner inputStream = null; try{ inputStream = new Scanner(new FileInputStream("Programming Assignment 1 Data.txt")); }catch(FileNotFoundException e){ System.out.println("File Programming Assignment 1 Data.txt was not found"); System.out.println("or could not be opened."); System.exit(0); } String[] temp = new String[8]; int i = 0; String first = "", last = "", month = "", day = "", year = "", address = "", city = "", state = "", zip = "", number = ""; while(inputStream.hasNext()){ inputStream.useDelimiter("\n"); temp[i] = inputStream.next(); if(i == 0){ String theName = temp[i]; String[] split = theName.split(" "); first = split[0]; last = split[1]; } if(i == 1){ String theDate = temp[i]; String[] split = theDate.split(" "); month = split[0]; day = split [1]; year = split [2]; } i++; if(i == 8){ addressType tempAddr = new addressType(temp[2],temp[3],temp[4],temp[5]); dateType tempDob = new dateType(month,day,year); extPersonType extPerson = new extPersonType(first,last,tempAddr,tempDob,number,temp[7]); linkedList.addToStart(extPerson); i = 0; } } } public void searchPerson(String searchLastName){ Scanner cin = new Scanner(System.in); String lastName; System.out.println("Enter last name: "); lastName = cin.next(); linkedList.findData(lastName); } }
extPersonType
public class extPersonType extends personType{ addressType address; dateType dob; String phoneNumber; String personStatus; public extPersonType(String theFirst, String theLast, addressType newAddress, dateType newDob, String newPhoneNumber, String newPersonStatus){ super(theFirst, theLast); address = newAddress; dob = newDob; phoneNumber = newPhoneNumber; personStatus = newPersonStatus; } public String toString(){ return (this.getFirst() + " " + this.getLast() + "," + " " + address + " " + dob + " " + phoneNumber + " " + personStatus); } }
OrderedLinkedList
public class TNOrderedLinkedList <extPersonType>{ public Node<extPersonType> head; public TNOrderedLinkedList(){ head = null; } public void addToStart(extPersonType itemData){ head = new Node(itemData, head); } public boolean deleteHeadNode(){ if(head != null){ head = head.link; return true; } else return false; } public int size(){ int count = 0; Node<extPersonType> position = head; while(position != null){ count++; position = position.link; } return count; } public boolean contains(extPersonType item){ return (find(item) != null); } private Node<extPersonType> find(extPersonType target){ Node<extPersonType> position = head; extPersonType itemAtPosition; while(position != null){ itemAtPosition = position.data; if(itemAtPosition.equals(target)) return position; position = position.link; } return null; } public extPersonType findData(extPersonType target){ Node<extPersonType> result = find(target); if(result == null) return null; else return result.data; } public void outputList(){ Node<extPersonType> position = head; while(position != null){ System.out.println(position.data); position = position.link; } } public boolean isEmpty(){ return (head == null); } public void clear(){ head = null; } private class Node<extPersonType>{ private extPersonType data; private Node<extPersonType> link; public Node(){ data = null; link = null; } public Node(extPersonType newData, Node<extPersonType> linkValue){ data = newData; link = linkValue; } } }