4

I am working on an assignment in which I have to:

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

  2. Create a class called Department which will contain a list of employees.

    a. Department class will have a method which will return its employees ordered by age.

    b. Value of Department can be only one of the following:

    • "Accounting"
    • "Marketing"
    • "Human Resources"
    • "Information Systems"

I am having the hardest time trying to figure out how to complete 2b. Here is what I have so far:

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());
        }
    });
    }
}   
3
  • 13
    " b. Value of Department can be only one of the following: "Accounting" "Marketing" "Human Resources" "Information Systems"" -- use an enum type for this property, not a String. This will force Java to only allow specific items of the type for this variable or parameter. Commented Jul 12, 2013 at 3:10
  • 1
    For more information, check out Enum Types Commented Jul 12, 2013 at 3:45
  • Next time please only give information that is relevant to your question. The whole bunch of code and most of the description of your assignment has nothing to do with your question. Commented Jul 12, 2013 at 4:34

1 Answer 1

15

You can use enumerations for the same purpose which will restrict you to use only specified values. Declare your Department enum as follows

public enum Department {

    Accounting, Marketting, Human_Resources, Information_Systems

}

You 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 while creating employee, you can use

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

EDIT as suggested by Adrian Shum and of course because it is a great suggestion.

  • The enums are constants thats why its good to be declared in capital letters according to java conventions.
  • But we don't want the capital representation of the enums to be seen so we can create enum constructors and pass readable info to it.
  • We wil modify enum to include 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 are creating an Employee object as follows and using it,

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

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

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

3 Comments

Oh Wow...that makes more sense now. Thank you so much!
This is an elegant solution to this problem.
just a suggestion, in Java convention, naming of enum should follow constants, that means, the values should be ACCOUNTING, MARKETING, HUMAN_RESOURCES, INFORMATION_SYSTEMS. You may add a method for the enum to provide a human readable name if you want.

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.