From 7d9413b7cffb6146db37b1b5c1d401ae92c83846 Mon Sep 17 00:00:00 2001 From: PrinceCatt <2991588053@qq.com> Date: Sat, 9 Nov 2024 13:05:14 +0800 Subject: [PATCH] before_pull --- logs/logs.log | 0 src/main/java/bookbob/Main.java | 364 +++++++++++++------------------- 2 files changed, 147 insertions(+), 217 deletions(-) create mode 100644 logs/logs.log diff --git a/logs/logs.log b/logs/logs.log new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/main/java/bookbob/Main.java b/src/main/java/bookbob/Main.java index 18a260c70e..c3342fd0b3 100644 --- a/src/main/java/bookbob/Main.java +++ b/src/main/java/bookbob/Main.java @@ -5,18 +5,78 @@ import bookbob.functions.CommandHandler; import bookbob.functions.FileHandler; +import java.io.File; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; import java.time.DateTimeException; import java.time.format.DateTimeParseException; import java.util.Scanner; +import java.util.logging.ConsoleHandler; +import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; public class Main { private static final Logger logger = Logger.getLogger("Main.class"); + private static final String directoryName = "logs"; + private static final String logFilePath = directoryName + "/logs.log"; + + private static void loggerConfig() { + try { + String currentDirectory = System.getProperty("user.dir"); + String directory = currentDirectory + File.separator + directoryName; + Files.createDirectories(Paths.get(directoryName)); + java.util.logging.FileHandler fh = new java.util.logging.FileHandler(logFilePath, true); + fh.setFormatter(new java.util.logging.SimpleFormatter()); + fh.setLevel(Level.FINE); + logger.addHandler(fh); + Logger rootLogger = Logger.getLogger(""); + Handler[] handlers = rootLogger.getHandlers(); + for (Handler handler : handlers) { + if (handler instanceof ConsoleHandler) { + rootLogger.removeHandler(handler); + } + } + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private static void logAndExecute(String commandName, Runnable action) { + logger.log(Level.INFO, "Processing {0} command", commandName); + try { + action.run(); + logger.log(Level.INFO, "Successfully processed {0} command", commandName); + } catch (IllegalArgumentException e) { + logger.log(Level.WARNING, "Invalid input for {0} command: {1}", new Object[]{commandName, e.getMessage()}); + System.out.println("Invalid input: " + e.getMessage()); + } catch (DateTimeParseException e) { + logger.log(Level.WARNING, "Error in {0} command: incorrect date format", commandName); + System.out.println("Error: incorrect date format."); + } catch (DateTimeException e) { + logger.log(Level.WARNING, "Error in {0} command: incorrect time format", commandName); + System.out.println("Error: incorrect time format."); + } catch (Exception e) { + logger.log(Level.SEVERE, "Unexpected error processing {0} command", e); + System.out.println("An unexpected error occurred: " + e.getMessage()); + } + } + + private static boolean validateParameters(String input, String... requiredMarkers) { + for (String marker : requiredMarkers) { + if (!input.contains(marker)) { + System.out.println("Please provide the required parameter: " + marker); + logger.log(Level.INFO, "Missing required parameter: {0}", marker); + return false; + } + } + return true; + } public static void main(String[] args) throws IOException { System.out.println("Welcome to BookBob, Dr. Bob!"); + loggerConfig(); Scanner in = new Scanner(System.in); Records records = new Records(); AppointmentRecord appointmentRecord = new AppointmentRecord(); @@ -32,284 +92,154 @@ public static void main(String[] args) throws IOException { switch (command) { case "find": - logger.log(Level.INFO, "Processing find command"); - try { - commandHandler.find(input, records); - logger.log(Level.INFO, "Successfully processed find command"); - } catch (IllegalArgumentException e) { - logger.log(Level.WARNING, "Invalid input for find command: {0}", e.getMessage()); - System.out.println("Invalid input: " + e.getMessage()); - e.printStackTrace(); // prints the stack trace to the console - } catch (Exception e) { - logger.log(Level.SEVERE, "Error processing find command", e); - System.out.println("An error occurred while processing the find command: " + e.getMessage()); - } + logAndExecute("find", () -> commandHandler.find(input, records)); break; case "exit": - logger.log(Level.INFO, "Processing exit command"); - try{ - commandHandler.removePastAppointments(appointmentRecord); + logAndExecute("exit", () -> { + try { + commandHandler.removePastAppointments(appointmentRecord); + } catch (IOException e) { + throw new RuntimeException(e); + } commandHandler.exit(input); - logger.log(Level.INFO, "Successfully processed exit command"); - } catch (Exception e) { - logger.log(Level.WARNING, "Error processing exit", e); - } + }); break; case "add": - logger.log(Level.INFO, "Processing add command"); - - try { - int nameStart = input.indexOf("n/"); - int nricStart = input.indexOf("ic/"); - int visitStart = input.indexOf("v/"); - - if (nameStart == -1) { - System.out.println("Please provide the patient's name."); - logger.log(Level.INFO, "Name of the patient is not provided"); - break; - } - - if (nricStart == -1) { - System.out.println("Please provide the patient's NRIC."); - logger.log(Level.INFO, "NRIC of the patient is not provided"); - break; - - } - - if (visitStart == -1) { - System.out.println("Please provide the visit date."); - logger.log(Level.INFO, "Visit date is not provided"); - break; - } - - commandHandler.add(input, records); - logger.log(Level.INFO, "Successfully processed add command"); - } catch (Exception e) { - logger.log(Level.WARNING, "Error processing add command", e); - System.out.println("Error in adding patient record, specific error: " + e.getMessage()); + if (validateParameters(input, "n/", "ic/", "v/")) { + logAndExecute("add", () -> { + try { + commandHandler.add(input, records); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); } break; case "list": - logger.log(Level.INFO, "Processing list command"); - try { - commandHandler.list(records); - logger.log(Level.INFO, "Successfully processed list command"); - } catch (Exception e) { - logger.log(Level.WARNING, "Error processing list", e); - System.out.println("Error in listing patient record, specific error: " + e.getMessage()); - } + logAndExecute("list", () -> commandHandler.list(records)); break; case "edit": - logger.log(Level.INFO, "Processing edit command"); - try { - commandHandler.edit(input, records); - logger.log(Level.INFO, "Successfully processed edit command"); - } catch (Exception e) { - logger.log(Level.WARNING, "Error processing edit", e); - System.out.println("Error in editing patient record, specific error: " + e.getMessage()); - } + logAndExecute("edit", () -> { + try { + commandHandler.edit(input, records); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); break; case "editVisit": - logger.log(Level.INFO, "Processing editVisit command"); - try { - commandHandler.editVisit(input, records); - logger.log(Level.INFO, "Successfully processed editVisit command"); - } catch (Exception e) { - logger.log(Level.WARNING, "Error processing editVisit", e); - System.out.println("Error in editing patient visit, specific error: " + e.getMessage()); - } + logAndExecute("editVisit", () -> { + try { + commandHandler.editVisit(input, records); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); break; case "delete": - logger.log(Level.INFO, "Processing delete command"); - try { + logAndExecute("delete", () -> { if (inputArr.length > 1) { String nric = inputArr[1].trim(); - commandHandler.delete(nric, records); - logger.log(Level.INFO, "Successfully processed delete command"); + try { + commandHandler.delete(nric, records); + } catch (IOException e) { + throw new RuntimeException(e); + } } else { System.out.println("Please specify an NRIC to delete."); - logger.log(Level.INFO, "Empty NRIC inputted"); + logger.log(Level.INFO, "Empty NRIC inputted for delete command"); } - } catch (Exception e) { - logger.log(Level.WARNING, "Error processing deletion"); - System.out.println("Error in deleting files, specific error: " + e.getMessage()); - } + }); break; case "help": - logger.log(Level.INFO, "Processing help command"); - try{ - commandHandler.help(); - logger.log(Level.INFO, "Successfully processed help command"); - } catch (Exception e) { - logger.log(Level.WARNING, "Error processing help", e); - } + logAndExecute("help", commandHandler::help); break; case "appointment": - logger.log(Level.INFO, "Processing appointment command"); - try { - int nameStart = input.indexOf("n/"); - int nricStart = input.indexOf("ic/"); - int dateStart = input.indexOf("date/"); - int timeStart = input.indexOf("time/"); - - if (nameStart == -1) { - System.out.println("Please provide the patient's name."); - logger.log(Level.INFO, "Name of the patient is not provided"); - break; - } - - if (nricStart == -1) { - System.out.println("Please provide the patient's NRIC."); - logger.log(Level.INFO, "NRIC of the patient is not provided"); - break; - } - - if (dateStart == -1) { - System.out.println("Please provide the date."); - logger.log(Level.INFO, "Date of the appointment is not provided"); - break; - } - - if (timeStart == -1) { - System.out.println("Please provide the time."); - logger.log(Level.INFO, "Time of the appointment is not provided"); - break; - } - - commandHandler.appointment(input, appointmentRecord); - } catch (DateTimeParseException e) { - logger.log(Level.WARNING, "Error processing appointment command", e); - System.out.println("Error in adding appointment, wrong date format"); - } catch (DateTimeException e) { - logger.log(Level.WARNING, "Error processing appointment command", e); - System.out.println("Error in adding appointment, wrong time format"); - } catch (Exception e) { - logger.log(Level.WARNING, "Error processing appointment command", e); - System.out.println("Error in adding appointment, specific error: " + e.getMessage()); + if (validateParameters(input, "n/", "ic/", "date/", "time/")) { + logAndExecute("appointment", () -> { + try { + commandHandler.appointment(input, appointmentRecord); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); } break; case "listAppointments": - logger.log(Level.INFO, "Processing list appointments command"); - try{ - commandHandler.listAppointments(appointmentRecord); - logger.log(Level.INFO, "Successfully processed list appointments command"); - } catch (DateTimeParseException e) { - logger.log(Level.WARNING, "Error processing appointment command", e); - System.out.println("Error in adding appointment, wrong date format"); - } catch (DateTimeException e) { - logger.log(Level.WARNING, "Error processing appointment command", e); - System.out.println("Error in adding appointment, wrong time format"); - } catch (Exception e) { - logger.log(Level.WARNING, "Error processing list appointments", e); - System.out.println("Error in listing appointments, specific error: " + e.getMessage()); - } + logAndExecute("listAppointments", () -> commandHandler.listAppointments(appointmentRecord)); break; case "deleteAppointment": - logger.log(Level.INFO, "Processing delete appointment command"); - try { - commandHandler.deleteAppointment(input, appointmentRecord); - logger.log(Level.INFO, "Successfully processed deletion of appointment command"); - } catch (DateTimeParseException e) { - logger.log(Level.WARNING, "Error processing deletion of appointment command", e); - System.out.println("Error in adding appointment, wrong date format"); - } catch (DateTimeException e) { - logger.log(Level.WARNING, "Error processing deletion of appointment command", e); - System.out.println("Error in adding appointment, wrong time format"); - } catch (IllegalArgumentException e) { - logger.log(Level.WARNING, "Error processing deletion of appointment command"); - System.out.println("Error in deleting appointment, missing nric, date or time."); - } catch (Exception e) { - logger.log(Level.WARNING, "Error processing deletion of appointment"); - System.out.println("Error in deleting appointment, specific error: " + e.getMessage()); - } + logAndExecute("deleteAppointment", () -> { + try { + commandHandler.deleteAppointment(input, appointmentRecord); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); break; case "findAppointment": - logger.log(Level.INFO, "Processing find appointment command"); - try { - commandHandler.findAppointment(inputArr[1], appointmentRecord); - logger.log(Level.INFO, "Successfully processed find appointment command"); - } catch (DateTimeParseException e) { - logger.log(Level.WARNING, "Error processing appointment command", e); - System.out.println("Error in adding appointment, wrong date format"); - } catch (DateTimeException e) { - logger.log(Level.WARNING, "Error processing appointment command", e); - System.out.println("Error in adding appointment, wrong time format"); - } catch (Exception e) { - logger.log(Level.SEVERE, "Error processing find command", e); - System.out.println("An error occurred while processing the find command: " + e.getMessage()); + if (inputArr.length > 1) { + logAndExecute("findAppointment", () -> commandHandler. + findAppointment(inputArr[1], appointmentRecord)); + } else { + System.out.println("Please provide search criteria for findAppointment."); + logger.log(Level.INFO, "Missing criteria for findAppointment"); } break; case "findVisit": - logger.log(Level.INFO, "Processing find visit command"); - try { - commandHandler.findVisitByIc(inputArr[1], records); - logger.log(Level.INFO, "Successfully processed find visit command"); - } catch (Exception e) { - logger.log(Level.WARNING, "Error processing find visit command", e); + if (inputArr.length > 1) { + logAndExecute("findVisit", () -> commandHandler.findVisitByIc(inputArr[1], records)); + } else { + System.out.println("Please provide NRIC for findVisit."); + logger.log(Level.INFO, "Missing NRIC for findVisit"); } break; case "findMedication": - logger.log(Level.INFO, "Processing find medication command"); - try { - commandHandler.findVisitByMedication(inputArr[1], records); - logger.log(Level.INFO, "Successfully processed find medication command"); - } catch (Exception e) { - logger.log(Level.WARNING, "Error processing find medication command", e); + if (inputArr.length > 1) { + logAndExecute("findMedication", () -> commandHandler.findVisitByMedication(inputArr[1], records)); + } else { + System.out.println("Please provide medication for findMedication."); + logger.log(Level.INFO, "Missing medication for findMedication"); } break; - case "findDiagnosis": - logger.log(Level.INFO, "Processing find diagnosis command"); - try { - commandHandler.findVisitByDiagnosis(inputArr[1], records); - logger.log(Level.INFO, "Successfully processed find diagnosis command"); - } catch (Exception e) { - logger.log(Level.WARNING, "Error processing find diagnosis command", e); + if (inputArr.length > 1) { + logAndExecute("findDiagnosis", () -> commandHandler.findVisitByDiagnosis(inputArr[1], records)); + } else { + System.out.println("Please provide diagnosis for findDiagnosis."); + logger.log(Level.INFO, "Missing diagnosis for findDiagnosis"); } break; case "addVisit": - logger.log(Level.INFO, "Processing addVisit command"); - try { - // Check for required fields - int nricStart = input.indexOf("ic/"); - int visitStart = input.indexOf("v/"); - - if (nricStart == -1) { - System.out.println("Please provide the patient's NRIC."); - logger.log(Level.INFO, "NRIC not provided"); - break; - } - - if (visitStart == -1) { - System.out.println("Please provide the visit date and time."); - logger.log(Level.INFO, "Visit date/time not provided"); - break; - } - - commandHandler.addVisit(input, records); - logger.log(Level.INFO, "Successfully processed addVisit command"); - } catch (Exception e) { - logger.log(Level.WARNING, "Error processing addVisit command", e); - System.out.println("Error processing addVisit command: " + e.getMessage()); + if (validateParameters(input, "ic/", "v/")) { + logAndExecute("addVisit", () -> { + try { + commandHandler.addVisit(input, records); + } catch (IOException e) { + throw new RuntimeException(e); + } + }); } break; + default: System.out.println("Unknown command. Type 'help' for a list of commands."); + logger.log(Level.INFO, "Unknown command received: {0}", command); break; } }