Hello Everyone,
1st and foremost I would like to greet all forum members. I am having a nightmare trying to set this program to search for string values with-in a circular list. My objective here is to input a value corresponding to a surname of class person which extends from another class called AnyClass. The result should be found or not found in list ( the surname)and a display of the record. I have already sorted out the index number search and would be ETERNALLY GRATEFULL for anybody who sorts me out in this regard.(the surname part). here under is the code. if anybody wants to paste it to an editor to test, i have grouped all classes in 1 , But the utilities file is separate.
import java.util.*; class Utilities { public static String randName( int n) { String name = ""; // empty string name = name + (char)randInt(65,90); for (int k = 1; k < n; k++) name = name + (char)randInt(97,122); return name; } } import java.util.*; import java.io.*; class Node { public Node next; public Person obj; public Node( Person newObj) { obj = newObj; next = null; } public void show() { System.out.println(obj.getData()); } public void editNode() { //obj.editData(); } } class AnyClass // Refer to T1 { int seqNo; /* ------------ Constructor (random data) ------------------*/ public AnyClass( int num) { seqNo = num; } public AnyClass() { } /* ------------ Methods ------------------*/ public String getData() { return "Sequence number: "+ seqNo +". "; } public void editData() { } public boolean equals (Object keyObj) { AnyClass temp = (AnyClass) keyObj; return (temp.seqNo == this.seqNo); } public boolean equals (String key) { return false; } } class Person extends AnyClass { private String surname; int id; protected double pay; public Person (String iSurn,int iIdNo, double iPay) { super (iIdNo); surname = iSurn; id = iIdNo; pay = iPay; } public Person () { } double getSalary() // returns value of object's data - encapsulation (no direct access) { return pay; } void setPay (double newPay) //alterates value of object's data - encapsulation (no direct access) { pay = newPay; } public String getData() { return super.getData()+"Surname: "+surname+", "+"ID No: "+id+ "Salary EUR "+getSalary(); } /*public boolean equals(Object keyObj) { Person temp = (Person)keyObj; return (temp.seqNo==this.seqNo);//&&(temp.surname == this.surname); } // maybe wrong */ public boolean equals(String keyName) { return(this.surname.equals(keyName)); } } // end of class Person class List // Refer to notes { public Node head; public List() { head = null; } public String getData() // create empty CLL { if(head==null)return "Empty List"; //if last is null, print "()" Node temp = head.next; // create temporary pointer and assign to last.next to start from beginning String retval = "( "; // print "(" to indicate start of CLL print do { temp.show(); retval= (retval + temp.obj.getData() + " "); // print "(" + object data temp = temp.next; // incriment temp to next node } while(temp!=head.next); // do this until temp has reach again the first node retval+=")"; // when first node is reached, print ")" return "End of Process"; // return all available data } public boolean isEmpty() { return (head==null); } public void clear() { head=null; } public Object delete() { if(isEmpty())return ("empty list"); Object temp = head.next.obj; //obj head.next = head.next.next; return temp; } public AnyClass equals(AnyClass keyObj) // () { if(isEmpty())System.out.println("is Empty"); Node temp = head.next; do { if(temp.obj.equals (keyObj)) //System.out.println("Found"); return temp.obj; temp=temp.next; } while(temp!=head.next); return null; } public Person equals(Person keyObj) // () { if(isEmpty())System.out.println("is Empty"); Node temp = head.next; do { if(temp.obj.equals (keyObj)) //System.out.println("Found"); return temp.obj; temp=temp.next; } while(temp!=head.next); return null; } public void reverse() { List temp = new List(); Node temper = head.next; if(isEmpty()){} else { do{ temp.append(temper.obj); temper = temper.next; } while(temper!=head.next); head = temp.head; } } public void append(Person newObj) { Node temp = new Node(newObj); //newObj if(head ==null) { temp.next = temp; head = temp; } else { temp.next = head.next; head.next = temp; } } } public class mainList { public static void main(String [] args) { List myList = new List(); // empty list myList.getData(); for (int k = 1; k<= 10; k++) myList.append(new Person(Utilities.randName(6),k,600.00)); myList.getData(); System.out.println("\n"); System.out.println("************** Circular list Search Function*********************"); System.out.println("\n"); Scanner myInp = new Scanner(System.in); System.out.println("Please enter value to search?"); int i = myInp.nextInt(); AnyClass key = new AnyClass(i); AnyClass temp = myList.equals(key); if( temp!= null) System.out.println("Object found. Its data is: "+temp.getData()); else System.out.println("Object Not Found!"); System.out.println("************** Circular list Search Function 2*********************"); System.out.println("\n"); String nameInput; Scanner in = new Scanner(System.in); System.out.println("Enter name to search; "); nameInput = in.nextLine(); } }