The goals of this assignment are to:
In this assignment you will be writing a Java program that creates and manipulates a Company reporting hierarchy tree. Every node in the tree represents an employy with some specific features, including name, employee ID, data of joining and title. Every edge in the tree represents supervisor-employee relationship. The main program will get input from file and send its output to the console.
An Employee class is provided for you (see Employee.java). The Employee class represents a single employee that keeps track of the name (as a String), id (as an int), date of joining (as a String) and title (as a String). The Employee class has the following constructor and methods:
Constructor | Description |
---|---|
Employee(String name, int id, String dateOfJoining, String title) | Constructs an Employee with the required details. |
Method | Description |
String getName() | Return the name of the employee. |
int getId() | Return the id of the employee |
String getDateOfJoining() | Return the date of joining of the employee |
String getTitle() | Return the title of the employee |
You may not modify the Employee class.
A TreeNode class is also provided for you (see TreeNode.java). The TreeNode class represents a single node in the company hierarchy tree. A node has the infomation of a employee (as a Employee) and also the infomation about the employee's supervisor as a parent node (as a TreeNode) and information about the employee's workers (other employees who report to current employee) as children nodes (as a List of TreeNodes). The TreeNode class has the following constructor and methods:
Constructor | Description |
---|---|
TreeNode (Employee employee, TreeNode supervisorNode) | Constructs a TreeNode with employee and supervisorNode. |
Method | Description |
Employee getEmployee() | Return the employee in this node |
TreeNode getSupervisor() | Return the reporting supervisor for the employee in this node |
List<TreeNode> getWorkers() | Return the worker list for the employee in this node |
void addWorker(TreeNode workerNode) | Add new worker to this employee |
void updateSupervisor(TreeNode supervisorNode) | Updates supervisor of an employee TreeNode |
void updateEmployee(Employee employee) | Updates employee of the current TreeNode |
You may not modify the TreeNode class.
The CompanyHierarchyException Class
Below are the specifications for this class:
The CompanyHierarchy class stores the root of company hierarchy tree. The CompanyHierarchy class has the
constructor and methods shown in the table below. A shell of this class is also provided for you (see CompanyHierarchy.java), you must implement all the methods.
Note: In addition to the information given below, your CompanyHierarchy
class will need to be able to detect null parameter values and, when a null parameter value is passed, or <0 value is passed for id,
throw a java.lang.IllegalArgumentException. If root of the CompanyHierarchy tree is null, then return an appropriate value.
Constructor | Description |
---|---|
CompanyHierarchy() | Constructs an empty company hierarchy tree. |
Method | Description |
String getCEO() | Get the name of the CEO in this company tree Return null if root is null |
int getNumEmployees() | Return the number of employees in this company tree Return 0 if root is null |
int getMaxLevels() | Return the number of levels in the tree : 0+ values Return 0 if root is null |
Employee getEmployee(int id, String name) | Return the employee details of given employee id and name Return null if no such employee was found If you found a match with employee id, but the name is different, then throw a CompanyHierarchyException with this message: "Incorrect employee name for id!" |
boolean addEmployee(Employee employee, int supervisorId, String supervisorName) | Adds employee as a child to the given supervisor node if supervisor exists on tree
When the root and supervisorName are both null, you should set the employee as the root and return true. Do not throw an IllegalArgumentException for this circumstance alone. If there is no such supervisor, return false If the company hierarchy tree already has this employee, don't do anything and return false; otherwise add the employee and return true If you found that another employee in the CompanyHierarchy tree that has this same id (that is your employee instance will have an ID) then throw CompanyHierarchyException with this message: "Id already used!" If you found a match with supervisor id, but the supervisor name is different, then throw a CompanyHierarchyException with this message: "Incorrect supervisor name for id!" |
boolean contains(int id, String name, String exceptionMessage) | Return true if the company hierarchy tree contains employee with given ID and name, otherwise return false.
If the employee id matches, but name doesn't match, then throw a CompanyHierarchyException with the message as passed by argument exceptionMessage |
boolean removeEmployee(int id, String name) | Removes the given employee(if found on the tree) and updates all the workers to report to the given employee's supervisor
Make sure to update all the required fields due to this supervisor-worker relationship change Returns true or false accordingly If you found a match with employee id, but the name is different, then throw a CompanyHierarchyException with this message: "Incorrect employee name for id!" If asked to remove the root, then throw a CompanyHierarchyException with this message: "Cannot remove CEO of the company!" You may use any methods in the Java's ArrayList<E> class |
boolean replaceEmployee(int id, String name, Employee newEmployee) | Replaces the given employee(if found on the tree) and if the new employee title matches with old employee
Returns true or false accordingly If the new employee If you found that another employee in the CompanyHierarchy tree that has this same id (that is your newEmployee instance will have an ID) then throw CompanyHierarchyException with this message: "Id already used!" If the new employee title doesn't match with the old employee's title, then throw a CompanyHierarchyException with this message: "Replacement title does not match existing title!" |
List<Employee> getEmployeeWithTitle(String title) | Search and return the list of employees with the provided title
If none found return null |
List<Employee> getEmployeeInJoiningDateRange(String startDate, String endDate) | Search and return the list of employees with date of joing within the provided range
If none found return null Assume that mm/dd/yyyy is the format which you have to expect. Use the SimpleDateFormat to parse the date and once parsed use any relevant functions from Date to perform comparison. |
List<Employee> getCoWorkers(int id, String name) | Return the list of employees who are in the same level as the given employee sharing the same supervisor
If you found a match with employee id, but the name is different, then throw a CompanyHierarchyException with this message: "Incorrect employee name for id!" |
List<Employee> getSupervisorChain(int id, String name) | Returns the supervisor list(till CEO) for a given employee
Returns null if employee is not found If you found a match with employee id, but the name is different, then throw a CompanyHierarchyException with this message: "Incorrect employee name for id!" If asked to find supervisor chain of the root, then throw a CompanyHierarchyException with this message: "No Supervisor Chain found for that employee!" |
You may not add any public methods to the CompanyHierarchy class, but you should add private helper methods. Wherever possible, you should write any repititive code inside a private helper function. Two such example functions are already provided on the shell of this class.
A text file storing information of employee information will be read by your program. Each line in the text file (except the first line) corresponds to a employee's information in this format:
The application program, CompanyHierarchyMain, creates and uses a CompanyHierarchy to represent and process information about company employees. The employee information is read from a text file (explained above) and then the program processes user commands.
The CompanyHierarchyMain.java file contains the outline of the CompanyHierarchyMain class. Download this file and use it as the starting point for your CompanyHierarchyMain implementation.
The command line format for the program is:
where FileName is the name of the text file to be processed. See below for more information about command line arguments.
The main method of the CompanyHierarchyMain class does the following:
The command options are:
Command format | Description |
---|---|
c id,name | Print the name(s) of the co-employees(sharing the same supervisor) of the employee with given id and name. Print the names on separate lines. If no such employee is found, display "Employee not found!". If the employee has no co-employee under the same supervisor, display "The employee has no co-workers.". |
d | Display information about the company hierarchy tree by doing the following:
|
e title | Print the name(s) of the employee(s) that has the given title. Print the names on separate lines. If no such employee is found, display "Employee not found!" |
a newid,newname,DOJ,title, supervisorId,supervisorName |
Add a new employee with given details to the company tree. Display "Employee added" if the addition was successful. If there is no such supervisor in the company tree, display "Cannot add employee as supervisor was not found!". If employee already exists in the company tree, then display "Employee already exists!". |
r id,name | Remove the employee with given id and name from the company tree and re-assign the worker's to the removed employee's supervisor. Display "Employee removed" after the removal. If there is no such employee in the company tree, display "Employee not found!". |
s id,name | Print the name(s) of all the supervisors in the supervisor chain of the given employee. Print the names on separate lines. If no such employee is found, display "Employee not found!" |
u id,name,newid,newname,DOJ,title | Replace the employee with give id and name from the company tree with the provided employee details. Display "Employee replaced" after the removal. If there is no such employee in the company tree, display "Employee not found!" |
j startDate,endDate | Print the name(s) of the employee(s) whose date of joining are between startDate and endDate(you may assume that startDate is equal to or before end date). Print the names on separate lines. If no such employee is found, display "Employee not found!" |
x | exit |
You may not add any additional commands to those listed above.
Recall that in Java, when you run a program it is a main method that runs and that main method always has the following header:
The array-of-Strings parameter, args, contains the command-line arguments that are specified when the program is run. If the program is run from a Linux prompt (i.e., not from a programming environment like Eclipse), the command-line arguments are simply typed after the name of the class whose main method is to be executed. For example:
runs the Java interpreter on the main method of the CompanyHierarchyMain class, passing the string "sampleInput.txt" as the command-line argument.
To use command-line arguments when you run a Java program using Eclipse:
After you have read this program page and given thought to the problem we suggest the following steps: