0

I am trying to write a program that has to retrieve a list of employees that is stored in an Array. my code looks like this;

package Assignment2;
import java.util.Scanner;
public class employeeTest {


    public static void main(String []args)
    {   Scanner in = new Scanner(System.in);
        int menuSel;
        System.out.println("Menu");
        System.out.println("1. Display all staff");
        System.out.println("2. Clear Schedule");
        System.out.println("3. Display Schedule");
        System.out.println("4. Assign shifts to casual employees");
        System.out.println("5. Assign shifts to permanent employees");
        System.out.println("6. Calculate total weekly wages");
        System.out.println("7. Exit program");
        menuSel = in.nextInt();
        switch(menuSel){
        case 1: 

        }

    }




    public static void employeeList(Employeeclass _list[])
    {//this is the array that stores the employees
        _list[0] = new permanentEmployee("Joshua", "Power", "POW30310713", 5, 812.47);
        _list[1] = new casualEmployee("Joshua", "Power", "POW12345678", 5, 812.47, 56.00);
        _list[2] = new casualEmployee("Robyn", "Power", "ROB12345678", 3, 5.0, 00.00);
        _list[3] = new permanentEmployee("Joshua", "Power", "POW12345678", 5, 812.47);
        _list[4] = new permanentEmployee("Joshua", "Power", "POW12345678", 5, 812.47);
        _list[5] = new casualEmployee("Robyn", "Power", "ROB12345678", 3, 5.0, 00.00);
        _list[6] = new casualEmployee("Joshua", "Power", "POW12345678", 5, 812.47, 23.0);
        _list[7] = new casualEmployee("Robyn", "Power", "ROB12345678", 3, 5.0, 00.00);
        _list[8] = new casualEmployee("Robyn", "Power", "ROB12345678", 3, 5.0, 00.00);
        _list[9] = new casualEmployee("Robyn", "Power", "ROB12345678", 3, 5.0, 00.00);
    }
    public static void listEmployees(Employeeclass _list[])
    {
        for (int i=0; i<_list.length; i++)
            System.out.println(_list[i].toString());

    }

    {

        Employeeclass list[] = new Employeeclass[10];
        employeeList(list);
        listEmployees(list);

    }

}
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

This solution implies you have retrieved an unsorted list of employeees..be it a list of all employees or a list of casual employees..

Take a look as the java.util.Comparator class. It's used in situations like this..in fact the java.util.Collections has a method to sort using one (https://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)).

You can sort based on any class property you'd like.

Here's an example: How to use Comparator in Java to sort

Community
  • 1
  • 1
Slihp
  • 763
  • 5
  • 12