2015-09-27 88 views
-1

我是新来的Java和在员工管理系统工作。我创建了几个类,包括Employee(名称,dob等),Department(部门名称,描述等)。我有一个要求部门必须有2个empoyee和少于10. 任何人都可以告诉我如何使该协会? 系类:Java中的员工管理系统

public class Department { 
    private String departmentName; 
    private String locationofDep; 
Employee emp = new Employee() 


Getter.. setter 
} 

public class Employee { 
     private String empName; 
     private String dob; 


    Getter.. setter 
    } 
+2

哪里是你的代码的员工信息管理员工的长度问题? –

+0

你能告诉我你尝试过的源代码吗? –

+0

看来你以为我是下来的选民。不,我刚刚评论:) –

回答

0

根据这个问题你应该有对象的Employee一个collection。因此您必须创建EmployeeCollectionList,Set等。但是,您不能限制两个值之间的容量。以编程方式,只有在调用Collection的getter方法时,您才能做的事情是throwException。您可以将两个默认值设置为Collection。我不相信这会对你有用。在代码中进行以下修改。

public class Department { 

    private String departmentName; 
    private String locationofDep; 
    //collection of empleyees 
    Set<Employee> employees = new HashSet<>(); 

    //use this method to add employees 
    public void addEmployee(Employee employee) { 
     this.employees.add(employee);; 
    } 

    public Set<Employee> getEmployees() throws Exception { 
     if (this.employees.size() < 2 || this.employees.size() > 10) { 
      throw new Exception("Employees out of capacity"); 
     } 

     return this.employees; 
    } 

    //other getters and setters should be here 
} 

public class Employee { 

    private String empName; 
    private String dob; 

    //put getters and setters 
} 

所以,你可以当你得到使用try-catch