(Solved Homework): Write a program to implement an appointment book.  Implement a superclass Appointment and subclasses

Write a program to implement an appointment book.  Implement a superclass Appointment and subclasses Onetime, Daily, and Monthly. An appointment has a description (for example, “meet with accountant”) 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.  Then fill an array of Appointment objects with a mixture of appoin tments. Have the user enter a date and print out all appointments that occur on that date.  Allow the user to add new appointments. The user must specify the type of appointment, the description, and the date. Provide a test class to show the implementation of your appointment book. (Java)

Expert Answer

 PROGRAM CODE:

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

 

iWriteHomework
Order NOW for a 10% Discount
Pages (550 words)
Approximate price: -

Why Us?

Top Quality and Well-Researched Papers

All ourbpapers are written from scratch. In fact, Clients who ask for paraphrasing services are highly discouraged. We have writers ready to craft any paper from scratch and deliver quality ahead of time.

Professional and Experienced Academic Writers

Our writers keeps you posted on your papers progress - providing you with paper outline/draft. You are also at liberty to communicate directly with your writer.

Free Unlimited Revisions

If you think we missed something, send your order for a free revision. You have 10 days to submit the order for review after you have received the final document. You can do this yourself after logging into your personal account or by contacting our support.

Prompt Delivery and 100% Money-Back-Guarantee

All papers are always delivered on time, in many cases quite ahead of time. In case we need more time to master your paper, we may contact you regarding the deadline extension. In case you cannot provide us with more time, a 100% refund is guaranteed.

Original & Confidential

We use several writing tools checks to ensure that all documents you receive are free from plagiarism. Our editors carefully review all quotations in the text. We also promise maximum confidentiality in all of our services.

24/7 Customer Support

Our support agents are available 24 hours a day 7 days a week and committed to providing you with the best customer experience. Get in touch whenever you need any assistance.

Try it now!

Calculate the price of your order

Total price:
$0.00

How it works?

Follow these simple steps to get your paper done

Place your order

Fill in the order form and provide all details of your assignment.

Proceed with the payment

Choose the payment system that suits you most.

Receive the final file

Once your paper is ready, we will email it to you.

Our Services

No need to work on your paper at night. Sleep tight, we will cover your back. We offer all kinds of writing services.

Essays

Essay Writing Service

No matter what kind of academic paper you need and how urgent you need it, you are welcome to choose your academic level and the type of your paper at an affordable price. We take care of all your paper needs and give a 24/7 customer care support system.

Admissions

Admission Essays & Business Writing Help

An admission essay is an essay or other written statement by a candidate, often a potential student enrolling in a college, university, or graduate school. You can be rest assurred that through our service we will write the best admission essay for you.

Reviews

Editing Support

Our academic writers and editors make the necessary changes to your paper so that it is polished. We also format your document by correctly quoting the sources and creating reference lists in the formats APA, Harvard, MLA, Chicago / Turabian.

Reviews

Revision Support

If you think your paper could be improved, you can request a review. In this case, your paper will be checked by the writer or assigned to an editor. You can use this option as many times as you see fit. This is free because we want you to be completely satisfied with the service offered.

× Contact Live Agents