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

Comment this code using javadoc: import java.io.File; import java.io.FileNotFoun

ID: 3696030 • Letter: C

Question

Comment this code using javadoc:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import bcs345.hwk.schedule.business.Appointment;
import bcs345.hwk.schedule.business.PersonAppointments;

public class PersonAppointmentsApplication extends Application {
   protected PersonAppointments PersonAppointments = new PersonAppointments();
   protected int apptCountNumber;
   private TextField firstName;
   private TextField lastName;
   private TextField apptCount;
   VBox vBox = new VBox();

   private ObservableList appointments = FXCollections.observableArrayList();

   @Override
   public void start(Stage primaryStage) throws Exception {
       primaryStage.setTitle("Person Appointments");

       createMenu(primaryStage);
       createTabs();
       Scene scene = new Scene(vBox, 400, 350, Color.WHITE);
       primaryStage.setScene(scene);

       primaryStage.show();
   }

   private void createMenu(Stage primaryStage) {
       MenuBar menuBar = new MenuBar();
       vBox.getChildren().add(menuBar);

       // --- Menu File
       Menu fileMenu = new Menu("File");

       MenuItem openItem = new MenuItem("Open...");
       MenuItem saveAsItem = new MenuItem("Save As...");
       MenuItem saveReportItem = new MenuItem("Save Report...");
       MenuItem exitItem = new MenuItem("Exit");
       fileMenu.getItems().addAll(openItem, new SeparatorMenuItem(), saveAsItem, saveReportItem,
               new SeparatorMenuItem(), exitItem);

       menuBar.getMenus().addAll(fileMenu);

       openItem.setOnAction(new EventHandler() {
           @Override
           public void handle(ActionEvent Event) {
               String currentDir = System.getProperty("user.dir") + File.separator;
               FileChooser fileChooser = new FileChooser();
               fileChooser.setTitle("Open PersonAppointments File");
               fileChooser.setInitialDirectory(new File(currentDir));
               File selectedFile = fileChooser.showOpenDialog(primaryStage);
               if (selectedFile != null) {
                   Scanner s;
                   try {
                       s = new Scanner(selectedFile);
                       PersonAppointments.Read(s);
                       s.close();
                       fillGuiComponents();
                   } catch (FileNotFoundException e) {
                       e.printStackTrace();
                   }
               }
           }
       });

       saveAsItem.setOnAction(new EventHandler() {
           @Override
           public void handle(ActionEvent Event) {
               String currentDir = System.getProperty("user.dir") + File.separator;
               FileChooser fileChooser = new FileChooser();
               fileChooser.setTitle("Save As");
               fileChooser.setInitialDirectory(new File(currentDir));
               File selectedFile = fileChooser.showSaveDialog(primaryStage);
               if (selectedFile != null) {
                   try {
                       PrintStream ps = new PrintStream(selectedFile);
                       fillPersonAppointments();
                       PersonAppointments.Write(ps);
                       ps.close();
                   } catch (FileNotFoundException e) {
                       e.printStackTrace();
                   }
               }
           }
       });

       saveReportItem.setOnAction(new EventHandler() {
           @Override
           public void handle(ActionEvent Event) {
               String currentDir = System.getProperty("user.dir") + File.separator;
               FileChooser fileChooser = new FileChooser();
               fileChooser.setTitle("Save As");
               fileChooser.setInitialDirectory(new File(currentDir));
               File selectedFile = fileChooser.showSaveDialog(primaryStage);
               if (selectedFile != null) {
                   try {
                       PrintStream ps = new PrintStream(selectedFile);
                       fillPersonAppointments();
                       PersonAppointments.Report(ps);
                       ps.close();
                   } catch (FileNotFoundException e) {
                       e.printStackTrace();
                   }
               }
           }
       });

       exitItem.setOnAction(new EventHandler() {
           @Override
           public void handle(ActionEvent Event) {
               Platform.exit();
           }
       });
   }

   private void createTabs() {
       TabPane tabPane = new TabPane();
       tabPane.getTabs().addAll(createPersonInfoTab(), createAppointmentsTab());
       vBox.getChildren().add(tabPane);
   }

   private Tab createPersonInfoTab() {
       Tab pInfoTab = new Tab("Person Info");
       pInfoTab.setClosable(false);

       GridPane grid = new GridPane();
       grid.setPadding(new Insets(10, 10, 10, 10));
       grid.setVgap(3);
       grid.setHgap(3);

       grid.add(new Label("First name"), 0, 0);

       firstName = new TextField();
       firstName.setPrefColumnCount(10);
       grid.add(firstName, 1, 0);
       grid.add(new Label("Last name"), 0, 1);

       lastName = new TextField();
       grid.add(lastName, 1, 1);
       grid.add(new Label("Appt count"), 0, 2);

       apptCount = new TextField();
       apptCount.setEditable(false);
       grid.add(apptCount, 1, 2);

       pInfoTab.setContent(grid);
       return pInfoTab;
   }

