Appointment.java
package appointment;
public class Appointment {
private String description;
private int year;
private int month;
private int day;
public Appointment(String description, int year, int month, int day) {
this.description = description;
this.year = year;
this.month = month;
this.day = day;
}
public int getYear() {
return year;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public String getDescription()
{
return description;
}
public void setDescription(String desc)
{
this.description = desc;
}
public String getDate()
{
return month + “/” + day + “/” + year;
}
public boolean occursOn(int year, int month, int day)
{
return this.year >= year ;
}
@Override
public String toString() {
return getDate() + ” – ” + description ;
}
}
OneTime.java
package appointment;
public class OneTime extends Appointment
{
public OneTime(String description, int year, int month, int day) {
super(description, year, month, day);
}
@Override
public boolean occursOn(int year, int month, int day) {
// TODO Auto-generated method stub
return super.occursOn(year, month, day) && getMonth() == month && getDay() == day;
}
@Override
public String toString() {
return “One time Appointment: ” + super.toString();
}
}
Daily.java
package appointment;
public class Daily extends Appointment{
public Daily(String description, int year, int month, int day) {
super(description, year, month, day);
// TODO Auto-generated constructor stub
}
//it occurs daily so its true all the time
@Override
public boolean occursOn(int year, int month, int day) {
return super.occursOn(year, month, day);
}
@Override
public String toString() {
// TODO Auto-generated method stub
return “Daily Appointment: ” + super.toString();
}
}
Monthly.java
package appointment;
public class Monthly extends Appointment{
public Monthly(String description, int year, int month, int day) {
super(description, year, month, day);
// TODO Auto-generated constructor stub
}
@Override
public boolean occursOn(int year, int month, int day) {
// TODO Auto-generated method stub
return super.occursOn(year, month, day) && getDay() == day;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return “Monthly Appointment: ” + super.toString();
}
}
AppointmentBook.java
package appointment;
import java.util.Scanner;
public class AppointmentBook {
int y , m , d , counter ;
Scanner kbd;
Appointment appointments[];
public AppointmentBook() {
y = 0;
m = 0;
d = 0;
counter = 0;
appointments = new Appointment[20];
kbd = new Scanner(System.in);
}
public boolean isValidMonth(int m)
{
if(m<1 || m>12)
return false;
else return true;
}
public boolean isLeapYear(int y)
{
if(y%400 == 0)
return true;
else if(y%4 == 0)
return true;
else
return false;
}
public boolean isValidDate(int m, int d, int y)
{
if(d<1)
return false;
if(m==2 )
{
if(isLeapYear(y))
if(d>29)
return false;
else
if(d>28)
return false;
}
else if(m==1 || m==3 || m==5 || m == 7 || m == 8 || m == 10 || m == 12)
if(d>31)
return false;
else
if(d>30)
return false;
return true;
}
public void readDate()
{
System.out.print(“Enter year: “);
y = kbd.nextInt();
while(true)
{
System.out.print(“nEnter month: “);
m = kbd.nextInt();
if(isValidMonth(m))
break;
}
while(true)
{
System.out.print(“nEnter Date: “);
d = kbd.nextInt();
if(isValidDate(m, d, y))
break;
}
}
public void process() {
appointments[counter++] = new Monthly(“Dentist check up”, 2017, 4, 5);
appointments[counter++] = new OneTime(“Meeting with Zac”, 2017, 6, 20);
appointments[counter++] = new Daily(“Grocery Shopping”, 2017, 5, 15);
appointments[counter++] = new OneTime(“Follow up on zac’s request”, 2017, 9, 10);
appointments[counter++] = new Monthly(“Get Hair cut”, 2017, 2, 3);
while(true)
{
String desc;
int ch = 0, type = 0;
System.out.print(“n1. Check for appointmentsn2. Make an appointmentn3. ExitnEnter your choice: “);
ch = kbd.nextInt();
if(ch == 1)
{
readDate();
System.out.println(“nThe appointments that occur on ” + m + “/” + d + “/” + y + ” are..”);
for(Appointment app: appointments)
if(app != null && app.occursOn(y, m, d))
System.out.println(app);
}
else if(ch ==2)
{
System.out.print(“nEnter description for new appointment(q to quit): “);
//flushing out newline
kbd.nextLine();
desc = kbd.nextLine();
if(desc.equals(“q”))
break;
readDate();
while(type<1 || type>3)
{
System.out.print(“n1. One timen2. Dailyn3. MonthlynSelect an appointment: “);
type = kbd.nextInt();
}
if(type == 1)
appointments[counter++] = new OneTime(desc, y, m, d);
else if(type == 2)
appointments[counter++] = new Daily(desc, y, m, d);
else
appointments[counter++] = new Monthly(desc, y, m, d);
}
else if(ch == 3)
{
System.out.println(“Thank you !”);
System.exit(0);
}
else
System.out.println(“Wrong choice…”);
}
}
}
AppointmentTester.java
package appointment;
public class AppointmentTester {
public static void main(String[] args) {
AppointmentBook book = new AppointmentBook();
book.process();
}
}
OUTPUT:
1. Check for appointments
2. Make an appointment
3. Exit
Enter your choice: 1
Enter year: 2017
Enter month: 12
Enter Date: 11
The appointments that occur on 12/11/2017 are..
Daily Appointment: 5/15/2017 – Grocery Shopping
1. Check for appointments
2. Make an appointment
3. Exit
Enter your choice: 2
Enter description for new appointment(q to quit): go to the mall
Enter year: 2017
Enter month: 4
Enter Date: 5
1. One time
2. Daily
3. Monthly
Select an appointment: 1
1. Check for appointments
2. Make an appointment
3. Exit
Enter your choice: 3