Reputation: 23
I hava below task. I stuck on the problem a). Create classes, which describe employees with hourly wage and fixed payment. Give your suggestions about relations between classes. Implement method for calculating the average monthly salary. For employees with hourly wage use next formula: “average monthly salary= 20.8*8* hourly rate”, for employees with fixed payment – “average monthly salary= fixed monthly payment”. Write well commented code for solving next problems a) Sort the collection of employees in descending order by the average monthly salary. In the case of equal salary – by the name. Write ID, name and monthly salary for all employees from collection. b) Write information about first five employees from collection (problem a). c) Write ID of three last employees from collection (problem b). d) Write code for reading and writing collection of these objects from (into) file. e) Write code for handling the incorrect format of incoming file.
I have created below classes, but I have no idea how to sort different objects from different classes. Please help me!!!!
import java.util.*;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<EmployeeFixedPayment> coll = new ArrayList<Employee>();
EmployeeHourlyWage a = new EmployeeHourlyWage("Edd", "Goo", 23, 4);
EmployeeHourlyWage b = new EmployeeHourlyWage("Tedd", "Foo", 2, 5);
EmployeeHourlyWage c = new EmployeeHourlyWage("Bob", "Bee", 4, 2);
EmployeeHourlyWage d = new EmployeeHourlyWage("Kate", "See", 2, 5);
EmployeeFixedPayment e = new EmployeeFixedPayment("Lisa", "Lee", 7, 500);
EmployeeFixedPayment f = new EmployeeFixedPayment("Mike", "Ree", 10,
450);
EmployeeFixedPayment g = new EmployeeFixedPayment("Izia", "Kurz", 13,
1000);
EmployeeFixedPayment j = new EmployeeFixedPayment("Aisha", "Moore", 20,
800);
coll.add(a);
coll.add(b);
coll.add(c);
coll.add(d);
coll.add(e);
coll.add(f);
coll.add(g);
coll.add(j);
Collections.sort(coll);
// System.out.println(coll.size());
for (Employee i : coll) {
System.out.print(i.secondName + " ");
}
}
}
public class Employee {
String firstName;
String secondName;
int id;
public Employee(String firstName, String secondName,int id){
this.firstName = firstName;
this.secondName = secondName;
this.id = id;
}
public void printEmployee(){
System.out.println(firstName+" "+secondName+" "+id);
}
public String getSecondName() {
return secondName;
}
public void setSecondName(String secondName) {
this.secondName = secondName;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
}
public class EmployeeFixedPayment extends Employee {
double fixedPayment;
public EmployeeFixedPayment(String firstName, String secondName, int id,
double salary) {
super(firstName, secondName, id);
fixedPayment = salary;
}
public double getSalary() {
return fixedPayment;
}
public void setSalary(double salary) {
fixedPayment = salary;
}
}
public class EmployeeHourlyWage extends Employee {
Double hourlyWage;
public EmployeeHourlyWage(String firstName, String secondName, int id, double hourlyRate) {
super(firstName, secondName, id);
hourlyWage = 20.8*8*hourlyRate;
}
public double getWage(){
return hourlyWage;
}
public void setWage(double rate) {
hourlyWage = 20.8*8*rate;
}
}
Upvotes: 1
Views: 2720
Reputation: 311843
The trick here is to pull the relevant information (i.e., the monthly salary) up to a base class (Employee
), so you can write a Comparator
for it.
So first, let's pull the relevant method up to Employee
. Since Employee
does not contain the logic to calculate its salary, that'd make the class abstract
:
public abstract class Employee {
public abstract double getMonthlySalary();
// Rest of the members and methods you declared
}
public class EmployeeFixedPayment extends Employee {
double fixedPayment;
@Override
public double getMonthlySalary() {
return fixedPayment;
}
// Rest of the members, ctots, etc. you declared
}
public class EmployeeHourlyWage extends Employee {
double hourlyWage;
@Override
public double getMonthlySalary() {
return hourlyWage * 20.8 * 8;
}
// Rest of the members, ctots, etc. you declared
}
Now that all Employee
s expose their salary in a coherent interface, they can be sorted by implementing a Comparator
:
public class EmployeeComparator implements Comparator<Employee> {
@Override
public int compare (Employee e1, Employee e2) {
// Note we're comparing e2 to e1 to get a descneding effect
int salaryCompare = Double.compare(e2.getMonthlySalary(), e1.getMonthlySalary());
if (salaryCompare != 0) {
return salaryCompare;
}
// If the salaries are equal, compare names
return e1.getName().compareTo(e2.getName());
}
}
Now, to put it all together, we just sort the List
with our custom Comparator
:
List<Employee> coll = new ArrayList<>();
Collections.sort(coll, new EmployeeComparator());
Upvotes: 0
Reputation: 16844
Implement the Comparable
interface for Employee
. This way they get sortet the right way when you call Collections.sort(coll)
.
Personally I find the CompareToBuilder
class helpfull for this.
Upvotes: 1