I have written a Java program that consist of three relevant classes; Employee, Client & Contract. In my main method I have created an array of various instances of all 3 classes as so;
// Create Clients
Client Client1 = new Client(1, "Client 1", 0);
Client Client2 = new Client(2, "Client 2", 0);
// Create Array of Clients
Client[] clients = new Client[] {Client1, Client2};
// Create Contracts
Contract Contract1 = new Contract("Contract 1", 1, 850, 4, 0, 1, 0);
Contract Contract2 = new Contract("Contract 2", 2, 500, 4, 0, 1, 0);
Contract Contract3 = new Contract("Contract 3", 3, 1500, 3, 0, 1, 0);
// Create Array of Contracts
Contract[] contracts = new Contract[] {Contract1, Contract2, Contract3};
// Create Employees
Employee Employee1 = new Employee(1, "Bradley", 0);
Employee Employee2 = new Employee(2, "Patrick", 0);
Employee Employee3 = new Employee(3, "Erin", 0);
Employee Employee4 = new Employee(4, "Jim", 0);
Employee Employee5 = new Employee(5, "Fredrick", 0);
// Create Array of Employees
Employee[] employees = new Employee[] {Employee1, Employee2, Employee3, Employee4, Employee5};
I have a function in Contract.Java called assignContractToEmployeeWithLeastContracts which takes 2 arguments, an array of employees, and contract. The function is:
// Assign contract to employee with least contracts
public void assignContractToEmployeeWithLeastContracts(Employee[] employees, Contract contract) {
// Assign to employee with minimum contracts
int minContract = Integer.MAX_VALUE;
Employee employeeWithMinContracts = null;
for (Employee employee : employees) {
if (employee.getCurrentlyAssignedContracts() < minContract) {
// swap min and employee if true
employeeWithMinContracts = employee;
minContract = employeeWithMinContracts.getCurrentlyAssignedContracts();
}
}
employeeWithMinContracts.assignContract(employeeWithMinContracts, contract);
}
The problem I have is I need to handle what happens if 2 employees both have the same (minimum) number of contracts assigned. How can I pick one of the employees at random if this happens? I have tried implementing a seed within the foreach loop but keep breaking everything.
Any help would be appriciated,
Thanks, B
Aucun commentaire:
Enregistrer un commentaire