Comment this program in @Javadoc: package bcs345.hwk.schedule.presentation; impo
ID: 3680101 • Letter: C
Question
Comment this program in @Javadoc:
package bcs345.hwk.schedule.presentation;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import bcs345.hwk.schedule.business.Appointment;
public class AppointmentConsoleUI {
public void ShowUI() {
Appointment appt = new Appointment();
Scanner scan = new Scanner(System.in);
char option;
while (true) {
System.out.println(" Appointment UI");
System.out.println("----------------");
System.out.println("1 - Read appt from file");
System.out.println("2 - Write appt to file");
System.out.println("3 - Show appt date on screen");
System.out.println("4 - Show appt time on screen");
System.out.println("5 - Show appt JSON string on screen");
System.out.println("6 - Show all appt data on screen");
System.out.println("7 - Exit");
System.out.print(" Enter choice: ");
option = scan.nextLine().charAt(0);
switch (option) {
case '1':
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Load which file?");
int result = chooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
Scanner s;
try {
s = new Scanner(chooser.getSelectedFile());
appt.Read(s);
s.close();
} catch (FileNotFoundException e) {
System.err.println("File not found. ");
break;
}
} else
System.out.println("File selection canceled. ");
break;
case '2':
chooser = new JFileChooser();
chooser.setDialogTitle("Save to file?");
result = chooser.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
try {
PrintStream ps = new PrintStream(chooser.getSelectedFile());
appt.Write(ps);
ps.close();
} catch (FileNotFoundException e) {
System.err.println("File not found. ");
break;
}
} else
System.out.println("File selection canceled. ");
break;
case '3':
System.out.println(appt.getDate());
break;
case '4':
System.out.println(appt.getTime());
break;
case '5':
System.out.println(appt.GetJSON());
break;
case '6':
System.out.println(appt);
break;
case '7':
return;
default:
System.err.println("Invalid option.");
}
}
Explanation / Answer
package bcs345.hwk.schedule.presentation;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import bcs345.hwk.schedule.business.Appointment;
public class AppointmentConsoleUI {
/** Method ShowUI accepts the input from console and performs some file operation based on *selected option
*/
public void ShowUI() {
Appointment appt = new Appointment();
Scanner scan = new Scanner(System.in);
char option;
while (true) {
System.out.println(" Appointment UI");
System.out.println("----------------");
System.out.println("1 - Read appt from file");
System.out.println("2 - Write appt to file");
System.out.println("3 - Show appt date on screen");
System.out.println("4 - Show appt time on screen");
System.out.println("5 - Show appt JSON string on screen");
System.out.println("6 - Show all appt data on screen");
System.out.println("7 - Exit");
System.out.print(" Enter choice: ");
option = scan.nextLine().charAt(0);
switch (option) {
case '1':
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Load which file?");
int result = chooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
Scanner s;
try {
s = new Scanner(chooser.getSelectedFile());
appt.Read(s);
s.close();
} catch (FileNotFoundException e) {
System.err.println("File not found. ");
break;
}
} else
System.out.println("File selection canceled. ");
break;
case '2':
chooser = new JFileChooser();
chooser.setDialogTitle("Save to file?");
result = chooser.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
try {
PrintStream ps = new PrintStream(chooser.getSelectedFile());
appt.Write(ps);
ps.close();
} catch (FileNotFoundException e) {
System.err.println("File not found. ");
break;
}
} else
System.out.println("File selection canceled. ");
break;
case '3':
System.out.println(appt.getDate());
break;
case '4':
System.out.println(appt.getTime());
break;
case '5':
System.out.println(appt.GetJSON());
break;
case '6':
System.out.println(appt);
break;
case '7':
return;
default:
System.err.println("Invalid option.");
}
}