I need to transfer all the data I got in ArrayList to LinkedList and display all it all just like I did with ArrayList. I failed to display it out when I got the data transferred to LinkedList. The code is as below :
import javax.swing.*;
import java.util.*;
public class testEmployee
{
public static void main (String [] args)
{
ArrayList <Employee> empArray = new ArrayList();
LinkedList yrIncm = new LinkedList();
Employee emp;
int empNum;
boolean found = true;
empNum = Integer.parseInt(JOptionPane.showInputDialog ("How many employees information do you want to store?"));
for (int i = 0; i < empNum; i++)
{
String sEmpId = JOptionPane.showInputDialog ("Please enter the employee's ID");
String sEmpName = JOptionPane.showInputDialog ("Please enter the employee's Name");
String sEmpPosition = JOptionPane.showInputDialog ("Please enter the employee's position");
Double dSalary = Double.parseDouble (JOptionPane.showInputDialog ("Please enter the employee's monthly salary"));
emp = new Employee (sEmpId, sEmpName, sEmpPosition, dSalary);
empArray.add (emp);
}
System.out.println ("Employee that obtains a monthly salary more than RM 2000.00");
System.out.println ("===========================================================");
for (int i = 0; i<empArray.size(); i++)
{
if (empArray.get(i).getSalary() > 2000)
{
empArray.get(i).display(); // This will display the info using ArrayList
}
}
System.out.println ("\nEmployee that have yearly income greater than RM 80,000");
System.out.println ("=======================================================");
for (int i = 0; i<empArray.size(); i++)
{
if ((empArray.get(i).getSalary() * 12) > 80000)
{
yrIncm.add (empArray.get(i)); // Is this the correct way of transferring the data?
System.out.println (yrIncm); // How do you print it all back?
}
}
}
}
The display() in class Employee:
public void display()
{
System.out.println ("\nEmployee's ID : " + sEmpId);
System.out.println ("Employee's Name : " + sEmpName);
System.out.println ("Employee's Position : " + sEmpPosition);
System.out.println ("Employee's Salary : RM " + df.format (dSalary));
}
I'm unable to use the method display() to print it out from the LinkedList. Any help would be appreciated
display(). You should instead override thetoString()method inEmployee. You can then print a list and all its elements will be properly printed.