Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

§ In BlueJ, create “New Project” named LASTNAME-company Use good programming sty

ID: 3574869 • Letter: #

Question

§ In BlueJ, create “New Project” named LASTNAME-company

Use good programming style and Javadoc documentation standards

Create a “New Class…” named Company to store the employee mappings of employee names (Key) to employee ids (Value)

Declare and initialize class constants MIN and MAX to appropriate values

Declare and instantiate a Random randomGenerator field object

Declare a String field named name for the company’s name

Declare a HashMap<String, String> field named employees

Add a constructor with only 1 parameter named name and:

Assign name field to name parameter after trim, uppercase & replaceAll   (1 or more ANY white space regular expression with just 1 single space)

Initialize employees field by creating an instance of that object

Add accessors getName( ) and getEmployees( )

Add accessor getTotalNumberEmployees( ) to return the total number of mappings in the collection of the employees field

Add formatString(String origString) to return a formatted origString after trim, uppercase, and replaceAll in between white spaces (with single space)

MUST first check if origString is null (which just returns an empty String)

Add method String generateId(String name) to return the employee id generated by taking the first letter of each word in name parameter AND adding a random 3-digit integer between 100-999 (inclusive of both ends)

MUST .split the parameter name into a String[ ] nameArray with regex taking into account multiple white spaces in between words in name

MUST use a for loop to get the first letter in each word in nameArray

MUST use class constants MIN & MAX (NO hard-code) to generate (using .nextInt) the random number used for the id in the range [MIN – MAX]

Add addEmployee(String inputName) & removeEmployee(String inputName) both with void returns and:

Check if (trimmed) inputName is empty or null, print “Name is INVALID”

MUST use formatString for inputName (remember Strings are immutable)

Print “Existing: <name>” (if name already exists for addEmployee)

OR …“Non-existing: <name>” (if non-existing for removeEmployee)

MUST use generateId (for addEmployee) to generate employee id to .put

MUST print heading with name & id, if employee add/delete is successful

Add void removeIds(String id) to remove ALL mappings in employees where the value matches the id search parameter and:

MUST use a local variable to keep track if a match was found

MUST also have check if id parameter is null or empty

MUST use formatString to format the id input parameter

MUST use a for loop, .keySet and .iterator to iterate thru all employees

Check if each employee id value is equal (ignoring case) to the search id

MUST use the Iterator.remove to remove id matching mappings

MUST print heading with name & id for EACH removed employee

Only after the entire search is completed (THUS … outside of loop), check if no matches found and print “NO employees with id: <id>”

Add void listEmployees() to print ALL employee names (Key) & ids (Value):

MUST always print the heading “Employees for <name>:”

MUST use getTotalNumberEmployees or .isEmpty to check if NO entries in employees exist and prints“NO employees”

MUST use a for-each loop and .keySet to iterate thru all employees

MUST print employee in format “     <name> : <id>” (w/ leading spaces)

§ In BlueJ, create “New Project” named LASTNAME-company

Use good programming style and Javadoc documentation standards

Create a “New Class…” named Company to store the employee mappings of employee names (Key) to employee ids (Value)

Declare and initialize class constants MIN and MAX to appropriate values

Declare and instantiate a Random randomGenerator field object

Declare a String field named name for the company’s name

Declare a HashMap<String, String> field named employees

Add a constructor with only 1 parameter named name and:

Assign name field to name parameter after trim, uppercase & replaceAll   (1 or more ANY white space regular expression with just 1 single space)

Initialize employees field by creating an instance of that object

Add accessors getName( ) and getEmployees( )

Add accessor getTotalNumberEmployees( ) to return the total number of mappings in the collection of the employees field

Add formatString(String origString) to return a formatted origString after trim, uppercase, and replaceAll in between white spaces (with single space)

MUST first check if origString is null (which just returns an empty String)

Add method String generateId(String name) to return the employee id generated by taking the first letter of each word in name parameter AND adding a random 3-digit integer between 100-999 (inclusive of both ends)

MUST .split the parameter name into a String[ ] nameArray with regex taking into account multiple white spaces in between words in name

MUST use a for loop to get the first letter in each word in nameArray

MUST use class constants MIN & MAX (NO hard-code) to generate (using .nextInt) the random number used for the id in the range [MIN – MAX]

Add addEmployee(String inputName) & removeEmployee(String inputName) both with void returns and:

Check if (trimmed) inputName is empty or null, print “Name is INVALID”

MUST use formatString for inputName (remember Strings are immutable)

MUST use .containsKey to see if inputName key exists in employees and:

Print “Existing: <name>” (if name already exists for addEmployee)

OR …“Non-existing: <name>” (if non-existing for removeEmployee)

MUST use generateId (for addEmployee) to generate employee id to .put

MUST print heading with name & id, if employee add/delete is successful

Add void removeIds(String id) to remove ALL mappings in employees where the value matches the id search parameter and:

MUST use a local variable to keep track if a match was found

MUST also have check if id parameter is null or empty

MUST use formatString to format the id input parameter

MUST use a for loop, .keySet and .iterator to iterate thru all employees

Check if each employee id value is equal (ignoring case) to the search id

MUST use the Iterator.remove to remove id matching mappings

MUST print heading with name & id for EACH removed employee

Only after the entire search is completed (THUS … outside of loop), check if no matches found and print “NO employees with id: <id>”

Add void listEmployees() to print ALL employee names (Key) & ids (Value):

MUST always print the heading “Employees for <name>:”

MUST use getTotalNumberEmployees or .isEmpty to check if NO entries in employees exist and prints“NO employees”

