Question

Programming language: JAVA Implement a superclass Appointment and subclasses OneTime, Daily, and Monthly. An appointment has...

Programming language: JAVA

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, check whether the day of the month matches. Ask the user to enter a date to check (for example, 2006 10 5), and ask to user if they want to check against OneTime, Day or Month appointment. Based on what the user selected, OccursOn inside each of the sub class should run and display any matching appointment and associated descriptions.For OneTime subclass, OccursOn need three inputs (Year, Month and Day) to validate, for Day subclass, OccursOn needs one input (Day) to validate, and for Month subclass, OccursOn need one input (Month) to validate. OccursOn is different for different subclasses. There should be appointments already created and you are simply asking the users for a date to check against them.

0 0
Add a comment Improve this question
Answer #1

Below is your code, Let me know in comments if you face any issue

Appointment.java

import java.util.Date;

public class Appointment {
   protected String description;
   protected Date date;

   public Appointment(String description, Date date) {
       this.description = description;
       this.date = date;
   }

   public String getDescription() {
       return description;
   }

   public void setDescription(String description) {
       this.description = description;
   }

   public Date getDate() {
       return date;
   }

   public void setDate(Date date) {
       this.date = date;
   }

}

OneTime.java

import java.util.Calendar;
import java.util.Date;

public class OneTime extends Appointment {
   public OneTime(String description, Date date) {
       super(description, date);
   }

   public boolean OccursOn(int year, int month, int day) {
       Calendar cal = Calendar.getInstance();
       cal.setTime(this.date);
       if (cal.get(Calendar.YEAR) == year && cal.get(Calendar.MONTH) == month
               && cal.get(Calendar.DAY_OF_MONTH) == day) {
           return true;
       } else {
           return false;
       }
   }
}

Daily.java

import java.util.Calendar;
import java.util.Date;

public class Daily extends Appointment {
   public Daily(String description, Date date) {
       super(description, date);
   }

   public boolean OccursOn(int day) {
       Calendar cal = Calendar.getInstance();
       cal.setTime(this.date);

       if (cal.get(Calendar.DAY_OF_MONTH) == day) {
           return true;
       } else {
           return false;
       }
   }
}

Monthly.java

import java.util.Calendar;
import java.util.Date;

public class Monthly extends Appointment {
   public Monthly(String description, Date date) {
       super(description, date);
   }

   public boolean OccursOn(int month) {
       Calendar cal = Calendar.getInstance();
       cal.setTime(this.date);

       if (cal.get(Calendar.MONTH) == month ) {
           return true;
       } else {
           return false;
       }
   }
}

AppointmentTester.java

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Scanner;

public class AppointmentTester {
   public static void main(String[] args) {
       List<Appointment> appointments = new ArrayList<>();
       appointments.add(new Appointment("see the dentist",
               new GregorianCalendar(2006, 10, 5).getTime()));
       appointments.add(new Appointment("passport office visit",
               new GregorianCalendar(2006, 11, 11).getTime()));
       appointments.add(new Appointment("Monthly Bank visit on First day",
               new GregorianCalendar(2006, 1, 1).getTime()));
       appointments.add(new Appointment("Yearly checkup in December",
               new GregorianCalendar(2006, 11, 1).getTime()));
       appointments.add(new Appointment("Salary day, last day of month",
               new GregorianCalendar(2006, 11, 31).getTime()));

       Scanner sc = new Scanner(System.in);
       System.out.print("Enter a date in format (YYYY MM DD): ");
       int year = Integer.parseInt(sc.next());
       int month = Integer.parseInt(sc.next());
       int day = Integer.parseInt(sc.next());
       System.out.print("Select one option below:" + "\n1. One Time"
               + "\n2. Day or date of the month"
               + "\n3. Monthly or month of the year"
               + "\nSelect one option (1,2,3): ");
       String choice = sc.next();
       sc.close();

       List<Appointment> occuringApps = new ArrayList<Appointment>();
       if (choice.equals("1")) {
           for (Appointment appointment : appointments) {
               OneTime app = new OneTime(appointment.description,
                       appointment.date);
               if (app.OccursOn(year, month, day)) {
                   occuringApps.add(appointment);
               }
           }
       } else if (choice.equals("2")) {
           for (Appointment appointment : appointments) {
               Daily app = new Daily(appointment.description, appointment.date);
               if (app.OccursOn(day)) {
                   occuringApps.add(appointment);
               }
           }
       } else if (choice.equals("3")) {
           for (Appointment appointment : appointments) {
               Monthly app = new Monthly(appointment.description,
                       appointment.date);
               if (app.OccursOn(month)) {
                   occuringApps.add(appointment);
               }
           }
       } else {
           System.out.println("Wrong Choice . Please start over. ");
           return;
       }
       if (occuringApps.size() > 0) {
           System.out.println("Below are your appointments: ");
           for (Appointment appointment : occuringApps) {
               System.out.println(appointment.description);
           }
       } else {
           System.out.println("No appointments on given date.");
       }
   }
}

