In: Computer Science
Could you please improve my program with the method below?
Improve the appointment book program of PA5 Exercises P9.3 and P9.4 by letting the user save the appointment data to a file and reload the data from a file. The saving part is straightforward: Make a method save. Save the type, description, and date to a file.
public static void save(String file, Appointment appointment) throws IOException
{...}
For the loading part, first determine the type of the appointment to be loaded, create an object of that type, and then call a load method to load the data.
public static ArrayList<Appointment> load(String
fileName)
throws FileNotFoundException
{.....}
Provide JUnit test class, P9_5Test with test methods to test
different methods of the Appointment program such as occursOn,
adding of new appointments, and saving and reloading of appointment
files.
Test Cases for occursOn()
In your test class, create Onetime, daily, and monthly appointment
for date: 2020-10-15
1) check date 2020-10-15, all 3 should occur
2) check date 2020-11-15, daily and monthly should occur
3) check date 2020-12-05, only daily should occur
4) check date 2020-09-15, none would occur since before the date that appointment is made.
Test Cases of saving appointment files
Save the Onetime, daily and monthly appointments in a file
“appointments.txt”
1) test if appointments are added
2) test if appointments are saved
Test Cases for loading of appointment files
Load the appointments from an appointment file (i.e
savedApps.txt)
1) test for the correct number of appointments
2) test if appointments are loaded (i.e compare appointment object with content of the file)
--------------------------------------------------------------------------------------------------------------------------------------------------
Here is the program
P9_3.java
package com.company;
import java.util.Scanner;
public class P9_3 {
    public static void main(String[] args) {
        int day,month,year;
        Appointment[] Schedule= new Appointment[3];
        Schedule[0]= new OneTime("Doctor Appointment",03,10,2020);
        Schedule[1]= new Daily("Zoom Meeting",21,8,2020);
        Schedule[2]=new Monthly("Car Wash",21,6,2020);
        Scanner in = new Scanner(System.in);
        System.out.println("Enter day: ");
        day = in.nextInt();
        System.out.println("Enter month: ");
        month = in.nextInt();
        System.out.println("Enter year: ");
        year = in.nextInt();
        System.out.println("Display Appointments Schedule: ");
        for(Appointment a : Schedule) {
            if(a.occursOn(day, month, year))
                System.out.println(a.toString());
        }
    }
}
----------------------------------------------------------------------------------
Appointment.java
package com.company;
public class Appointment {
    protected String Schedule;
    protected int D;
    protected int M;
    protected int Y;
    //Appointment Constructor
    public Appointment(String Display, int Day, int Month, int Year)
    {
        this.Schedule = Display;
        this.D = Day;
        this.M = Month;
        this.Y = Year;
    }
    //getters and setters
    public String getDescription() {
        return Schedule;
    }
    public void setDescription(String description) {
        this.Schedule = description;
    }
    public int getDay() {
        return D;
    }
    public void setDate(int Day) {
        this.D = Day;
    }
    public int getMonth() {
        return M;
    }
    public void setMonth(int Month) {
        this.M = Month;
    }
    public int getYear() {
        return Y;
    }
    public void setYear(int Year) {
        this.Y = Year;
    }
    // this method would be overridden in the subclasses
    public boolean occursOn(int D, int M, int Y)
    {
        if(getDay() == D  && getMonth() == M && getYear() == Y)
            return true;
        else
            return false;
    }
    //toString()
    public String toString()
    {
        return Schedule + " on "+ D + "/" + M + "/" + Y ;
    }
}
------------------------------------------------------------------------------------------
P9_4.java
package com.company;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class P9_4 {
    public static void main(String[] args) {
        //Scanner class used to take user input from console
        Scanner sc = new Scanner(System.in);
        //Using ArrayList to store appointment as we don't have fix set of Appointment
        List<Appointment> appointments = new ArrayList<>();
        //Using do while so user get at least one prompt to add appointment
        do {
            System.out.print("Select an option: A for add an appointment, C for checking, Q to quit: ");
            String option = sc.nextLine();
            //Checking option to perform option specific operation
            if(option.equals("A")) {
                //If user selects to add an appointment user will be asked below additional details to add appointment
                System.out.print("Enter the type (O - Onetime, D - Daily, or M - Monthly): ");
                String appointmentType = sc.nextLine();
                System.out.print("Enter the date(yyyy-mm-dd): ");
                String date = sc.nextLine();
                System.out.print("Enter the description: ");
                String desc = sc.nextLine();
                //Here splitting a date by '-' as we are taking input in yyyy-mm-dd format
                String[] datePart = date.split("-");
                int day;
                int month;
                int year;
                if(datePart != null && datePart.length == 3) {
                    //getting last part as day from array
                    day = Integer.parseInt(datePart[2]);
                    //getting middle part as month from array
                    month = Integer.parseInt(datePart[1]);
                    //getting first part as year from array
                    year = Integer.parseInt(datePart[0]);
                } else {
                    //if date is not valid i.e not in given format then this error message gets display and user start from first
                    System.out.println("Invalid Date");
                    break;
                }
                //This will create new onetime appointment
                if(appointmentType.equals("O")) {
                    Appointment onetime = new OneTime(desc,day , month, year);
                    appointments.add(onetime);
                } else if(appointmentType.equals("D")) {
                    //This will create new daily appointment
                    Appointment daily = new Daily(desc,day , month, year);
                    appointments.add(daily);
                } else if(appointmentType.equals("M")) {
                    //This will create new monthly appointment
                    Appointment monthly = new Monthly(desc,day , month, year);
                    appointments.add(monthly);
                } else {
                    //if appointment type is not valid i.e not in given choice O,D and M then this error message gets display and user start from first
                    System.out.println("Invalid Appointment Type");
                    continue;
                }
                continue;
            } else if (option.equals("C")) {                //This will check appointments for give date
                System.out.print("Enter the year:");
                int year = Integer.parseInt(sc.nextLine());
                System.out.print("Enter the month:");
                int month = Integer.parseInt(sc.nextLine());
                System.out.print("Enter the day:");
                int day = Integer.parseInt(sc.nextLine());
                for (Appointment appointment:appointments) {
                    //This will check appointments for give date
                    if(appointment.occursOn(day, month, year)) {
                        System.out.println(appointment.toString());
                    }
                }
                continue;
            } else if(option.equals("Q")) { //This will quit the program
                System.out.println("Thank you");
                break;
            } else {                    //if option is not valid i.e not in given choice A,C and Q then this error message gets display and user start from first
                System.out.println("Invalid Option");
                continue;
            }
        } while (true);
    }
}
---------------------------------------------------------------------------------------------------
Daily.java
package com.company;
public class Daily extends Appointment {
        public Daily(String Display, int Day, int Month, int Year) {
            super(Display, Day, Month, Year);
        }
        //daily events except future events
        public boolean occursOn(int Year, int Month, int Day)
        {
            if(getDay() < Day || getMonth() < Month || getYear() < Year)
                return true;
            else
                return false;
        }
        public String toString()
        {
            return " Your Daily Appointment -"+ Schedule +" from " + getDay() + "/" + getMonth() + "/" + getYear();
        }
}
-------------------------------------------------------------------------------------------------
Monthly.java
package com.company;
import java.time.Year;
public class Monthly extends Appointment {
    public Monthly(String Display, int Day, int Month, int Year) {
        super(Display, Day, Month, Year);
    }
    public boolean occursOn(int Day, int Month, int Year)
    {
        //future events are not returned
        if(getDay() == Day && (getMonth() < Month || getYear() < Year))
            return true;
        else
            return false;
    }
    public String toString()
    {
        return " Your Monthly Appointment: "+ Schedule + " from " + getYear() + "/" + getMonth() + "/" + getDay();
    }
}
----------------------------------------------------------------------------------------------------
Onetime.java
package com.company;
public class OneTime extends Appointment {
        public OneTime(String Display, int Day, int Month, int Year) {
            super(Display, Day, Month, Year);
        }
        public String toString()
        {
            return "Your OneTime Appointment -" + super.toString();
        }
}
/**************P9_3.java******************/
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class P9_3 {
public static void main(String[] args) {
       //Scanner class used to take
user input from console
       Scanner sc = new
Scanner(System.in);
       //Using ArrayList to store
appointment as we don't have fix set of Appointment
       List<Appointment>
appointments = new ArrayList<>();
       //Using do while so user get at
least one prompt to add appointment
       do {
           //Prompt to
selection operation
          
System.out.print("Select an option: A for add an appointment, C for
checking, Q to quit: ");
           String option =
sc.nextLine();
           //Checking
option to perform option specific operation
          
if(option.equals("A")) {
          
    //If user selects to add an appointment user
will be asked below additional details to add appointment
          
    System.out.print("Enter the type (O - Onetime, D
- Daily, or M - Monthly): ");
          
    String appointmentType = sc.nextLine();
          
    System.out.print("Enter the date(yyyy-mm-dd):
");
          
    String date = sc.nextLine();
          
    System.out.print("Enter the description:
");
          
    String desc = sc.nextLine();
          
    //Here splitting a date by '-' as we are taking
input in yyyy-mm-dd format
          
    String[] datePart = date.split("-");
          
   
          
    int day;
          
    int month;
          
    int year;
          
    if(datePart != null && datePart.length
== 3) {
          
        //getting last part as day
from array
          
        day =
Integer.parseInt(datePart[2]);
          
        //getting middle part as
month from array
          
        month =
Integer.parseInt(datePart[1]);
          
        //getting first part as year
from array
          
        year =
Integer.parseInt(datePart[0]);
          
    } else {
          
        //if date is not valid i.e
not in given format then this error message gets display and user
start from first
          
        System.out.println("Invalid
Date");
          
        break;
          
    }
          
   
          
    //This will create new onetime appointment
          
    if(appointmentType.equals("O")) {
          
        Appointment onetime = new
OneTime(desc,day , month, year);
          
       
appointments.add(onetime);
          
    } else if(appointmentType.equals("D")) {
          
        //This will create new daily
appointment
          
        Appointment daily = new
Daily(desc,day , month, year);
          
       
appointments.add(daily);
          
    } else if(appointmentType.equals("M")) {
          
        //This will create new
monthly appointment
          
        Appointment monthly = new
Monthly(desc,day , month, year);
          
       
appointments.add(monthly);
          
    } else {
          
        //if appointment type is not
valid i.e not in given choice O,D and M then this error message
gets display and user start from first
          
        System.out.println("Invalid
Appointment Type");
          
        continue;
          
    }
          
    continue;
           } else if
(option.equals("C")) {       
        //This will check
appointments for give date
          
    System.out.print("Enter the year:");
          
    int year =
Integer.parseInt(sc.nextLine());
          
    System.out.print("Enter the month:");
          
    int month =
Integer.parseInt(sc.nextLine());
          
    System.out.print("Enter the day:");
          
    int day = Integer.parseInt(sc.nextLine());
          
    for (Appointment appointment:appointments)
{
          
        //This will check
appointments for give date
          
        if(appointment.occursOn(day,
month, year)) {
          
           
System.out.println(appointment.toString());
          
        }
          
    }
continue;
           } else
if(option.equals("Q")) { //This will quit the program
          
    System.out.println("Thank you");
          
    break;
           } else {
          
        //if option is not valid i.e
not in given choice A,C and Q then this error message gets display
and user start from first
          
    System.out.println("Invalid Option");
          
    continue;
           }
       } while (true);
   }
}
/*****************Daily.java*********************/
public class Daily extends Appointment {
   public Daily(String Display, int Day, int Month,
int Year) {
       super(Display, Day, Month,
Year);
   }
   public String toString()
   {
       return " Your Daily Appointment: "+
Schedule + " from " + getYear() + "/" + getMonth() + "/" +
getDay();
   }
}