MUST use a for-each loop and .keySet to iterate thru all employees

MUST print employee in format “     <name> : <id>” (w/ leading spaces)

Explanation / Answer

Please find below the java program with comments:

import java.util.Scanner; //import section
import java.util.Set;
import java.util.Map;
import java.util.TreeSet;
import java.util.Iterator;
import java.util.TreeMap;


public class Company {                   //public class company declared
public static void main(String[] args) {
TreeMap<String, Employee> firstAndLast =
new TreeMap<String, Employee>();
TreeMap<Integer, Employee> idNumber =
new TreeMap<Integer, Employee>();
TreeMap<Employee, Integer> performanceScale =
new TreeMap<Employee, Integer>();
TreeSet<Integer> sort = new TreeSet<Integer>();

Scanner keyboardInput = new Scanner(System.in);       //Keyboard input accepted here

boolean exit = false;

int choice;
while (exit != true) {
System.out.println("//-----MENU-----//"); //Print Section to printout all options
System.out.println("1. Add an Employee ");
System.out.println("2. Remove an Employee ");
System.out.println("3. Modify performance scale ");
System.out.println("4. Print all the performance scale ");
System.out.println("5. Sort first and last name based on ID ");
System.out.println("6. Exit the program.");
System.out.print("Enter choice: ");

choice = keyboardInput.nextInt();

switch (choice) { //switch cases
case 1:                           //Case 1
addEmployee(firstAndLast, idNumber, performanceScale);
break;
case 2:                           //Case 2
removeEmployee(firstAndLast, idNumber, performanceScale);
break;
case 3:                           //Case 3
modifyPerformanceScale(idNumber, performanceScale);
break;
case 4:                           //Case 4
printAllperfScale(performanceScale);
break;
case 5:                           //Case 5
printLastNameAscending(firstAndLast, idNumber);
break;
case 6:                           //Case 6
exit = true;
System.out.println("Exiting program..."); /Program will exit
break;
default:
System.out
.println("Please choose a number from 1 - 5 from the menu."); //Print Section
}
}// end while

} // end main

public static void addEmployee(TreeMap<String, Employee> firstAndLastMap, //addemployee() finction
TreeMap<Integer, Employee> idNumberMap,
TreeMap<Employee, Integer> performanceScale) {
Scanner keyboardInput = new Scanner(System.in);
String firstName;
String lastName;
int id;
int perfScale;

System.out.print("Enter first name for the Employee: "); //Enter the first name here
firstName = keyboardInput.nextLine();
System.out.print("Enter last name for the Employeer: ");
lastName = keyboardInput.nextLine();
System.out.print("Enter ID number of the Employee: ");
id = keyboardInput.nextInt();
System.out.print("Enter Performance Scale rating between 1 to 5: ");
perfScale = keyboardInput.nextInt();

Employee addEmployee = new Employee(lastName, firstName, id, perfScale);   //Add the employees here  

firstAndLastMap.put(lastName + ", " + firstName, addEmployee);
idNumberMap.put(id, addEmployee);
//changed - Added(addEmployee,perfScale) from put(perfScale)
performanceScale.put(addEmployee, perfScale);

}

public static void removeEmployee(TreeMap<String, Employee> firstAndLastMap,
TreeMap<Integer, Employee> idNumberMap,
TreeMap<Employee, Integer> performanceScale) {

Scanner keyboardInput = new Scanner(System.in);
String firstName;
String lastName;
int id;
System.out.print("Enter First name of Employee you want to remove: ");
firstName = keyboardInput.nextLine();
System.out.print("Enter last name of Employee you want to remove: ");
lastName = keyboardInput.nextLine();

System.out.print("Enter ID number of Employee you want to remove: ");
id = keyboardInput.nextInt();
//System.out.println();               // Print section


firstAndLastMap.remove(lastName + ", " + firstName);
idNumberMap.remove(id);
//This should be remove(perfScale)

}
//To change TreeMap<String, emp...to <Integer, emp
public static void modifyPerformanceScale(TreeMap<Integer, Employee> idNumber,
TreeMap<Employee, Integer> performanceScale) {                       //modifyPerformanceScale() declared
System.out.print("Enter ID: ");
Scanner keyboardInput = new Scanner(System.in);                       //input through the keyboard

int idNumber1;
int modScale;
idNumber1 = keyboardInput.nextInt();

System.out.print("Enter the number you want to change to: ");
modScale = keyboardInput.nextInt();                           //input through the keyboard

Employee employee = idNumber.get(idNumber1);                       //employee details
performanceScale.put(employee, modScale);
}
       
public static void printAllperfScale(TreeMap<Employee,Integer> performanceScale) {       //printAllperfScale() declared here
Set Employee1 = performanceScale.entrySet();
Iterator itr1 = Employee1.iterator();

while (itr1.hasNext()) {
Map.Entry me = (Map.Entry) itr1.next();
System.out.println(me.getValue());
}
}


public static void printLastNameAscending(TreeMap<String, Employee> LastName,
TreeMap<Integer, Employee>idNumber) {                       //printLastNameAscending() method declared
Set Employee1 = LastName.entrySet();                           //set employee details here
Set Employee2 = idNumber.entrySet();
Iterator itr1 = Employee1.iterator();
Iterator itr2 = Employee2.iterator();

while (itr1.hasNext() && itr2.hasNext()) {
Map.Entry me = (Map.Entry) itr1.next();
Map.Entry be = (Map.Entry) itr2.next();
System.out.print(me.getValue()+ " ID: ");
System.out.println(be.getValue());
}

}

}