I’m trying to use the comparable interface to compare the appointment times (String) in the format of dd-mm-yyyy hh:mm
Also it seems that it can’t find the getAppointmentTime method, which is clearly there
In the tester class, I’m guessing I should just use Collection.sort and that’s it.
But these are the errors I’m getting…
HW3\Appointment.java:13: error: Appointment is not abstract and does not override abstract method compareTo(Appointment) in Comparable
public class Appointment implements Comparable<Appointment>
^
HW3\Appointment.java:189: error: name clash: compareTo(Object) in Appointment and compareTo(Appointment) in Comparable have the same erasure, yet neither overrides the other
public int compareTo(Object obj)
^
HW3\Appointment.java:191: error: cannot find symbol
return getAppointmentTime().compareTo(obj.getAppointmentTime());
^
symbol: method getAppointmentTime()
location: variable obj of type Object
Code:
package HW3;
import java.lang.Comparable;
public class Appointment implements Comparable<Appointment>
{
private String serviceType;
private int patientID;
private String patientName;
private String appointmentTime;
private String doctorName;
private String symptoms;
public Appointment(String serviceType, int patientID, String patientName, String appointmentTime, String doctorName, String symptoms)
{
this.serviceType = serviceType;
this.patientID = patientID;
this.patientName = patientName;
this.appointmentTime = appointmentTime;
this.doctorName = doctorName;
this.symptoms = symptoms;
}
public String getServiceType()
{
return serviceType;
}
public int getPatientID()
{
return patientID;
}
public String getPatientName()
{
return patientName;
}
public String getAppointmentTime()
{
return appointmentTime;
}
public String getDoctorName()
{
return doctorName;
}
public String getSymptoms()
{
return symptoms;
}
public int compareTo(Object obj)
{
return getAppointmentTime().compareTo(obj.getAppointmentTime());
}
}
>Solution :
Try the below code:
package HW3;
import java.lang.Comparable;
public class Appointment implements Comparable<Appointment> {
private String serviceType;
private int patientID;
private String patientName;
private String appointmentTime;
private String doctorName;
private String symptoms;
public Appointment(String serviceType, int patientID, String patientName, String appointmentTime, String doctorName, String symptoms) {
this.serviceType = serviceType;
this.patientID = patientID;
this.patientName = patientName;
this.appointmentTime = appointmentTime;
this.doctorName = doctorName;
this.symptoms = symptoms;
}
public String getServiceType() {
return serviceType;
}
public int getPatientID() {
return patientID;
}
public String getPatientName() {
return patientName;
}
public String getAppointmentTime() {
return appointmentTime;
}
public String getDoctorName() {
return doctorName;
}
public String getSymptoms() {
return symptoms;
}
@Override
public int compareTo(Appointment obj) {
return getAppointmentTime().compareTo(obj.getAppointmentTime());
}
}
Changes:
Changed the method parameter of compareTo method from Object to Appointment