In: Computer Science
i have a skeleton class and i have to fill some methods of this class. i am having difficulties especially with the findAppointment method. so this is the problem.
Implement a superclass Appointment and subclasses OneTime,
Daily, and Monthly. An Appointment has a description (for example
“see the dentist”) and a 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. You must write: •
Appointment.java • Daily.java • Monthly.java • OneTime.java
You need to write AppointmentBook.java file – a skeleton and a
sample run of the program is given to you. This file has a main
method
I have done all the first part. the appointment, monthly, daily, OneTime. they work fine. the appointmentBook i havent finished. here is the skeleton. please use ArrayList and i want it in java.
**************AppointmentBook******************.
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
Build on the Appointment hierarchy.
Give the user the option to add new appointments.
The user must specify the type of the appointment and
description, and then, if required, the day or date.
*/
public class AppointmentBook
{ ................ }
/**
Adds a new Appointment object based on user input.
@param in the Scanner to read from.
*/
public void addAppointment(Scanner in)
{ ......................
}
/**
Method to print all appointments on a certain date.
@param in the Scanner to read from.
*/ public void findAppointments(Scanner in)
{ .....................}
// Just to test the appointment book
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
AppointmentBook ab = new AppointmentBook();
System.out.println("Welcome to the Appointment Book");
System.out.println("-------------------------------");
boolean done = false;
while (!done)
{
System.out.print("Appointments: (F)ind, (A)dd, or (Q)uit: ");
String choice = in.next();
if (choice.equals("F") || choice.equals("f"))
{
ab.findAppointments(new Scanner(System.in));
}
else if (choice.equals("A") || choice.equals("a"))
{
ab.addAppointment(new Scanner(System.in));
}
done = choice.equals("Q") || choice.equals("q");
}
System.out.println("Good bye. Have a nice day!");
in.close();
}
}
***********this is the output*********
Welcome to the Appointment Book
-------------------------------
Appointments: (F)ind, (A)dd, or (Q)uit: A
Enter type [(D)aily, (M)onthly, (O)netime] and description: D
Exercise
Appointments: (F)ind, (A)dd, or (Q)uit: A
Enter type [(D)aily, (M)onthly, (O)netime] and description: D Eat
dinner
Appointments: (F)ind, (A)dd, or (Q)uit: A
Enter type [(D)aily, (M)onthly, (O)netime] and description: M Pay
Bills
Enter the day of the appointment: 1
Appointments: (F)ind, (A)dd, or (Q)uit: A
Enter type [(D)aily, (M)onthly, (O)netime] and description: M Pay
Rent
Enter the day of the appointment: 10
Appointments: (F)ind, (A)dd, or (Q)uit: A
Enter type [(D)aily, (M)onthly, (O)netime] and description: O Visit
Patagonia
Enter the date of the appointment (mm dd yyyy) : 12 01 2020
Appointments: (F)ind, (A)dd, or (Q)uit: a
Enter type [(D)aily, (M)onthly, (O)netime] and description: O Visit
Norway
Enter the date of the appointment (mm dd yyyy) : 07 01 2020
Appointments: (F)ind, (A)dd, or (Q)uit: F
Enter the date (mm, dd, yyyy) to search: 09 10 2019
Exercise
Eat dinner
Pay Rent
Appointments: (F)ind, (A)dd, or (Q)uit: F
Enter the date (mm, dd, yyyy) to search: 07 01 2020
Exercise
Eat dinner
Pay Bills
Visit Norway
Appointments: (F)ind, (A)dd, or (Q)uit: f
Enter the date (mm, dd, yyyy) to search: 12 01 2020
Exercise
Eat dinner
Pay Bills
Visit Patagonia
Appointments: (F)ind, (A)dd, or (Q)uit: q
Good bye. Have a nice day!
Hi,
Step
We can have a private variable something like
appointmentList
to maitain the list of appointments.
private ArrayList<Appointment> appointmentList = new ArrayList<Appointment>();
Step
Add logic to the method addAppointment()
Build an Appointment object and add the object to the list based on
the user input.
Step
Add logic to the method findAppointments()
Iterate through the appointment list and find the appointments
based on the user input.
Sample Methods Below
////////////////////////////
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
Build on the Appointment hierarchy.
Give the user the option to add new appointments.
The user must specify the type of the appointment and
description, and then, if required, the day or date.
*/
public class AppointmentBook
{
private ArrayList<Appointment> appointmentList =
new ArrayList<Appointment>();
/**
Adds a new Appointment object based on user input.
@param in the Scanner to read from.
* @throws ParseException
*/
public void addAppointment(Scanner in) throws
ParseException
{
System.out.print("Enter Type:
[(D)aily, (M)onthly,(O)neTime] and description: ");
String choice = in.next();
Appointment a;
try {
if
(choice.equals("D") || choice.equals("d"))
{
String desc= in.next();
a= new Daily(desc);
}
else if
(choice.equals("M") || choice.equals("m"))
{
String desc= in.next();
System.out.print("Enter the day of the
appointment: ");
int day =in.nextInt();
a= new Monthly(desc,day);
}
else {
String desc= in.next();
System.out.print("Enter the day of the
appointment: ");
int day =in.nextInt();
int month=in.nextInt();
int year=in.nextInt();
a= new OneTime(desc,day,month,year);
}
// Adding to the
appointment
appointmentList.add(a);
} catch (Exception e) {
// TODO
Auto-generated catch block
e.printStackTrace();
}
}
/**
Method to print all appointments on a certain date.
@param in the Scanner to read from.
*/ public void findAppointments(Scanner in)
{
System.out.print("Enter the dd mm
yyyy: ");
int day = in.nextInt();
int month = in.nextInt();
int year = in.nextInt();
System.out.printf("\nYou have the
following appointments on (%s/%s/%s):\n", day, month, year);
for (Appointment a :
appointmentList) {
//Check if the
appointment id Daily
if (a instanceof
Daily) {
System.out.println(a);
}
//Check if the
appointment id Monthly
else if(a
instanceof Monthly) {
Monthly m = (Monthly)a;
if (day == m.getDay()) {
System.out.println(a);
}
}
//Check if the
appointment id OneTime
else if (a
instanceof OneTime) {
OneTime ot = (OneTime)a;
if (day == ot.getDay() && month ==
ot.getMonth() && year == ot.getYear()) {
System.out.println(a);
}
}
}
}
// Just to test the appointment book
public static void main(String[] args) throws
ParseException
{
Scanner in = new
Scanner(System.in);
AppointmentBook ab = new
AppointmentBook();
System.out.println("Welcome to
the Appointment Book");
System.out.println("-------------------------------");
boolean done = false;
while (!done)
{
System.out.print("Appointments: (F)ind, (A)dd, or (Q)uit: ");
String choice =
in.next();
if
(choice.equals("F") || choice.equals("f"))
{
ab.findAppointments(new
Scanner(System.in));
}
else if
(choice.equals("A") || choice.equals("a"))
{
ab.addAppointment(new Scanner(System.in));
}
done =
choice.equals("Q") || choice.equals("q");
}
System.out.println("Good bye. Have
a nice day!");
in.close();
}
}
///////////////////////////////////////////////////////
Hope this helps.