I am working on a dummy hospital database. I have an ArrayList
that has the combination of all possible times that a doctor can theoretically hold an appointment, and another ArrayList
that holds actual registered appointments.
Availability {
int doctorid;
String specialty;
Date date;
int order_of_appointment;
}
//////////
ArrayList<Availability> allTimes;
ArrayList<Availability> busyTimes;
What I want to accomplish is finding the times where doctors are free. Which is the result of (allTimes - busyTimes)
I tried using allTimes.removeAll(busyTimes)
but it didn't remove anything.
I made sure that I am overriding the equals() method in the Availability
class but it still doesn't remove anything.
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Availability)) return false;
Availability that = (Availability) o;
return doctorid == that.doctorid &&
order_of_appointment == that.order_of_appointment &&
Objects.equals(specialty, that.specialty) &&
Objects.equals(date, that.date);
}
Output:
busyTimes = [Availability{doctorid=1, specialty='internal medicine', date=2021-11-02, order_of_appointment=2} ]
allTimes = [Availability{doctorid=1, specialty='internal medicine', date=2021-11-02, order_of_appointment=1} , Availability{doctorid=1, specialty='internal medicine', date=2021-11-02, order_of_appointment=2} , Availability{doctorid=1, specialty='internal medicine', date=2021-11-02, order_of_appointment=3}]
The output I get for freeTimes is identical to allTimes even though I'm expecting it to remove the appointment with order_of_appointment==2.
I am totally clueless on what might be causing this. Any help would be appreciated. Thanks in advance!
CodePudding user response:
You don't show how are you creating the array, or how are you adding the elements.
I did a simple program with ArrayLists and works as expected:
import java.util.*;
class A {
int id;
A(int id) {
this.id = id;
}
@Override
public boolean equals(Object o) {
return o instanceof A && this.id == ((A)o).id;
}
public String toString() {
return String.format("A{id:%s}", id);
}
public static void main(String ... args) {
List<A> a = new ArrayList<A>(Arrays.asList(new A(1), new A(2)));
List<A> b = new ArrayList<A>(Arrays.asList(new A(2), new A(3)));
a.removeAll(b);
System.out.println(a);
}
}
Output:
[A{id:1}]
CodePudding user response:
You need to override equals(Object obj) method in Availability class with your comparison logic.
I implemented it. please check.
import java.util.Date;
public class Availability {
private int doctorId;
private String specialty;
private Date date;
private int orderOfAppointment;
public Availability() {
super();
}
public Availability(int doctorId, String specialty, Date date, int orderOfAppointment) {
super();
this.doctorId = doctorId;
this.specialty = specialty;
this.date = date;
this.orderOfAppointment = orderOfAppointment;
}
public int getDoctorId() {
return doctorId;
}
public void setDoctorId(int doctorId) {
this.doctorId = doctorId;
}
public String getSpecialty() {
return specialty;
}
public void setSpecialty(String specialty) {
this.specialty = specialty;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public int getOrderOfAppointment() {
return orderOfAppointment;
}
public void setOrderOfAppointment(int orderOfAppointment) {
this.orderOfAppointment = orderOfAppointment;
}
@Override
public String toString() {
return "Availability [doctorId=" doctorId ", specialty=" specialty ", date=" date
", orderOfAppointment=" orderOfAppointment "]";
}
@Override
public boolean equals(Object obj) {
boolean returnVal = false;
Availability busyslote = (Availability) obj;
if (this.doctorId == busyslote.doctorId && this.orderOfAppointment == busyslote.orderOfAppointment
&& this.specialty.equalsIgnoreCase(busyslote.specialty) && this.date.equals(busyslote.date)) {
returnVal = true;
} else {
returnVal = false;
}
return returnVal;
}
}
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class AppointmentMain {
public static void main(String[] args) {
List<Availability> allAppointment = new ArrayList<>();
List<Availability> attenedAppointment = new ArrayList<>();
Availability obj1 = new Availability(1, "Internal Medicine", new Date(), 1);
Availability obj2 = new Availability(1, "Internal Medicine", new Date(), 2);
Availability obj3 = new Availability(1, "Internal Medicine", new Date(), 3);
allAppointment.add(obj1);
allAppointment.add(obj2);
allAppointment.add(obj3);
Availability obj4 = new Availability(1, "Internal Medicine", new Date(), 3);
attenedAppointment.add(obj4);
System.out.println("Befour count :" allAppointment.size());
allAppointment.removeAll(attenedAppointment);
System.out.println("After count :" allAppointment.size());
}
}