Modify EmployeeData\'s compareTo() method so that elements are sorted based on t
ID: 3861803 • Letter: M
Question
Modify EmployeeData's compareTo() method so that elements are sorted based on the employees' department number (deptNum) and ID (emplID). Specifically, employee's should first be sorted in ascending order according to department number first, and those employees within the same department should be sorted in ascending order according to the employee ID.
EmployeeData.java
EmployeeRecords.java
public class EmployeeData implements Comparable<EmployeeData> {
private String firstName; // First Name
private String lastName; // Last Name
private Integer emplID; // Employee ID
private Integer deptNum; // Department Number
EmployeeData(String firstName, String lastName, Integer emplID, Integer deptNum) {
this.firstName = firstName;
this.lastName = lastName;
this.emplID = emplID;
this.deptNum = deptNum;
}
@Override
public int compareTo(EmployeeData otherEmpl) {
String fullName = ""; // Full name, this employee
String otherFullName = ""; // Full name, comparison employee
int comparisonVal = 0; // Outcome of comparison
// Compare based on department number first
comparisonVal = deptNum.compareTo(otherEmpl.deptNum);
// If in same organization, use name
if (comparisonVal == 0) {
fullName = lastName + firstName;
otherFullName = otherEmpl.lastName + otherEmpl.firstName;
comparisonVal = fullName.compareTo(otherFullName);
}
return comparisonVal;
}
@Override
public String toString() {
return lastName + " " + firstName +
" ID: " + emplID +
" Dept. #: " + deptNum;
}
}
EmployeeData.java
EmployeeRecords.java
public class EmployeeData implements Comparable<EmployeeData> {
private String firstName; // First Name
private String lastName; // Last Name
private Integer emplID; // Employee ID
private Integer deptNum; // Department Number
EmployeeData(String firstName, String lastName, Integer emplID, Integer deptNum) {
this.firstName = firstName;
this.lastName = lastName;
this.emplID = emplID;
this.deptNum = deptNum;
}
@Override
public int compareTo(EmployeeData otherEmpl) {
String fullName = ""; // Full name, this employee
String otherFullName = ""; // Full name, comparison employee
int comparisonVal = 0; // Outcome of comparison
// Compare based on department number first
comparisonVal = deptNum.compareTo(otherEmpl.deptNum);
// If in same organization, use name
if (comparisonVal == 0) {
fullName = lastName + firstName;
otherFullName = otherEmpl.lastName + otherEmpl.firstName;
comparisonVal = fullName.compareTo(otherFullName);
}
return comparisonVal;
}
@Override
public String toString() {
return lastName + " " + firstName +
" ID: " + emplID +
" Dept. #: " + deptNum;
}
}
Explanation / Answer
public class EmployeeData implements Comparable<EmployeeData> {
private String firstName; // First Name
private String lastName; // Last Name
private Integer emplID; // Employee ID
private Integer deptNum; // Department Number
EmployeeData(String firstName, String lastName, Integer emplID, Integer deptNum) {
this.firstName = firstName;
this.lastName = lastName;
this.emplID = emplID;
this.deptNum = deptNum;
}
@Override
public int compareTo(EmployeeData otherEmpl) {
int comparisonVal = 0; // Outcome of comparison
// Compare based on department number first
comparisonVal = deptNum.compareTo(otherEmpl.deptNum);
// If in same organization, use emp Id
if (comparisonVal == 0) {
comparisonVal = emplID.compareTo(otherEmpl.emplID);
}
return comparisonVal;
}
@Override
public String toString() {
return lastName + " " + firstName +
" ID: " + emplID +
" Dept. #: " + deptNum;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.