Сделать абстрактный базовый класс Appointment
вместе с методами occursOn
. Также удалите переменную date
, так как некоторым подклассам это не понадобится:
public abstract class Appointment {
private String description;
public Appointment(String description)
this.description=description;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public abstract boolean occursOn(int year, int month, int day) throws
ParseException;
}
Теперь мы можем приступить к созданию подклассов, в которых будет реализован метод occursOn
. Для подкласса Onetime
он будет в основном таким же, как ваш исходный класс Appointment
, поэтому я не буду сейчас это писать. Вместо этого давайте взглянем на Monthly
, например:
public class Monthly extends Appointment {
private String description;
private int onThisDay; //on this day of the month is the appointment
public Appointment(String description, int onThisDay) {
this.description=description;
this.onThisDay = onThisDay;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
//override tostring as well
@Override
public String toString() {
return "Your " + description + " appointment is on the " +
onThisDay + ". day of the month";
}
//implement our abstract method, month and year params have no importance here
public boolean occursOn(int year, int month, int day) throws
ParseException {
return day == onThisDay; //just check if day matches
}
}
Что касается того, как заполнить список встреч, вот оно:
List<Appointment> appointments = new ArrayList<>();
//2 appointments for example
appointments.addAll(new Onetime("Doctor",2019,07,21), new Monthly("Dentist",11));
appointments.forEach(app -> System.out.println(app.toString())); //print all