Output

Enter a date in format (YYYY MM DD): 2006 11 5
Select one option below:
1. One Time
2. Day or date of the month
3. Monthly or month of the year
Select one option (1,2,3): 3
Below are your appointments:
passport office visit
Yearly checkup in December
Salary day, last day of month

Add a comment
Know the answer?
Add Answer to:
Programming language: JAVA Implement a superclass Appointment and subclasses OneTime, Daily, and Monthly. An appointment has...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (JAVA) In this assignment, you will implement a superclass Appointment and subclasses Onetime, Daily, and Monthly....

    (JAVA) In this assignment, you will 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 mon.th matches. In your application part, fill an array of Appointment objects with a mixture of appointments. Have the...

  • *PYTHON* Implement a superclass Appointment and subclasses Onetime, Daily, and Monthly. An appointment has the date,...

    *PYTHON* Implement a superclass Appointment and subclasses Onetime, Daily, and Monthly. An appointment has the date, the month, the year, and the description of an appointment (e.g., “see the dentist”). Then, write a method occcursOn(year, month, day) that checks whether the appointment occurs on that date. In the test program, you should create a list of appointments using your subclasses of Onetime, Daily, and Monthly. Once you have those appointments available, allow the user to input day, month, and year...

  • Help me with a python program : Implement a superclass appointment and subclasses Onetime, Daily and...

    Help me with a python program : 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(year, month, 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 a list of appointment objects with a mixture of appointments. Have the user enter a date and print...

  • Write a Java program that implements a superclass Appointment and subclasses Onetime, Daily, and Monthly. An...

    Write a Java program that implements a superclass Appointment and subclasses Onetime, Daily, and Monthly. An appointment has a description (for example, “see the dentist”) and a date. It writes a method occursOn (int year, int month, int day) that checks whether the appointmentoccurs on that date. For example, for a monthly appointment, you must check whether the day of the month matches. Then it will fill an array of Appointment objects with a mixture of appointments. When the user...

  • 5.Implement a superclass Appointment and subclasses Onetime, Daily and Monthly. An appointment has a description (for...

    5.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(year,month,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 and the appointment date started before the date entered. Then, fill a list of Appointment objects with a mixture of appointments. Have the user enter a date and...

  • Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will...

    Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...

  • Programming Assignment 6 Write a Java program that will implement a simple appointment book. The ...

    Programming Assignment 6 Write a Java program that will implement a simple appointment book. The program should have three classes: a Date class, an AppointmentBook class, and a Driver class. • You will use the Date class that is provided on Blackboard (provided in New Date Class example). • The AppointmentBook class should have the following: o A field for descriptions for the appointments (i.e. Doctor, Hair, etc.). This field should be an array of String objects. o A field...

  • I need to create a Java class file and a client file that does the following:  Ask user to enter Today’s date in the fo...

    I need to create a Java class file and a client file that does the following:  Ask user to enter Today’s date in the form (month day year): 2 28 2018  Ask user to enter Birthday in the form (month day year): 11 30 1990  Output the following: The date they were born in the form (year/month/day). The day of the week they were born (Sunday – Saturday). The number of days between their birthday and today’s...

  • CIT 149 Java 1 programming question Use DrJava to compile the following try-catch program ? The...

    CIT 149 Java 1 programming question Use DrJava to compile the following try-catch program ? The Assignment ? Specifications General Structure your file name and class name on the following pattern: The first three letters of your last name (begin with upper case.). Then the first two letters of your first name (begin with upper case.). Follow this with the name of the program: TryCatch. For a student called ’John Doe,’ the class name and file name would be: DoeJoTryCatch...

  • The purpose of this homework is to practice OOP programming covering Inheritance in java. The scenario...

    The purpose of this homework is to practice OOP programming covering Inheritance in java. The scenario is to design an online shopping system for a local supermarket (e.g., Europa Foods Supermarket or Wang Long Oriental Supermarket) and is mostly concentrated on the product registration system. Design and draw a UML diagram, and write the code for the following classes: ID: The identification number must start with 1 and follows with 6 numbers, e.g., “1123456”. Description, e.g. “Banana” Recommended Price per...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT