In: Computer Science
I got this homework, to make an appointment program. it says like this: Write 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 are provided with a Junit test AppointmentTest .java. Run your code with this file to make sure all the tests pass. Once you complete the code and all your tests pass, 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 completed the classes: Appointment, OneTime, Daily, and Monthly. And all the tests passed. The classes look like this:
public abstract class Appointment {
String description;
public Appointment(String description) {
this.description = description;
}
public abstract boolean occursOn(int year, int month, int
day);
}
public class OneTime extends Appointment {
int year;
int month;
int day;
public OneTime(int year, int month, int day, String
description){
super(description);
this.year = year;
this.month = month;
this.day = day;
}
@Override
public boolean occursOn( int year, int month, int day){
return (this.year==year && this.month==month &&
this.day==day );
}
}
public class Daily extends Appointment {
public Daily(String description){
super( description);
}
@Override
public boolean occursOn(int year, int month, int day){
return true;
}
}
public class Monthly extends Appointment {
int day;
public Monthly(int day,String description){
super(description);
this.day = day;
}
@Override
public boolean occursOn(int year, int month, int day){
return (this.day == day);
}
}
Iam having trouble writing the AppointmentBook.java class, which skeleton is given to me and i guess i should write the methods given there. The file looks like this :
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();
}
}
And this is a sample run provided to me:
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!
Thanks for your help in advance.
***************Appointment class.********************
public abstract class Appointment {
String description;
public Appointment(String description) {
this.description =
description;
}
public abstract boolean occursOn(int year, int
month, int day);
}
********************* OneTime class *********************
public class OneTime extends Appointment {
int year;
int month;
int day;
public OneTime(int year, int month, int day, String
description) {
super(description);
this.year = year;
this.month = month;
this.day = day;
}
@Override
public boolean occursOn(int year, int month, int day)
{
return (this.year == year
&& this.month == month && this.day == day);
}
}
*******************Daily class ******************
public class Daily extends Appointment {
public Daily(String description) {
super(description);
}
@Override
public boolean occursOn(int year, int month, int day)
{
return true;
}
}
**************** Monthly class *******************
public class Monthly extends Appointment {
int day;
public Monthly(int day, String description) {
super(description);
this.day = day;
}
@Override
public boolean occursOn(int year, int month, int day)
{
return (this.day == day);
}
}
***************** AppointmentBook class ********************
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 Appointment[] appointmentList = new
Appointment[100];
int size = 0;
/**
* Adds a new Appointment object based on user
input.
*
* @param in
* the Scanner to read from.
*/
public void addAppointment(Scanner in) {
System.out.println("Enter type
[(D)aily, (M)onthly, (O)netime] and description: ");
String input = in.nextLine();
// this will give you first
character in the input string which will be
// type of the appointment.
String type = input.substring(0,
input.indexOf(" "));
// this will give you rest of the
string which will be description of
// the appointment. After stripping
first character rest will will be
// the description.
String description =
input.substring(input.indexOf(" ") + 1, input.length());
if (type.equalsIgnoreCase("D"))
{
Appointment
dailyAppointment = new Daily(description);
appointmentList[size++] = dailyAppointment;
} else if
(type.equalsIgnoreCase("M")) {
System.out.println("Enter the day of the appointment:");
int day =
in.nextInt();
Appointment
monthlyAppointment = new Monthly(day, description);
appointmentList[size++] = monthlyAppointment;
} else if
(type.equalsIgnoreCase("O")) {
System.out.println("Enter the date of the appointment (mm dd yyyy)
: ");
String date =
in.nextLine();
String[]
dateArray = date.split(" ");
int day =
Integer.parseInt(dateArray[0]);
int month =
Integer.parseInt(dateArray[1]);
int year =
Integer.parseInt(dateArray[2]);
Appointment
oneTimeAppointment = new OneTime(year, month, day,
description);
appointmentList[size++] = oneTimeAppointment;
} else {
System.out.println("Invalid Option");
}
}
/**
* Method to print all appointments on a certain
date.
*
* @param in
* the Scanner to read from.
*/
public void findAppointments(Scanner in) {
System.out.println("Enter the date
(mm, dd, yyyy) to search: ");
String date = in.nextLine();
String[] dateArray = date.split("
");
int day =
Integer.parseInt(dateArray[0]);
int month =
Integer.parseInt(dateArray[1]);
int year =
Integer.parseInt(dateArray[2]);
for (Appointment appointment :
appointmentList) {
if
(appointment.occursOn(year, month, day)) {
System.out.println(appointment.description);
}
}
}
// 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;
do {
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));
} else if
(choice.equals("Q") || choice.equals("q")) {
done = true;
}
} while (!done);
System.out.println("Good bye.
Have a nice day!");
in.close();
}
}