Hi & thanks in advance for any help offered.
I'm trying to implement an Office class that contains an inner class: WorkerNode. The WorkerNode class has a name attribute (String) and WorkerNode attributes for boss, peer and subordinate. The attributes of Office are manager and current which are WorkerNode references. The manager refers to the entry point of the structure and current is the current node in the structure. For simplicity, i'm going to try to limit it to 3 levels and assume that the names are unique.
I've put together a Office class that containing main and provided the code I've worked on so far.
Ideally, the result would display the following:
The name, boss’s name and subordinate names for the current object e.g if the current object is Amy then should display
Amy reports to Barry
Harry
Fiona
I'm really struggling with dynamic linked lists and any help offered would be greatly appreciated.
Thanks
Pip
Office class:
public class Office { public static void main(String[] args) { String name=Input.getString("input the manager's name: "); Office office=new Office(name); int option; boolean found; do{ System.out.println("0: quit"); System.out.println("1: add subordinate to current"); System.out.println("2: find worker"); System.out.println("3: display current"); System.out.println("4: display office"); option=Input.getInteger("input option: "); switch(option){ case 1: name=Input.getString("input the subordinate's name: "); office.addSubordinate(name); break; case 2: name=Input.getString("input the worker's name: "); found=office.findWorker(name); if(!found) System.out.println("not found"); break; case 3: office.displayCurrent(); break; case 4: office.displayOffice(); break; } }while(option!=0); } }
Input Class:
import java.io.*; public class Input{ private static BufferedReader input=new BufferedReader (new InputStreamReader(System.in)); public static Character getCharacter(String prompt){ Character value; System.out.print(prompt); try{ value=Input.input.readLine().charAt(0); } catch(Exception error){ // error condition value=null; } return value; } public static Double getDouble(String prompt){ Double value; System.out.print(prompt); try{ value=Double.parseDouble(Input.input.readLine()); } catch(Exception error){ // error condition value=null; } return value; } public static Integer getInteger(String prompt){ Integer value; System.out.print(prompt); try{ value=Integer.parseInt(Input.input.readLine()); } catch(Exception error){ // error condition value=null; } return value; } public static String getString(String prompt){ String string; System.out.print(prompt); try{ string=Input.input.readLine(); } catch(Exception error){ // error condition string=null; } return string; } }