0

我对 Java 和一般编程非常陌生,并且我有一个评估要完成,我将员工(具有姓名、年龄和部门属性;部门只能是四个枚举值之一)加载到对他们进行排序的程序中按年龄并判断年龄是否为质数。该作业需要 Company、Department 和 Employee 类。我相信我可以计算出年龄/主要成分——我知道如何在谷歌上搜索算法。我正在努力解决的是将所有离散的部分组合成一个有凝聚力的整体。

这是我到目前为止所拥有的。我已经安排了一名员工,但我这样做的方式似乎完全不优雅且效率低下。我确信有更好的方法,但我遇到了心理障碍。

编辑:正如下面指出的,我不清楚。我正在寻求帮助的是填充数据结构。

公司等级:

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);


}

}

部门列表类:

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);
 }
}

}

员工等级:

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){

}


}

我也有一个部门类,但它目前是空的。

我在正确的轨道上吗?有人可以轻推我一下吗?谢谢!

4

2 回答 2

1

如果你问 Java 中是否有类似属性的东西,不,没有(至少现在还没有)。如果您询问如何填充您的对象,例如 IOC 容器,例如 Spring,将是一个更好的选择。

现在,当涉及到您的代码时,您在两个不同的类中有两个主要方法。只有一个会被调用。如果你想创建一个静态实例,你会更好

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

或者

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

当涉及到枚举时,您最好为它定义一个构造函数

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

    private String displayName;

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

    public String getDisplayName() {
        return diplayName;
    }
 }

在 Employee 构造函数中,您需要将字段值分配给作为参数接收的字段值。

于 2013-10-23T23:54:14.883 回答
1

不要在 Java 程序中对数据进行硬编码。将数据放在文件中并编写加载数据的方法。

如果您必须对程序中的数据进行硬编码,请使用以下示例:

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

在主程序中

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

现在您有了一个静态的 Employee 对象数组,您可以将其“加载”到您的数据结构中。

于 2013-10-23T23:56:04.093 回答