Question: Implement a superclass Appointment and subclasses Onetime, Daily, and Monthly. An appointment has a description (for example, see the dentist) and a date (use int's
Implement a superclass Appointment and subclasses Onetime, Daily, and Monthly. An appointment has a description (for example, see the dentist) and a date (use int's to store the date). Write a method occursOn(int year, int month, int day) that checks whether the appointment occurs on that date. For example, for a monthly appointment, you must check whether the day of the month matches. (Think about what you need to check for the other types of appointments.)
Additionally, you will need to implement a class called Calendar. This will contain an ArrayList of Appointment objects (The list is an instance variable of the Calendar class). You will need to provide the following methods for your Calendar class.
A no argument constructor
public Calendar(){...}
This constructor will initialize your ArrayList instance variable to an empty list.
/**
* A method to add an appointment to the calendar
* @param apt the appointment object to add to the calendar.
*/
public void add(Appointment apt) {}
/**
* A method to remove an appointment from the calendar.
* This method uses the occursOn() method from the public
* interface for the Appointment class. Therefore, if parameters
* are entered that occur after a start date for a given Daily
* appointment
* the Daily appointment will be removed as well. (Because occursOn() * willreturn true in this case). This is a limitation we will
* accept for now.
* @param year - the year of the appointment to remove
* @param month - the month of the appointment to remove
* @param day - the day of the appointment to remove
*/
public void remove(int year, int month, int day) {
//this method needs to iterate over your list of appointments
//and remove elements who's occursOn() method return true
//when passed the parameters above.
}
/**
* Method to return a string representation of this Calendar object.
* Overrides the Object method toString (see page 448 in text).
* (also see page 453 Special Topic 9.6)
* @return a String representation of the Calendar object.
*/
public String toString() {
String ret = "";
//this method needs to iterate over your list of appointments
//and construct the return string
//make sure to put each appointment on its own line
//by using
return ret;
}
Note that you also need to create a toString method for your Appointment class. (Your subclasses will inherit this version of the method).
Make sure to add accessors to your Appointment class for getYear, getMonth, getDay, and getDescription.
Tester file (AppointmentDemo.java) is below and posted under that is some sample output from a working project using the AppointmentDemo.java file.
AppointmentDemo.java
/**
* Demonstration of the Calendar and Appointment classes
*/
public class AppointmentDemo {
public static void main(String[] args) {
Calendar calendar = new Calendar();
//create some appointments and add them to our calendar
//note the method calls here imply that
//your Appointment class will need to have a 4 argument constructor
//that accepts year, month, day, and description
//the first call is year:2000, month: 8, day: 13
calendar.add(new Daily(2000, 8, 13, "Brush your teeth."));
calendar.add(new Monthly(2003, 5, 20, "Visit grandma."));
calendar.add(new Onetime(2004, 11, 2, "Dentist appointment."));
calendar.add(new Onetime(2004, 10, 31, "Trick or Treat."));
calendar.add(new Monthly(2004, 11, 2, "Dentist appointment."));
calendar.add(new Onetime(2004, 11, 2, "Dentist appointment."));
//note here we can simply use + calendar because we have
//implemented the toString() method
System.out.println("Before removal of appointment " + " " + calendar);
calendar.remove(2004, 11, 2);
//note that the daily appointment is removed because it occurs on
//11/2/2004 (as well as many other days).
System.out.println("After removal of 11/2/2004 " + " " + calendar);
}
}
Example Output of Tester File:
Before removal of appointment
Daily[Brush your teeth. Date: 8/13/2000]
Monthly[Visit grandma. Date: 5/20/2003]
Onetime[Dentist appointment. Date: 11/2/2004]
Onetime[Trick or Treat. Date: 10/31/2004]
Monthly[Dentist appointment. Date: 11/2/2004]
Onetime[Dentist appointment. Date: 11/2/2004]
After removal of 11/2/2004
Monthly[Visit grandma. Date: 5/20/2003]
Onetime[Trick or Treat. Date: 10/31/2004]
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