   private Tab createAppointmentsTab() {
       Tab appTab = new Tab("Appointments");
       appTab.setClosable(false);
       ListView listView = new ListView();
       listView.setPrefSize(400, 290);
       listView.setItems(appointments);
       appTab.setContent(listView);
       return appTab;

   }

   private void fillGuiComponents() {
       firstName.setText(PersonAppointments.getFirst());
       lastName.setText(PersonAppointments.getLast());
       fillList();
       apptCount.setText(apptCountNumber > 0 ? String.valueOf(apptCountNumber) : "");

   }

   private void fillPersonAppointments() {
       PersonAppointments.setFirst(firstName.getText());
       PersonAppointments.setLast(lastName.getText());
   }

   private void fillList() {
       apptCountNumber = 0;
       while (true) {
           try {
               Appointment app = PersonAppointments.GetByIndex(apptCountNumber);
               if (app != null) {
                   appointments.add(app);
                   apptCountNumber++;
               } else {
                   break;
               }
           } catch (ArrayIndexOutOfBoundsException | NullPointerException e) {
               break;
           }
       }
   }

}

Explanation / Answer

package com.chegg.arrays;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import bcs345.hwk.schedule.business.Appointment;
import bcs345.hwk.schedule.business.PersonAppointments;

/**
* <p>
* The PersonAppointmentsApplication program implements an application for
* Person Appointment Info that have a GUI interface by name Person Appointments
* with Menu Bar and 2 tab panes(Person Info and Appointments).When user chooses
* file menu option open it will read a file and populates the
* PersonAppointments object.when user chooses SaveAs option it will generate a
* file with appointment information.when user chooses SaveReport option it will
* create a report with appointment information.when user chooses exit it will
* exit GUI.
* </p>
*
*
*
* @author yourName
* @version 1.0
* @since 2016-04-29
*/
public class PersonAppointmentsApplication extends Application {
   protected PersonAppointments PersonAppointments = new PersonAppointments();
   protected int apptCountNumber;
   private TextField firstName;
   private TextField lastName;
   private TextField apptCount;
   VBox vBox = new VBox();
   private ObservableList appointments = FXCollections.observableArrayList();

   /**
   * This method is used to create GUI for user interaction. The GUI contains
   * Menu bar having the following File Menu options (Open...,Save AS...,Save
   * Report...,Exit).And it also contains two Tabs for Person Info and
   * Appointments.
   *
   * @param primaryStage
   * @throws Exception
   *
   */
   @Override
   public void start(Stage primaryStage) throws Exception {
       primaryStage.setTitle("Person Appointments");
       createMenu(primaryStage);
       createTabs();
       Scene scene = new Scene(vBox, 400, 350, Color.WHITE);
       primaryStage.setScene(scene);
       primaryStage.show();
   }

   /**
   * This method is used to create Menu bar having the following File Menu
   * options (Open...,Save AS...,Save Report...,Exit). And each Menu Option
   * has an event handler to perform appropriate task,like open option gives a
   * dialog box to open a file from the disk,SaveAs option gives a dialog box
   * to store the file at particular place in the disk,SaveReport option gives
   * a dialog box to save the generated report file at particular place on the
   * disk,Exit option for existing the GUI interface.
   *
   * @param primaryStage
   *
   */
   private void createMenu(Stage primaryStage) {
       MenuBar menuBar = new MenuBar();
       vBox.getChildren().add(menuBar);
       // --- Menu File
       Menu fileMenu = new Menu("File");
       MenuItem openItem = new MenuItem("Open...");
       MenuItem saveAsItem = new MenuItem("Save As...");
       MenuItem saveReportItem = new MenuItem("Save Report...");
       MenuItem exitItem = new MenuItem("Exit");
       fileMenu.getItems().addAll(openItem, new SeparatorMenuItem(),
               saveAsItem, saveReportItem, new SeparatorMenuItem(), exitItem);
       menuBar.getMenus().addAll(fileMenu);
       openItem.setOnAction(new EventHandler() {
           @Override
           public void handle(ActionEvent Event) {
               String currentDir = System.getProperty("user.dir")
                       + File.separator;
               FileChooser fileChooser = new FileChooser();
               fileChooser.setTitle("Open PersonAppointments File");
               fileChooser.setInitialDirectory(new File(currentDir));
               File selectedFile = fileChooser.showOpenDialog(primaryStage);
               if (selectedFile != null) {
                   Scanner s;
                   try {
                       s = new Scanner(selectedFile);
                       PersonAppointments.Read(s);
                       s.close();
                       fillGuiComponents();
                   } catch (FileNotFoundException e) {
                       e.printStackTrace();
                   }
               }
           }
       });
       saveAsItem.setOnAction(new EventHandler() {
           @Override
           public void handle(ActionEvent Event) {
               String currentDir = System.getProperty("user.dir")
                       + File.separator;
               FileChooser fileChooser = new FileChooser();
               fileChooser.setTitle("Save As");
               fileChooser.setInitialDirectory(new File(currentDir));
               File selectedFile = fileChooser.showSaveDialog(primaryStage);
               if (selectedFile != null) {
                   try {
                       PrintStream ps = new PrintStream(selectedFile);
                       fillPersonAppointments();
                       PersonAppointments.Write(ps);
                       ps.close();
                   } catch (FileNotFoundException e) {
                       e.printStackTrace();
                   }
               }
           }
       });
       saveReportItem.setOnAction(new EventHandler() {
           @Override
           public void handle(ActionEvent Event) {
               String currentDir = System.getProperty("user.dir")
                       + File.separator;
               FileChooser fileChooser = new FileChooser();
               fileChooser.setTitle("Save As");
               fileChooser.setInitialDirectory(new File(currentDir));
               File selectedFile = fileChooser.showSaveDialog(primaryStage);
               if (selectedFile != null) {
                   try {
                       PrintStream ps = new PrintStream(selectedFile);
                       fillPersonAppointments();
                       PersonAppointments.Report(ps);
                       ps.close();
                   } catch (FileNotFoundException e) {
                       e.printStackTrace();
                   }
               }
           }
       });
       exitItem.setOnAction(new EventHandler() {
           @Override
           public void handle(ActionEvent Event) {
               Platform.exit();
           }
       });
   }

