2015-09-27 134 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