0

Employee Class:

public class Employee
{

String ID;
String Fname;
String Lname;
String City;
String Major;
int GPA;

    public Employee()
{
    GPA = 0;
}


public void ShowInfo()
{
    JOptionPane.showMessageDialog(null, "ID: " + ID + "\nFirst Name: " + Fname + "\nLast Name:" + Lname + "\nCity: " + City + "\nMajor: " + Major + "\nGPA: " + GPA); 
}

public void EnterInfo()
{

    ID = JOptionPane.showInputDialog("Enter Student ID");
    Fname = JOptionPane.showInputDialog("Enter Student First Name");
    Lname = JOptionPane.showInputDialog("Enter Student Last Name");
    City = JOptionPane.showInputDialog("Enter Student City");
    Major = JOptionPane.showInputDialog("Enter Student Major");
    String gpa = JOptionPane.showInputDialog("Enter Student GPA");
    GPA = Integer.parseInt(gpa);
}

  }
}

Linked List:

public class EmployeeList
{
Node first;
Node last;
int count;


public EmployeeList()
{
    first = new Node();
    first = null;

    last = new Node();
    last = null;

    count = 0;
}

public boolean empty()
{
    return first == null;
}

public void add(Employee emp)
{
    Node newEmployee = new Node();
    newEmployee.e = emp;
    newEmployee.e.EnterInfo();
    newEmployee.next=null;

    if(empty())
    {
        first=newEmployee;
        last=first;
    }       

    else  
    {
        last.next = newEmployee; 
        last = last.next;
    }

    count++;
}

public boolean search(String id)
{
    Node temp = new Node();
    Employee emp = new Employee();
    temp = first;

    while(temp!=null)
    {
        emp = temp.e;
        if(id.equals(emp.ID)) 
        {
            emp.ShowInfo();
            return true;
        }

        temp=temp.next;
    }  
    return false;  
 }

public boolean delete(String id)
{
    Employee emp = new Employee();

    if(!empty())
    { 
        if(first.e.ID.equals(id))
        { 
            first=first.next; 
            return true; 
        }
        else
        {
            Node previous = new Node();
            Node temp = new Node();

            previous = first;
            temp = first.next;

            while(temp!=null)
            {   
                emp = temp.e;

                if(id.equals(emp.ID))
                {
                    count--;
                    previous.next = temp.next;
                    return true;
                }

                previous = previous.next;
                temp = temp.next;
            }  

            return false;
        }
    }

return false;
}

public String ALL()
{
    String all = new String();
    Node temp = new Node();
    Employee emp = new Employee();

    temp = first;

    while(temp!=null)
    {
        emp = temp.e;
        all = all + emp.ID + "-";
        temp = temp.next;
    } 

    all = all + "null";

    return all;
} 

}

I really don't know what's the problem here, If i try to print them all, i keep getting the last entered value.

Node class:

public class Node
{
Employee e = new Employee();
Node next;
}

By searching im not getting any result, just employee ID not found. EnterInfo method is just for input of the variables (ID,Fname.....)

Any help ? and thanks.

Edit: i know its wrong that way, i should add getters and setter, but this is how the teacher started and told us to start this way.

3
  • Please post some code that actually compiles, and make sure you include the calling part. Use a debugger to step through your code yourself, it should be easy to find out what's happening that way. Keep to the Java code conventions, and know when you have to perform a new for variables. Typically, search methods for instance, should not contain any new Node or Employee instances. Commented Mar 19, 2012 at 23:22
  • You should submit more complete code: for the Node and the List. Otherwise, www.geekviewpoint.com has an easy to follow linkedList implementation. Commented Mar 19, 2012 at 23:27
  • I edited the post, all have been posted except the test class. Commented Mar 19, 2012 at 23:44

1 Answer 1

2

Your search is failing because you are incorrectly testing for String equality. This line:

if(emp.ID == id)

Tests for object reference equality. It will only work for interned values (which is out of scope for this assignment of yours). You should change it too:

if(id.equals(emp.ID))

Some quick notes on your code:

  • You are not following best practices in naming. Your variables should begin with lowercase letters.
  • You are not following best practice in property scoping. Your class variables should be private, with appropriate getters/setters
  • In the beginning of your search method you are unnecessarily creating a Node instance

    Node temp = new Node();

  • Your are incorrectly testing for String equality in your delete method.

Sign up to request clarification or add additional context in comments.

5 Comments

mm Still same issue when i enter a number, but when i enter a text it works... ill see what the problem now
mmm Also if i add a new employee, it doesnt work, all disapear except last one
Your add function looks fine. How are you verifying if the values are getting added or not?
PrintAll function, also searching for an ID will show not find it unless its the last one
You can add your PrintAll function code to your question. But I would advise you look through your code and ensure you are fixing the mistakes previously outlined. Also, a debugger would be indispensable in tracking down some of these issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.