How to set the value of an object variable in an ArrayList

I am working on a task in which I must:

  • Create an Employee class with the following attributes / variables: name age department

  • Create a class called Department that will contain a list of employees.

    but. The department class will have a method that returns its employees sorted by age.

    b. The department value can be only one of the following:

    • "Accounting"
    • "Marketing"
    • "Human resources"
    • "Information Systems"

My hardest part is trying to figure out how to complete 2b. Here is what I still have:

import java.util.*; public class Employee { String name; int age; String department; Employee (String name, int age, String department) { this.name = name; this.age = age; this.department = department; } int getAge() { return age; } } class Department { public static void main(String[] args) { List<Employee>empList = new ArrayList<Employee>(); Collections.sort (empList, new Comparator<Employee>() { public int compare (Employee e1, Employee e2) { return new Integer (e1.getAge()).compareTo(e2.getAge()); } }); } } 
+4
java arraylist
Jul 12 '13 at 3:05
source share
1 answer

You can use enumerations for the same purpose, which restricts the use of only the specified values. Declare a Department listing as follows

 public enum Department { Accounting, Marketting, Human_Resources, Information_Systems } 

Employee class can now be

 public class Employee { String name; int age; Department department; Employee(String name, int age, Department department) { this.name = name; this.age = age; this.department = department; } int getAge() { return age; } } 

and when creating an employee you can use

 Employee employee = new Employee("Prasad", 47, Department.Information_Systems); 

EDIT proposed by Adrian Shum and of course because it is a great offer.

  • Enumerations are constants, so it is useful to declare it in capital letters in accordance with java conventions.
  • But we don’t want the representation of capital from enums displayed, so that we can create enum constructors and pass readable information to it.
  • We modify enum to include the toString() method and constructor , which takes a string argument.

      public enum Department { ACCOUNTING("Accounting"), MARKETTING("Marketting"), HUMAN_RESOURCES( "Human Resources"), INFORMATION_SYSTEMS("Information Systems"); private String deptName; Department(String deptName) { this.deptName = deptName; } @Override public String toString() { return this.deptName; } } 

So, when we create an Employee object as follows and use it,

 Employee employee = new Employee("Prasad Kharkar", 47, Department.INFORMATION_SYSTEMS); System.out.println(employee.getDepartment()); 

We get a readable string representation as Information Systems , because it is returned by the toString() method, which is implicitly called the System.out.println() operator. Read a good tutorial on Enumerations

+15
Jul 12 '13 at 3:23
source share



All Articles