How do I use Java getters and setters with a collection of data without explicitly typing out the attributes for each item?

StackOverflow https://stackoverflow.com/questions/19554304

  •  01-07-2022
  •  | 
  •  

Question

I am very new to Java and to programming in general, and I have an assessment to complete where I load employees (with name, age, and department attributes; department can be only one of four enumerated values) into a program that will sort them by age and tell if the age is a prime number. The assignment requires Company, Department, and Employee classes. I am confident that I can figure out age/prime components — I know how to google for algorithms. What I am struggling with is putting all the discrete pieces into a cohesive whole.

Here is what I have so far. I've put in one employee, but the way I'm doing it seems completely inelegant and inefficient. I am sure there is a better way, but I've hit a mental block.

EDIT: as was pointed out below, I was unclear. What I am asking help with is populating the data structure.

Company class:

public class Company {

static Employee one = new Employee();



public static void main(String[] args) {

    one.setName("Counting Guru");
    one.setAge(55);
    one.setDepartment(DepartmentList.ACCOUNTING);


}

}

DepartmentList class:

import java.util.EnumMap;
import java.util.Map;
import java.util.Set;
public enum DepartmentList {
ACCOUNTING, MARKETING, HUMANRESOURCES, INFORMATIONSYSTEMS;




public static void main(String[] args) {
Map<DepartmentList,String> 
enumMap=new EnumMap<DepartmentList,String>(DepartmentList.class);
enumMap.put(DepartmentList.ACCOUNTING, "Accounting");
enumMap.put(DepartmentList.MARKETING, "Marketing");
enumMap.put(DepartmentList.HUMANRESOURCES, "Human Resources");
enumMap.put(DepartmentList.INFORMATIONSYSTEMS, "Information Systems");

 Set<DepartmentList> keySet = enumMap.keySet();
 for (DepartmentList department : keySet) {
     String value = enumMap.get(department);
     System.out.println("ENUMMAP VALUE:"+value);
 }
}

}

Employee class:

public class Employee {

String empName;
int empAge;
DepartmentList empDept;


Employee() {

}

public String getName() {
    return empName;
}



public void setName(String name) {
    this.empName = name;
}



public int getAge() {
    return empAge;
}



public void setAge(int age) {
    this.empAge = age;
}



public DepartmentList getDepartment() {
    return empDept;
}



public void setDepartment(DepartmentList department) {
    this.empDept = department;
}

public Employee(String empName, int empAge, DepartmentList empDept){

}


}

I also have a Department class, but it's currently empty.

Am I on the right track? Can someone give me a nudge? Thank you!

Was it helpful?

Solution

Don't hard-code the data inside the Java program. Put the data in a file and write methods to load the data.

If you MUST hardcode the data in the program, use something like this sample:

public class Employee
{
    String name;
    int age;
    public Employee(String name, int age)
    {
        this.name = name;
        this.age  = age;
    }
    // getters, setters, etc.
}

In the main program

private static Employee[] empData = 
{
    new Employee("John Smith", 50),
    new Employee("Fred Jones", 25),
    .
    .
    .
};

Now you have a static array of Employee objects that you can "load" into your data structure.

OTHER TIPS

If you're asking if there is something like a property in Java, no, there isn't (at least not yet). If you're asking how to populate your objects something like an IOC container, like Spring, would be a better choice.

Now as it comes to your code you have two main methods in two different classes. Only one will be called. If you want to create a static instance you will be better do

static Employee one = new Employee("Counting Guru", 55, DepartmentList.ACCOUNTING);

or

static Employee one = new Employee();
static {
    one.setName("Counting Guru");
    one.setAge(55);
    one.setDepartment(DepartmentList.ACCOUNTING);
}

When it comes to the enum then you'll better define a constructor for it

public enum DepartmentList {
    ACCOUNTING("Accounting"), MARKETING("Marketing");

    private String displayName;

    public DepartmentList(String displayName) {
        this.displayName = displayName;
    }

    public String getDisplayName() {
        return diplayName;
    }
 }

In the Employee constructor you need to assign the field values to the ones received as arguments.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top