Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the following java program: Define a class DateTime and has attributes: In

ID: 3686287 • Letter: W

Question

Write the following java program:

Define a class DateTime and has attributes:

Integer: day, month, year and time (Use Military time, 1:00pm is 1300, no colon).

Make DateTime Serializable and Comparable. Define a class appointment, which has String attributes description, location and who. It also has a DateTime attribute. Make appointment Serializable and Comparable by date and time. Us the SortedLinked list defined in OrderedListEx to store your appointments. Use a binary file to save and restore the list. Write the entire list out with one output statement. Write a menu that allows the user to do the following:

The menu should do the following:

1) Enter New Appointments

2) Delete An Appointment

3) Look Up A Single Appointment By Date and Time

4) Look Up All Appointments With A Single Person

5) List All Appointments On A Given Day

6) List All Appointments In Order

7) Quit

Explanation / Answer

I am yet in between . I will modify the answer.

Option 2,3 are not fully implemented.

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.Scanner;

public class Appointment implements Serializable, Comparable<Appointment> {
   /**
   *
   */
   private static final long serialVersionUID = 1L;
   String description;
   String location;
   String who;
   DateTime dateTime;

   public Appointment(String description, String location, String who,
           DateTime dateTime) {
       super();
       this.description = description;
       this.location = location;
       this.who = who;
       this.dateTime = dateTime;
   }

   public Appointment() {
   }

   @Override
   public int compareTo(Appointment anotherAppointment) {
       if (!(anotherAppointment instanceof Appointment))
           throw new ClassCastException("A Person object expected.");
       return this.dateTime.compareTo(anotherAppointment.dateTime);
   }

   @Override
   public String toString() {
       return "Appointment [description=" + description + ", location="
               + location + ", who=" + who + ", dateTime=" + dateTime + "]";
   }

   public static void main(String arg[]) {
       LinkedList<Appointment> listOfAppointment = null;
       if (restoreList() != null) {
           System.out.println("List populated from file.");
           listOfAppointment = restoreList();
       } else {
           listOfAppointment = new LinkedList<Appointment>();
       }
       Scanner scanner = new Scanner(System.in);
       while (true) {

           System.out.println("Enter the option:");
           System.out.println("1) Enter New Appointments");
           System.out.println("2) Delete An Appointment");
           System.out
                   .println("3) Look Up A Single Appointment By Date and Time");
           System.out
                   .println("4) Look Up All Appointments With A Single Person");
           System.out.println("5) List All Appointments On A Given Day");
           System.out.println("6) List All Appointments In Order");
           System.out.println("7) Quit");
           int option = scanner.nextInt();
           if (option == 1) {
               Appointment appointment = new Appointment();
               scanner.nextLine();// Consume newline left-over

               System.out.println("Enter description:");
               appointment.description = scanner.nextLine();
               System.out.println("Enter location:");
               appointment.location = scanner.next();
               scanner.nextLine();

               System.out.println("Enter who:");
               appointment.who = scanner.nextLine();

               System.out.println("Enter dateTime:");
               System.out.println("Enter day:");
               appointment.dateTime = new DateTime();
               appointment.dateTime.day = scanner.nextInt();
               System.out.println("Enter month:");
               appointment.dateTime.month = scanner.nextInt();
               System.out.println("Enter year:");
               appointment.dateTime.year = scanner.nextInt();
               System.out
                       .println("Enter time(Military time, 1:00pm is 1300, no colon):");
               appointment.dateTime.time = scanner.nextInt();
               listOfAppointment.add(appointment);
               System.out.println("Added:" + appointment.toString());
               saveFile(listOfAppointment);

           } else if (option == 2) {

           } else if (option == 3) {
               scanner.nextLine();// Consume newline left-over
               System.out.println("Enter date and time to look for in format : dd-MM-yyyy HH:mm");
               String date = scanner.nextLine();
               LinkedList<Appointment> list = searchListForDateTime(
                       listOfAppointment, date);
               if (list.isEmpty()) {
                   System.out.println("No appointments on: " + date);
               } else {
                   System.out.println("Appointments on: " + date);
                   System.out.println(list.toString());
               }
           } else if (option == 4) {
               scanner.nextLine();// Consume newline left-over

               System.out.println("Enter who:");
               String name = scanner.nextLine();
               LinkedList<Appointment> list = searchListForAPerson(
                       listOfAppointment, name);
               if (list.isEmpty()) {
                   System.out.println("No appointments with: " + name);
               } else {
                   System.out.println("Appointments with: " + name);
                   System.out.println(list.toString());
               }

           } else if (option == 5) {
               scanner.nextLine();// Consume newline left-over

               System.out.println("Enter day:");
               int day = scanner.nextInt();
               LinkedList<Appointment> list = searchListForADay(
                       listOfAppointment, day);
               if (list.isEmpty()) {
                   System.out.println("No appointments on day " + day);
               } else {
                   System.out.println("Appointments on day " + day);
                   System.out.println(list.toString());
               }
           } else if (option == 6) {
               Collections.sort(listOfAppointment);
               System.out.println(listOfAppointment);
           } else if (option == 7) {
               System.out.println("Saving Appointments!");
               saveFile(listOfAppointment);
               System.out.println("Quitting..");
               System.exit(0);
           }
       }
   }

