user1200204
user1200204

Reputation: 35

Another issue with LinkedList implementation with another class

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.

Upvotes: 0

Views: 574

Answers (1)

Perception
Perception

Reputation: 80633

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.

Upvotes: 2

Related Questions