From e3d3182d8c58e05543b56bac0f2f17f54d4c361a Mon Sep 17 00:00:00 2001 From: Lisa Date: Fri, 17 May 2019 18:29:26 +0200 Subject: [PATCH] Initial commit for 6.3 --- src/main/java/_6/_3/DuplicateIdException.java | 9 ++ src/main/java/_6/_3/InvalidIdException.java | 16 +++ src/main/java/_6/_3/StarkEnterprises.java | 115 ++++++++++++++++++ src/main/java/_6/_3/UnknownIdException.java | 9 ++ src/main/java/provided/_6/_3/Company.java | 73 +++++++++++ src/main/java/provided/_6/_3/StarkTest.java | 28 +++++ 6 files changed, 250 insertions(+) create mode 100644 src/main/java/_6/_3/DuplicateIdException.java create mode 100644 src/main/java/_6/_3/InvalidIdException.java create mode 100644 src/main/java/_6/_3/StarkEnterprises.java create mode 100644 src/main/java/_6/_3/UnknownIdException.java create mode 100644 src/main/java/provided/_6/_3/Company.java create mode 100644 src/main/java/provided/_6/_3/StarkTest.java diff --git a/src/main/java/_6/_3/DuplicateIdException.java b/src/main/java/_6/_3/DuplicateIdException.java new file mode 100644 index 0000000..558c838 --- /dev/null +++ b/src/main/java/_6/_3/DuplicateIdException.java @@ -0,0 +1,9 @@ +package _6._3; + +public class DuplicateIdException extends InvalidIdException { + public DuplicateIdException() { + } + public DuplicateIdException(String message,int id) { + super(message,id); + } +} diff --git a/src/main/java/_6/_3/InvalidIdException.java b/src/main/java/_6/_3/InvalidIdException.java new file mode 100644 index 0000000..771e896 --- /dev/null +++ b/src/main/java/_6/_3/InvalidIdException.java @@ -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; + } +} diff --git a/src/main/java/_6/_3/StarkEnterprises.java b/src/main/java/_6/_3/StarkEnterprises.java new file mode 100644 index 0000000..3d549a5 --- /dev/null +++ b/src/main/java/_6/_3/StarkEnterprises.java @@ -0,0 +1,115 @@ +package _6._3; + +import java.util.*; +import provided._6._3.Company; + +public class StarkEnterprises implements Company { + private Map employee = new HashMap<>(); + private Map projects = new HashMap<>(); + private Map> 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 getEmployees() { + return collectKeysSortedByValue(employee, new EntryValueComparator<>()); + } + + private class EntryValueComparator> implements Comparator> { + @Override + public int compare(Map.Entry o1, Map.Entry o2) { + return o1.getValue().compareTo(o2.getValue()); + } + } + + public Collection getProjectsForEmployee(int employeeId) throws UnknownIdException { + if (!employee.containsKey(employeeId)) { + throw new UnknownIdException("Employee does not exist", employeeId); + } + + Set projectIds = projectsPerEmployee.get(employeeId); + Map projectsById = new HashMap<>(); + for (Integer projectId : projectIds) { + projectsById.put(projectId, projects.get(projectId)); + } + return collectKeysSortedByValue(projectsById, new EntryValueComparator<>()); + } + + private static Collection collectKeysSortedByValue(Map map, Comparator> comparator) { + //return map.entrySet().stream() + // .sorted(comparator) + // .map(Map.Entry::getKey) + // .collect(Collectors.toList()); + List> entryList = new ArrayList<>(map.entrySet()); + entryList.sort(comparator); + List ids = new ArrayList<>(); + for (Map.Entry 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(); + } +} diff --git a/src/main/java/_6/_3/UnknownIdException.java b/src/main/java/_6/_3/UnknownIdException.java new file mode 100644 index 0000000..57642c1 --- /dev/null +++ b/src/main/java/_6/_3/UnknownIdException.java @@ -0,0 +1,9 @@ +package _6._3; + +public class UnknownIdException extends InvalidIdException { + public UnknownIdException() { + } + public UnknownIdException(String message,int id) { + super(message,id); + } +} diff --git a/src/main/java/provided/_6/_3/Company.java b/src/main/java/provided/_6/_3/Company.java new file mode 100644 index 0000000..4e91f22 --- /dev/null +++ b/src/main/java/provided/_6/_3/Company.java @@ -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 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 getProjectsForEmployee(int employeeId) throws UnknownIdException; + +} diff --git a/src/main/java/provided/_6/_3/StarkTest.java b/src/main/java/provided/_6/_3/StarkTest.java new file mode 100644 index 0000000..25e89c8 --- /dev/null +++ b/src/main/java/provided/_6/_3/StarkTest.java @@ -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()); + } + } + +}