Skip to content

Commit

Permalink
Merge pull request #348 from yentheng0110/YenTheng-CreateParserClass
Browse files Browse the repository at this point in the history
Create a Parser class for more OOP
  • Loading branch information
PrinceCatt authored Nov 9, 2024
2 parents e38d3c0 + 8859e99 commit 49eb9f3
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 188 deletions.
1 change: 1 addition & 0 deletions data/bookbob_data.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Name: John Doe | NRIC: S1234567A | Phone Number: 98765432 | Date_Of_Birth: | Home Address: | Allergy: [] | Sex: | Medical History: [] | Visit: [01-10-2024 15:30, Diagnosis: [], Medications: [], 21-10-2024 15:48, Diagnosis: [], Medications: []];
Name: Patricia Chan | NRIC: S8907890J | Phone Number: | Date_Of_Birth: | Home Address: | Allergy: [] | Sex: | Medical History: [] | Visit: [21-11-2024 18:00, Diagnosis: [], Medications: []];
18 changes: 18 additions & 0 deletions logs/logs.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Nov 09, 2024 7:14:04 PM bookbob.Main logAndExecute
INFO: Processing help command
Nov 09, 2024 7:14:04 PM bookbob.Main logAndExecute
INFO: Successfully processed help command
Nov 09, 2024 7:16:38 PM bookbob.Main logAndExecute
INFO: Processing list command
Nov 09, 2024 7:16:38 PM bookbob.Main logAndExecute
INFO: Successfully processed list command
Nov 09, 2024 7:16:53 PM bookbob.Main validateParameters
INFO: Missing required parameter: ic/
Nov 09, 2024 7:17:02 PM bookbob.Main validateParameters
INFO: Missing required parameter: n/
Nov 09, 2024 7:17:25 PM bookbob.Main logAndExecute
INFO: Processing add command
Nov 09, 2024 7:17:25 PM bookbob.Main logAndExecute
INFO: Successfully processed add command
Nov 09, 2024 7:17:47 PM bookbob.Main logAndExecute
INFO: Processing exit command
190 changes: 2 additions & 188 deletions src/main/java/bookbob/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
import bookbob.entity.AppointmentRecord;
import bookbob.functions.CommandHandler;
import bookbob.functions.FileHandler;
import bookbob.parser.Parser;

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;
Expand Down Expand Up @@ -43,37 +42,6 @@ private static void loggerConfig() {
}
}

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();
Expand All @@ -87,161 +55,7 @@ public static void main(String[] args) throws IOException {

while (true) {
String input = in.nextLine();
String[] inputArray = input.split(" ", 2);
String command = inputArray[0];

switch (command) {
case "find":
logAndExecute("find", () -> commandHandler.find(input, records));
break;

case "exit":
logAndExecute("exit", () -> {
try {
commandHandler.removePastAppointments(appointmentRecord);
} catch (IOException e) {
throw new RuntimeException(e);
}
commandHandler.exit(input);
});
break;

case "add":
if (validateParameters(input, "n/", "ic/", "v/")) {
logAndExecute("add", () -> {
try {
commandHandler.add(input, records);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
break;

case "list":
logAndExecute("list", () -> commandHandler.list(records));
break;

case "edit":
logAndExecute("edit", () -> {
try {
commandHandler.edit(input, records);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
break;

case "editVisit":
logAndExecute("editVisit", () -> {
try {
commandHandler.editVisit(input, records);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
break;

case "delete":
logAndExecute("delete", () -> {
if (inputArray.length > 1) {
String nric = inputArray[1].trim();
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 for delete command");
}
});
break;

case "help":
logAndExecute("help", commandHandler::help);
break;

case "appointment":
if (validateParameters(input, "n/", "ic/", "date/", "time/")) {
logAndExecute("appointment", () -> {
try {
commandHandler.appointment(input, appointmentRecord);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
break;

case "listAppointments":
logAndExecute("listAppointments", () -> commandHandler.listAppointments(appointmentRecord));
break;

case "deleteAppointment":
logAndExecute("deleteAppointment", () -> {
try {
commandHandler.deleteAppointment(input, appointmentRecord);
} catch (IOException e) {
throw new RuntimeException(e);
}
});
break;

case "findAppointment":
if (inputArray.length > 1) {
logAndExecute("findAppointment", () -> commandHandler.
findAppointment(inputArray[1], appointmentRecord));
} else {
System.out.println("Please provide search criteria for findAppointment.");
logger.log(Level.INFO, "Missing criteria for findAppointment");
}
break;

case "findVisit":
if (inputArray.length > 1) {
logAndExecute("findVisit", () -> commandHandler.findVisitByIc(inputArray[1], records));
} else {
System.out.println("Please provide NRIC for findVisit.");
logger.log(Level.INFO, "Missing NRIC for findVisit");
}
break;

case "findMedication":
if (inputArray.length > 1) {
logAndExecute("findMedication", () -> commandHandler.findVisitByMedication(inputArray[1], records));
} else {
System.out.println("Please provide medication for findMedication.");
logger.log(Level.INFO, "Missing medication for findMedication");
}
break;

case "findDiagnosis":
if (inputArray.length > 1) {
logAndExecute("findDiagnosis", () -> commandHandler.findVisitByDiagnosis(inputArray[1], records));
} else {
System.out.println("Please provide diagnosis for findDiagnosis.");
logger.log(Level.INFO, "Missing diagnosis for findDiagnosis");
}
break;

case "addVisit":
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;
}
Parser.handleCommand(input, commandHandler, records, appointmentRecord);
}
}
}
Loading

0 comments on commit 49eb9f3

Please sign in to comment.