public class Employee
{
protected String name;
protected int idNumber;
protected int age;
protected double salary;
protected String title;
protected String department;
private Employee nextEmployee;
public Employee(String name , int idNumber , int age , double salary , String title , String department)
{
this(name , idNumber , age , salary , title , department , null);
}
public Employee(String name , int idNumber , int age , double salary , String title , String department , Employee next)
{
this.name = name;
this.idNumber = idNumber;
this.age = age;
this.salary = salary;
this.title = title;
this.department = department;
this.nextEmployee = next;
}
public String getName()
{
return name;
}
public int getID()
{
return idNumber;
}
public int getAge()
{
return age;
}
public double getSalary()
{
return salary;
}
public String getTitle()
{
return title;
}
public String getDepartment()
{
return department;
}
public String toString()
{
return String.format("%s: %s\n%s: %d\n%s: %d\n%s: %.2f\n%s: %s\n%s: %s\n" , "Name" , getName() , "ID" , getID() , "Age" , getAge() , "Salary" , getSalary() , "Title" , getTitle() , "Department" , getDepartment());
}
public void updateSalary(int amount)
{
this.salary += amount;
}
public void updateSalary(double rate)
{
double temp = this.salary * rate;
this.salary += temp;
}
public void setNextEmployee(Employee next)
{
nextEmployee = next;
}
public Employee getNextEmployee()
{
return nextEmployee;
}
public void setSalary(double salary)
{
this.salary = salary;
}
}
public class ListOfEmployee
{
private Employee first;
private Employee last;
private static int i =0;
public ListOfEmployee()
{
first = last = null;
}
public void addEmployee(String name , int idNumber , int age , double salary , String title , String department)
{
if(isEmpty())
{
first = last = new Employee(name , idNumber , age , salary , title , department);
return;
}
else
{
last = last.getNextEmployee();
last = new Employee(name , idNumber , age , salary , title , department);
}
}
public void removeEmployee(String name , int id , String department)
{
if(isEmpty())
{
return ;
}
if(first == last)
{
first = last = null;
return;
}
Employee firedEmployee = first;
Employee previousEmployee = first;
Employee lastEmployee = last;
//to check whether the first employee is the one who is going to be fired
if(name.equalsIgnoreCase(firedEmployee.getName()) && firedEmployee.getID() == id && department.equalsIgnoreCase(firedEmployee.getDepartment()))
{
first = firedEmployee.getNextEmployee();
firedEmployee = null;
return;
}
//to check whether the last employee is the one who is going to be fired
if((name.equalsIgnoreCase(lastEmployee.getName())) && firedEmployee.getID() == id && department.equalsIgnoreCase(firedEmployee.getDepartment()))
{
while(firedEmployee.getNextEmployee() != lastEmployee)
{
firedEmployee = firedEmployee.getNextEmployee();
}
last = firedEmployee;
firedEmployee = firedEmployee.getNextEmployee();
firedEmployee = null;
return;
}
//since the first and the last employee are not the one who is being fired .
firedEmployee = firedEmployee.getNextEmployee();
previousEmployee = firedEmployee;
while((name.equalsIgnoreCase(firedEmployee.getName()) == false )&& firedEmployee.getID() != id && (department.equalsIgnoreCase(firedEmployee.getDepartment()) == false))
{
firedEmployee = firedEmployee.getNextEmployee();
if((name.equalsIgnoreCase(firedEmployee.getName()) == false) && firedEmployee.getID() != id && (department.equalsIgnoreCase(firedEmployee.getDepartment()) == false))
{
previousEmployee = firedEmployee;
}
}
Employee nextEmployee = firedEmployee.getNextEmployee();
previousEmployee.setNextEmployee(nextEmployee);
firedEmployee = null;
}
public boolean isEmpty()
{
return first == null;
}
public void printListOfEmployee()
{
Employee current = first;
while( current != null)
{
System.out.printf("%s\n" , current.toString());
current = current.getNextEmployee();
}
}
public void printEmployeeList()
{
Employee current = first;
while(current != null)
{
System.out.printf("%d\n" , i);
System.out.printf("Name: %s , ID: %d , Age: %d , Department : %s" , current.getName() , current.getID() , current.getAge() , current.getDepartment());
current = current.getNextEmployee();
}
}
public void salary(String name , int idNumber , int amount)
{
Employee current = findEmployee(name , idNumber);
if(current != null)
{
current.updateSalary(amount);
}
else
{
System.out.println("No such person");
}
}
public void salary(String name , int idNumber , double rate)
{
Employee current = findEmployee(name , idNumber);
if(current != null)
{
current.updateSalary(rate);
}
else
{
System.out.println("No such person");
}
}
public Employee findEmployee(String name , int idNumber )
{
if(isEmpty())
{
return null;
}
Employee current = first;
while((current.getName() != "name") && (current.getID() != idNumber ) && (current != null))
{
current.getNextEmployee();
}
if(current == null)
{
return null;
}
else
{
return current;
}
}
}
public class TestEmployee
{
public static void main(String args[])
{
ListOfEmployee employees = new ListOfEmployee();
employees.addEmployee("wen hao" , 12323 , 12 , 2323.00 , "dfad" , "safsdf");
employees.addEmployee("wen hao2" , 1233131 , 12 , 2323.00 ," adsfa " , "dfdfd");
employees.addEmployee("wen hao3" , 1233 , 12 , 2323.00 , "dfsdf" , "fdfd");
employees.printEmployeeList();
System.out.println();
}
}
i can't print all the three employees ...~ why ?


Sign In
Create Account

Guest_R3.RyozKidz_*
Back to top










