Question: #1: Validate that the month is valid (01 - 12), and the day is valid (01 -31) for a new appointment. #2: Validate that the
#1: Validate that the month is valid (01 - 12), and the day is valid (01 -31) for a new appointment.
#2: Validate that the time is valid (hour 00 - 23; minutes 00 - 59) for a new appointment.
#3: Validate that an appointment does not conflict in time with an already existent appointment in the array of appointments.
4: Another Loop...
Prompt user to enter a date so that all appointments that fall on that date can be displayed. Upon displaying the information (or a not found message), ask the user if they wish to check for appointments on another date? If so, repeat the same method calls.
import java.util.ArrayList;
import java.util.Scanner;
abstract class Appointment{ // abstract class declaration
private String description; // variable declaration
private String lastName; // variable declaration
private int hours=0; // variable declaration and assign hours as 0
private int minutes=0; // variable declaration and assign minutes as 0
public void setDescription(String description){ // called method
this.description = description; // since parameter and member same use this to assign parameter
}
public void setLastName(String lastName){ // called method
this.lastName = lastName; // since parameter and member same use this to assign parameter
}
public void setHours(int hours){ // called method
this.hours = hours; // since parameter and member same use this to assign parameter
}
public void setMinutes(int minutes){ // called method
this.minutes = minutes; // since parameter and member same use this to assign parameter
}
public String getDescription(){ // called method
return this.description; // return descripton what we assign from parameter
}
public String getLastName(){ // called method
return this.lastName; // return lastName what we assign from parameter
}
public int getHours(){ // called method
return this.hours; // retuen hours what we assign from parameter
}
public int getMinutes(){ // called method
return this.minutes; // retuen minutes what we assign from parameter
}
abstract public boolean occursOn(int year,int month,int day); // calling method
}
class Onetime extends Appointment{ // derived class
/* members declaration*/
private int year;
private int month;
private int day;
public void setYear(int year) { // called method
this.year=year; // since parameter and member same use this to assign parameter
}
public void setMonth(int month) { // called method
this.month=month; // since parameter and member same use this to assign parameter
}
public void setDay(int day) { // called method
this.day=day; // since parameter and member same use this to assign parameter
}
public int getYear() { // called method
return this.year; // since parameter and member same use this to assign parameter
}
public int getMonth() { // called method
return this.month; // since parameter and member same use this to assign parameter
}
public int getDay() { // called method
return this.day; // since parameter and member same use this to assign parameter
}
@Override
public boolean occursOn(int year, int month, int day) {
// method to check overide or same data
if(this.year==year && this.month==month && this.day==day) // check overide or same data
return true; // if exist return true
return false; // if not return false
}
}
class Daily extends Appointment{ // DERIVED CLASS
@Override
public boolean occursOn(int year, int month, int day) {
// TODO Auto-generated method stub
return true;
}
}
class Monthly extends Appointment{ // DERIVED CLASS
private int day; // member declaration
public void setDay(int day) { // called method
this.day=day;
}
public int getDay() { // called method
return this.day; // since parameter and member same use this to assign parameter
}
@Override
public boolean occursOn(int year, int month, int day) {
// called method
if(this.day==day) // check same day or not
return true; // same day return true
return false; // not same day return false
}
}
public class TestAppointment { // class declaration
static Scanner sc= new Scanner(System.in); // scanner to accept data
public static void main(String[] args) { // main function
ArrayList al = new ArrayList(); // array list declaration
System.out.println("How many appointments do you with to make?");
int num = sc.nextInt(); // Accept number of appointments
for(int i = 1;i<=num;i++) { // Loop to appoint ment menu selections
System.out.println("Please make a selection:");
System.out.println("1. One Time Appointment");
System.out.println("2. Daily Appointment");
System.out.println("3. Monthly Appointment");
int selection = sc.nextInt(); // Accept the selection
al.add(makeAppointment(selection)); // add the appointments to array llist
}
System.out.println("*******Thank you for making all your appointments*********");
char choice='y'; // variable declaration
while(choice=='y') {
System.out.println("What is the date you wish to look up in your Appointment's Calendar");
System.out.print("Enter the month: ");
int month=sc.nextInt(); // Acceept the month
System.out.print("Enter the day: ");
int day=sc.nextInt(); // Acceept the day
System.out.print("Enter the year: ");
int year=sc.nextInt(); // Acceept the year
System.out.println("On "+month+"/"+day+"/"+year+" you have the following appointments:"); // print the appointments
for(Appointment a:al) { // Loop to run for all appointments
boolean isPresent=checkAppointment(a,year,month,day); // calling method
if(isPresent) { // check appointment exist
System.out.println(a.getDescription()+" with "+a.getLastName()+" at "+a.getHours()+":"+a.getMinutes()); // print appointment details
}
}
System.out.println("Do you wish to look up another date?");
choice=sc.next().charAt(0); // accept choice
}
System.out.println("Thank you for using Appointment Calendar.");
}
public static Appointment makeAppointment(int selection) { // called method
/* variable declaration*/
Appointment app=null;
String description="";
String lastName="";
int hours=0;
int minutes = 0;
int day=0;
int year=0;
int month=0;
sc.nextLine();
switch (selection) { // switch statement to selection
case 1:
Onetime onetime=new Onetime();
System.out.println("What is the description of your appointment?");
description = sc.nextLine(); // Accept description for appointment
System.out.println("What is your person's name?");
lastName = sc.nextLine(); // Accept person's name
System.out.println("What is the year of your appointment?(2017-2018)");
year = sc.nextInt(); // Accept year
System.out.println("What is the month of your appointment?(1-12)");
month = sc.nextInt(); // Accept month
System.out.println("What is the day of your appointment?(1-31)");
day = sc.nextInt(); // Accept day
System.out.println("What is the hour of your appointment?(1-31)");
hours = sc.nextInt(); // Accept hours
System.out.println("What is the minutes of your appointment?(1-31)");
minutes = sc.nextInt(); // Accept minutes
/*calling methods*/
onetime.setDescription(description);
onetime.setHours(hours);
onetime.setLastName(lastName);
onetime.setMonth(month);
onetime.setMinutes(minutes);
onetime.setYear(year);
onetime.setDay(day);
System.out.println("Appointment added with "+lastName+" on "+month+"/"+day+"/"+year+" at "+hours+":"+minutes); // print appointment holder details
System.out.println();
return onetime;
case 2:
Daily daily = new Daily();
System.out.println("What is the description of your appointment?");
description = sc.nextLine(); // Accept description
System.out.println("What is your person's name?");
lastName = sc.nextLine(); // Accept lastName
System.out.println("What is the hour of your appointment?(1-31)");
hours = sc.nextInt(); // Accept hours
System.out.println("What is the minutes of your appointment?(1-31)");
minutes = sc.nextInt(); // Accept minutes
/* calling methods*/
daily.setDescription(description);
daily.setHours(hours);
daily.setLastName(lastName);
daily.setMinutes(minutes);
System.out.println("Appointment added with "+lastName+" Daily at "+hours+":"+minutes); // print appointment holder details
System.out.println();
return daily;
case 3:
Monthly monthly = new Monthly();
System.out.println("What is the description of your appointment?");
description = sc.nextLine(); // Accept description
System.out.println("What is your person's name?");
lastName = sc.nextLine(); // Accept lastName
System.out.println("What day of the month is your appointment?(1-12)");
day = sc.nextInt(); // Accept day
System.out.println("What is the hour of your appointment?(1-31)");
hours = sc.nextInt(); // Accept hours
System.out.println("What is the minutes of your appointment?(1-31)");
minutes = sc.nextInt(); // Accept minutes
/* calling methods*/
monthly.setDescription(description);
monthly.setHours(hours);
monthly.setLastName(lastName);
monthly.setMinutes(minutes);
monthly.setDay(day);
System.out.println("Appointment added with "+lastName+" Monthly on day "+day+" at "+hours+":"+minutes);
// print appointment holder details
System.out.println();
return monthly;
default:
System.out.println("Invalid Choice..!!");
return app;
}
}
public static boolean checkAppointment(Appointment a, int year, int month, int day) { // calling method to check appointments
return a.occursOn(year, month, day); // pass details
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
