Initial commit for 6.3

generic-observer
Lisa 7 years ago
parent 53a4e4c0ed
commit e3d3182d8c

@ -0,0 +1,9 @@
package _6._3;
public class DuplicateIdException extends InvalidIdException {
public DuplicateIdException() {
}
public DuplicateIdException(String message,int id) {
super(message,id);
}
}

@ -0,0 +1,16 @@
package _6._3;
public class InvalidIdException extends RuntimeException {
private int id;
public InvalidIdException() {
}
public InvalidIdException(String message,int id) {
super(message);
this.id = id;
}
public int getId() {
return id;
}
}

@ -0,0 +1,115 @@
package _6._3;
import java.util.*;
import provided._6._3.Company;
public class StarkEnterprises implements Company {
private Map<Integer, String> employee = new HashMap<>();
private Map<Integer, String> projects = new HashMap<>();
private Map<Integer, Set<Integer>> projectsPerEmployee = new HashMap<>();
@Override
public void addEmployee(int id, String name) throws DuplicateIdException {
if (employee.containsKey(id)) {
throw new DuplicateIdException();
}
employee.put(id, name);
}
@Override
public String getEmployeeName(int id) {
return employee.get(id);
}
@Override
public void addProject(int id, String name) throws DuplicateIdException {
if (projects.containsKey(id)) {
throw new DuplicateIdException();
}
projects.put(id, name);
}
@Override
public String getProjectName(int id) {
return projects.get(id);
}
@Override
public void assignEmployeeToProject(int employeeId, int projectId) throws UnknownIdException {
if (!employee.containsKey(employeeId)) {
throw new UnknownIdException("Employee does not exist", employeeId);
}
if (!projects.containsKey(projectId)) {
throw new UnknownIdException("Project does not exist", projectId);
}
projectsPerEmployee.computeIfAbsent(employeeId, HashSet::new).add(projectId);
}
public void removeEmployeeFromProject(int employeeId, int projectId) throws UnknownIdException {
if (!employee.containsKey(employeeId)) {
throw new UnknownIdException("Employee does not exist", employeeId);
}
if (!projects.containsKey(projectId)) {
throw new UnknownIdException("Project does not exist", projectId);
}
if (projectsPerEmployee.containsKey(employeeId)) {
projectsPerEmployee.get(employeeId).remove(projectId);
}
}
public Collection<Integer> getEmployees() {
return collectKeysSortedByValue(employee, new EntryValueComparator<>());
}
private class EntryValueComparator<T extends Comparable<T>> implements Comparator<Map.Entry<?, T>> {
@Override
public int compare(Map.Entry<?, T> o1, Map.Entry<?, T> o2) {
return o1.getValue().compareTo(o2.getValue());
}
}
public Collection<Integer> getProjectsForEmployee(int employeeId) throws UnknownIdException {
if (!employee.containsKey(employeeId)) {
throw new UnknownIdException("Employee does not exist", employeeId);
}
Set<Integer> projectIds = projectsPerEmployee.get(employeeId);
Map<Integer, String> projectsById = new HashMap<>();
for (Integer projectId : projectIds) {
projectsById.put(projectId, projects.get(projectId));
}
return collectKeysSortedByValue(projectsById, new EntryValueComparator<>());
}
private static <K, V> Collection<K> collectKeysSortedByValue(Map<K, V> map, Comparator<Map.Entry<?, V>> comparator) {
//return map.entrySet().stream()
// .sorted(comparator)
// .map(Map.Entry::getKey)
// .collect(Collectors.toList());
List<Map.Entry<K, V>> entryList = new ArrayList<>(map.entrySet());
entryList.sort(comparator);
List<K> ids = new ArrayList<>();
for (Map.Entry<K, V> entry : entryList) {
ids.add(entry.getKey());
}
return ids;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (Integer employeeId : this.getEmployees()) {
sb.append(this.employee.get(employeeId)).append('[').append(employeeId).append("]: ");
StringJoiner sj = new StringJoiner(" ");
for (Integer projectId : this.projectsPerEmployee.get(employeeId)) {
sj.add(this.projects.get(projectId) + "[" + projectId + "]");
}
sb.append(sj.toString()).append('\n');
}
return sb.toString();
}
}

@ -0,0 +1,9 @@
package _6._3;
public class UnknownIdException extends InvalidIdException {
public UnknownIdException() {
}
public UnknownIdException(String message,int id) {
super(message,id);
}
}

@ -0,0 +1,73 @@
package provided._6._3;
import java.util.Collection;
import _6._3.*;
public interface Company {
/**
* Adds a new employee to the company.
* @param id The id that uniquely identifies the employee
* @param name The name of the employee
* @throws DuplicateIdException If the given id is already in use
*/
void addEmployee(int id, String name) throws DuplicateIdException;
/**
* Returns the name of an employee identified by the given id,
* or null if the id is not known
* @param id The id of the employee
* @return The name of the employee or null if the id is unknown
*/
String getEmployeeName(int id);
/**
* Adds a new project to the company.
* @param id The id that uniquely identifies the project
* @param name The name of the project
* @throws DuplicateIdException If the given id is already in use
*/
void addProject(int id, String name) throws DuplicateIdException;
/**
* Returns the name of a project identified by the given id,
* or null if the id is not known
* @param id The id of the project
* @return The name of the project or null if the id is unknown
*/
String getProjectName(int id);
/**
* Assigns an employee to a project. An employee can be assigned to multiple projects,
* but not to a single project twice (ignore duplicate assignments).
* @param employeeId The id of the employee
* @param projectId The id of the project
* @throws UnknownIdException If either the employee or the project id is unknown
*/
void assignEmployeeToProject(int employeeId, int projectId) throws UnknownIdException;
/**
* Removes an assigned employee from a project. Does nothing if the employee was
* not assigned to the given project in the first place.
* @param employeeId The id of the employee
* @param projectId The id of the project
* @throws UnknownIdException If either the employee or the project id is unknown
*/
void removeEmployeeFromProject(int employeeId, int projectId) throws UnknownIdException;
/**
* Returns an ordered collection of employee ids, sorted by the name of the employees
* @return A list of employee ids, sorted by name
*/
Collection<Integer> getEmployees();
/**
* Returns an ordered collection of project ids that a given employee is assigned to,
* sorted by the name of the projects
* @param employeeId The id of the employee
* @return A list of project ids for the given employee, sorted by name
* @throws UnknownIdException If the employee id is unknown
*/
Collection<Integer> getProjectsForEmployee(int employeeId) throws UnknownIdException;
}

@ -0,0 +1,28 @@
package provided._6._3;
import _6._3.*;
public class StarkTest {
public static void main(String[] args) {
Company stark = new StarkEnterprises();
try {
stark.addEmployee(0, "Tony");
stark.addEmployee(1, "Pepper");
stark.addEmployee(2, "Jarvis");
stark.addProject(0, "Suit");
stark.addProject(1, "Jarvis");
stark.addProject(2, "Jarvis");
stark.addProject(3, "Finances");
stark.assignEmployeeToProject(0, 0);
stark.assignEmployeeToProject(0, 1);
stark.assignEmployeeToProject(1, 3);
stark.assignEmployeeToProject(2, 0);
stark.assignEmployeeToProject(2, 2);
System.out.println(stark);
} catch (InvalidIdException e) {
System.out.println("Invalid ID: " + e.getId());
}
}
}
Loading…
Cancel
Save