Question

In: Computer Science

Could you please improve my program with the method below? Improve the appointment book program of...

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. 1) check date 2020-10-15, all 3 should occur

  2. 2) check date 2020-11-15, daily and monthly should occur

  3. 3) check date 2020-12-05, only daily should occur

  4. 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. 1) test if appointments are added

  2. 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();
        }

}

Solutions

Expert Solution

  • /**************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();
       }
    }


Related Solutions

Could you double check my program? I cannot get it to run. If you could tell...
Could you double check my program? I cannot get it to run. If you could tell me any changes that need to be made as well show the output I would greatly appreciate it. LinkedList.java public class LinkedList { class Node{ int value; Node nextElement; public Node(int value) { this.value = value; this.nextElement = null; } } public Node first = null; public Node last = null; public void addNewNode(int element) { Node newValueNode = new Node(element); if(first == null)...
could you please draw me a mindmap for my thesis, my thesis topic : the impact...
could you please draw me a mindmap for my thesis, my thesis topic : the impact of block chain technology on financial markets
Could you please write a working program "linkedlist.cpp" using recursive approach for the given files below...
Could you please write a working program "linkedlist.cpp" using recursive approach for the given files below -------------------------------------------------------------------------------------------- // app.cpp #include <iostream> #include "linkedlist.h" using namespace std; void find(LinkedList& list, char ch) { if (list.find(ch)) cout << "found "; else cout << "did not find "; cout << ch << endl; } int main() { LinkedList list; list.add('x'); list.add('y'); list.add('z'); cout << list; find(list, 'y'); list.del('y'); cout << list; find(list, 'y'); list.del('x'); cout << list; find(list, 'y'); list.del('z'); cout << list;...
hello , could you please answer this question for me in details , my teacher want...
hello , could you please answer this question for me in details , my teacher want more than 400 word ?. it is an essay . 1) Discuss the application of Classical conditioning in reducing Anxiety.!!
I need a scholarship essay for my nursing program. please and thank you.
I need a scholarship essay for my nursing program. please and thank you.
Thank you for your response to my first question. COULD YOU PLEASE ASSIGN SOMEONE ELSE TO...
Thank you for your response to my first question. COULD YOU PLEASE ASSIGN SOMEONE ELSE TO HELP ME? I HAVE BEEN ASKING FOR HELP FOR SEVERAL HOURS. THIS IS A FICTITIOUS STUDY. ALL OF THE INFORMATION THAT I WAS GIVEN IS BELOW. PLEASE READ IT. For part #2, when comparing gender, GPA, and GRE test scores, would the statistical analysis be a one-way Anova, a two-way Anova, or a different test?.(See complete assignment below.) Using this information, develop the following...
How can I improve my code below to meet the requirements of this assignment with syntax...
How can I improve my code below to meet the requirements of this assignment with syntax add to my code to complete the assignment below: #include <iostream> #include <vector> using namespace std; class Inventory { private: int itemNumber; int quantity; double cost; double totalCost; public: Inventory() { itemNumber = 0; quantity = 0; cost = 0; totalCost = 0; } Inventory(int n, int q, double c) { itemNumber = n; quantity = q; cost = c; setTotalCost(); } void setItemNumber(int...
Please add to this Python, Guess My Number Program. Add code to the program to make...
Please add to this Python, Guess My Number Program. Add code to the program to make it ask the user for his/her name and then greet that user by their name. Please add code comments throughout the rest of the program If possible or where you add in the code to make it ask the name and greet them before the program begins. import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the...
Below is my C++ program of a student record system. I have done the program through...
Below is my C++ program of a student record system. I have done the program through structures. Please do the same program using classes this time and dont use structures. Also, please make the menu function clean and beautiful if you can. So the output will look good at the end. Thanks. #include<iostream> #include<string> using namespace std; struct StudentRecord {    string name;    int roll_no;    string department;    StudentRecord *next; }; StudentRecord *head = NULL; StudentRecord *get_data() {...
Please based on my python code. do the following steps: thank you Develop a method build_bst...
Please based on my python code. do the following steps: thank you Develop a method build_bst which takes a list of data (example: [6, 2, 4, 22, 34, 9, 6, 67, 42, 55, 70, 120, 99, 200]) and builds a binary search tree full of Node objects appropriately arranged. Test your build_bst with different sets of inputs similar to the example provided above. Show 3 different examples. Choose 1 example data.  Develop a function to remove a node from a bst...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT