Employee cannot be cast to java.lang.Comparable

0

Cześć, z niewiadomego mi powodu nie mogę przesortować tablicy obiektów. Podobno nie są "compareable", jednak nie potrafię odnaleźć żadnego błędu w mojej metodzie compareTo. Czy ktoś jest w stanie oświecić mnie czym jest to spowodowane?

import java.math.*;
import java.util.*;

public class Obiekty 
{
	public static void main(String[] args)
	{

		Employee[] employees = new Employee[6];
		employees[0]= new Employee("Abacki", 5000, 1995, 12, 01);
		employees[1]= new Employee("Babacki", 4500, 1995, 12, 05);
		employees[2]= new Employee("Cabacki", 5500, 1996, 12, 01);
		employees[3]= new Employee("Dabacki", 6000, 1996, 12, 05);
		employees[4]= new Employee("Eabacki" , 6000);
		employees[5]= new Employee("Fabacki", 7000, 1997, 12, 05);
		
		Arrays.sort(employees);
		
		for (Employee e : employees)
		{
			System.out.println(e);
		}		
	}
} 
abstract class Person
{
	protected String name;
	
	public Person(String aName)
	{
		name=aName;
	}
	
	public abstract String getDescription();
	
	public String getName()
	{
		return name;
	}
	
	public int compareTo(Person x)
	{
		return name.compareToIgnoreCase(x.getName());
	}
	
	public static void main(String args[])
	{
		Person TestJednostkowy = new Manager("Andrew Boston", 5000);
		System.out.println(TestJednostkowy.getName());
	}
}

import java.util.Date;
import java.util.GregorianCalendar;
/**
 * @author xyz
 * @version 0.1a beta
 * basic Employee class doing simple stuff 
 */
class Employee extends Person
{
	static private int nextID=1;
	private double salary;
	private int ID=nextID;
	private final Date hireDay;
	//konstruktor
	public Employee(String aName, double aSalary, int year_hired, int month_hired, int day_hired)
	{
		super(aName);
		salary = aSalary;
		GregorianCalendar hired = new GregorianCalendar(year_hired, month_hired - 1, day_hired);
		hireDay = hired.getTime(); //bierzemy aktualny czas z dowolnego obiektu klasy GregorianCalendar
		nextID++;
	}
	
	public Employee(String aName, double aSalary)
	{
		super(aName);
		salary = aSalary;
		GregorianCalendar hired = new GregorianCalendar(); //bierzemy aktualna datę
		hireDay = hired.getTime();
		nextID++;
	}
	
	public Employee(String aName)
	{
		super(aName);
		salary = 0;
		GregorianCalendar hired = new GregorianCalendar(); //bierzemy aktualna datę
		hireDay = hired.getTime();
		nextID++;
	}
	

	public String getDescription()
	{
		return "Pracownik o imieniu " + this.getName();
	}
	
	public double getSalary()
	{
		return salary;
	}
	
	public Date getHireDay()
	{
		return (Date) hireDay.clone();
	}
	
	public void riseSalary(double Percent)
	{
		salary += salary * Percent / 100;
		if (salary<0) salary=0;
	}
	
	public int getID()
	{
		return ID;
	}

	public int hashCode()
	{
		return 9 * name.hashCode() + 6 * (int) salary + 3 * hireDay.hashCode() + ID * 2;
	}
	
	static public int getNextID() /** @since 0.1a */
	{
		return nextID;
	}

	public String toString()
	{
		String wynik = getClass().getName() + "[ID=" + this.ID + ", name=" + this.name + ", salary=" + this.salary + ", hireDay=" +this.hireDay +"]";
		return wynik;
	}
	
	

	public static void main(String args[])
	{
		Employee TestJednostkowy = new Employee ("Kasia", 12000, 1995, 12,01);
		System.out.println(TestJednostkowy.hashCode() + " " + TestJednostkowy.getName() + " earns " +TestJednostkowy.getSalary());
		System.out.println(TestJednostkowy.toString());
	}
}

Komunikat błędu:

Exception in thread "main" java.lang.ClassCastException: Employee cannot be cast to java.lang.Comparable
at java.util.ComparableTimSort.countRunAndMakeAscending(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at Obiekty.main(Obiekty.java:19)

Z góry dzięki za wskazówki :)

1

Employee nie implementuje interfejsu Comparable, a jest to potrzebne do sortowania

1

Masz dwa wyjścia, implementacja interfejsu Comparable w Twojej klasie, lub posortowanie z użyciem Comparator'a

1 użytkowników online, w tym zalogowanych: 0, gości: 1