   /**
   * This method is used to create Tab panes in the GUI interface. User can
   * switch to appropriate PersonInfo or Appointments tabs for entering the
   * required data
   *
   */
   private void createTabs() {
       TabPane tabPane = new TabPane();
       tabPane.getTabs()
               .addAll(createPersonInfoTab(), createAppointmentsTab());
       vBox.getChildren().add(tabPane);
   }

   /**
   * This method is used to create Person Info tab for GUI interface. This tab
   * display first name,last name and appCount text fields to the user
   *
   * @return pInfoTab
   */
   private Tab createPersonInfoTab() {
       Tab pInfoTab = new Tab("Person Info");
       pInfoTab.setClosable(false);
       GridPane grid = new GridPane();
       grid.setPadding(new Insets(10, 10, 10, 10));
       grid.setVgap(3);
       grid.setHgap(3);
       grid.add(new Label("First name"), 0, 0);
       firstName = new TextField();
       firstName.setPrefColumnCount(10);
       grid.add(firstName, 1, 0);
       grid.add(new Label("Last name"), 0, 1);
       lastName = new TextField();
       grid.add(lastName, 1, 1);
       grid.add(new Label("Appt count"), 0, 2);
       apptCount = new TextField();
       apptCount.setEditable(false);
       grid.add(apptCount, 1, 2);
       pInfoTab.setContent(grid);
       return pInfoTab;
   }

   /**
   * This method is used to create Appointments tab for GUI interface.when the
   * user switch to this tab it displays all the existing appointment
   * information to the user
   *
   * @return appTab
   */
   private Tab createAppointmentsTab() {
       Tab appTab = new Tab("Appointments");
       appTab.setClosable(false);
       ListView listView = new ListView();
       listView.setPrefSize(400, 290);
       listView.setItems(appointments);
       appTab.setContent(listView);
       return appTab;
   }

   /**
   * This method is used to display information in Person Info tab components
   * such as firstName,lastName and appCount by taking from PersonAppointments
   * object which is populated by taking the values from file.For appCount
   * value it is calling fillList method.
   *
   */
   private void fillGuiComponents() {
       firstName.setText(PersonAppointments.getFirst());
       lastName.setText(PersonAppointments.getLast());
       fillList();
       apptCount.setText(apptCountNumber > 0 ? String.valueOf(apptCountNumber)
               : "");
   }

   /**
   * This method is used to populate PersonAppointments object by taking the
   * values from Person Info tab components firstName and lastName, in order
   * to persist the object into a report file
   *
   */
   private void fillPersonAppointments() {
       PersonAppointments.setFirst(firstName.getText());
       PersonAppointments.setLast(lastName.getText());
   }

   /**
   * This method is used to add appointment count to the PersonAppointments
   * object at 0th position and increments the appCount.
   */
   private void fillList() {
       apptCountNumber = 0;
       while (true) {
           try {
               Appointment app = PersonAppointments
                       .GetByIndex(apptCountNumber);
               if (app != null) {
                   appointments.add(app);
                   apptCountNumber++;
               } else {
                   break;
               }
           } catch (ArrayIndexOutOfBoundsException | NullPointerException e) {
               break;
           }
       }
   }
}