   public static void saveFile(LinkedList<Appointment> listOfAppointments) {
       System.out.println("IN SAVEFILE");
       try {
           FileOutputStream fos = new FileOutputStream("appointment.txt");
           ObjectOutputStream oos = new ObjectOutputStream(fos);
           oos.writeObject(listOfAppointments);
           oos.close();
           fos.close();
       } catch (IOException ioe) {
           ioe.printStackTrace();
       }
   }

   public static LinkedList<Appointment> restoreList() {
       System.out.println("IN RESTORE");

       LinkedList<Appointment> listOfAppointments = new LinkedList<Appointment>();
       try {
           FileInputStream fis = new FileInputStream("appointment.txt");
           ObjectInputStream ois = new ObjectInputStream(fis);
           listOfAppointments = (LinkedList<Appointment>) ois.readObject();
           ois.close();
           fis.close();
       } catch (EOFException ex1) {
           return null;
       }

       catch (IOException ioe) {
           ioe.printStackTrace();
           return null;
       } catch (ClassNotFoundException c) {
           System.out.println("Class not found");
           c.printStackTrace();
           return null;
       }
       return listOfAppointments;
   }

   public static LinkedList<Appointment> searchListForAPerson(
           LinkedList<Appointment> listOfAppointments, String who) {
       LinkedList<Appointment> list = new LinkedList<Appointment>();
       list.addAll(listOfAppointments);
       for (int i = 0; i < listOfAppointments.size(); i++) {
           if (!listOfAppointments.get(i).who.equals(who)) {
               list.remove(i);
           }
       }
       return list;
   }

   public static LinkedList<Appointment> searchListForADay(
           LinkedList<Appointment> listOfAppointments, int day) {
       LinkedList<Appointment> list = new LinkedList<Appointment>();
       list.addAll(listOfAppointments);
       for (int i = 0; i < listOfAppointments.size(); i++) {
           if (listOfAppointments.get(i).dateTime.day != day) {
               list.remove(i);
           }
       }
       return list;
   }
   public static LinkedList<Appointment> searchListForDateTime(
           LinkedList<Appointment> listOfAppointments,String date) {
       SimpleDateFormat sdfDate = new SimpleDateFormat("dd-MM-yyyy HH:mm");
       Date date1 = null;
       try {
           date1 = sdfDate.parse(date);
           System.err.println(date.toString());
       } catch (ParseException e) {
           e.printStackTrace();
       }
       DateTime dateTime=new DateTime(date1.getDate(), date1.getMonth(), date1.getYear()+1900, Integer.parseInt(String.valueOf(date1.getHours())+String.valueOf(date1.getMinutes())));
       System.out.println(dateTime.toString());
       System.out.println(date1.getHours());
       System.out.println(Integer.parseInt(String.valueOf(date1.getHours())+String.valueOf(date1.getMinutes())));

       LinkedList<Appointment> list = new LinkedList<Appointment>();
       list.addAll(listOfAppointments);
       for (int i = 0; i < listOfAppointments.size(); i++) {
           if (!listOfAppointments.get(i).dateTime.equals(dateTime)) {
               list.remove(i);
           }
       }
       return list;
   }
}