Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assertion, Logging and Exception #93

Merged
merged 8 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/main/java/bookbob/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@

import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {
private static final Logger logger = Logger.getLogger("Main.class");

public static void main(String[] args) throws IOException {
System.out.println("Welcome to BookBob, Dr. Bob!");

Expand Down Expand Up @@ -40,12 +44,20 @@ public static void main(String[] args) throws IOException {
break;

case "delete":
if (inputArr.length > 1) {
String nric = inputArr[1].trim();
commandHandler.delete(nric, records);
} else {
System.out.println("Please specify an NRIC to delete.");
logger.log(Level.INFO, "Processing patient record deletion");
try {
if (inputArr.length > 1) {
String nric = inputArr[1].trim();
commandHandler.delete(nric, records);
} else {
System.out.println("Please specify an NRIC to delete.");
logger.log(Level.INFO, "Empty NRIC inputted.");
}
} catch (Exception e) {
logger.log(Level.WARNING, "Error processing deletion");
System.out.println("Error in deleting files, specific error: " + e.getMessage());
Comment on lines +56 to +58

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good use of catch statement to catch an exception processing the delete command and print out the specific error message

}
logger.log(Level.INFO, "Processing patient record deletion");
break;

case "help":
Expand Down
25 changes: 14 additions & 11 deletions src/main/java/bookbob/functions/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,30 +171,33 @@ public void list(Records records) {
}
}

// @@Author G13nd0n
// @@author G13nd0n
public void delete(String nric, Records records) throws IOException {
assert nric != null:
"Please provide a valid NRIC";

Comment on lines +176 to +178

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good use of assertion to assert nric != null when user wants to delete a specific patient record

List<Patient> patients = records.getPatients();
int initialPatientSize = patients.size();
if (initialPatientSize == 0) {
System.out.println("There are no patients in the record currently.");
double initialSize = patients.size();
if (patients.isEmpty()) {
System.out.println("No patients found.");
return;
}
for (int i = 0; i < patients.size(); i++) {
Patient currentPatient = patients.get(i);
String patientNRIC = currentPatient.getNric();
if (patientNRIC.equals(nric)) {
Patient patient = patients.get(i);
if (patient.getNric().equals(nric)) {
patients.remove(i);
System.out.println("Patient " + currentPatient.getName() + ", " + patientNRIC + ", has been deleted.");
System.out.println("Patient " + patient.getName() + ", " + nric + ", has been deleted.");
break;
}
}
if (patients.size() == initialPatientSize) {
System.out.println("Patient " + nric + " not found");
}

if (patients.size() == initialSize) {
System.out.println("Patient with " + nric + " not found");
}
FileHandler.autosave(records);
}


// Takes in an input string and determines whether to exit the program
public void exit(String input) {
if(input.equalsIgnoreCase("exit")) {
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/bookbob/BookBobTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,46 @@ void testFind() throws IOException{
assertEquals(expectedOutput,
outputStreamCaptor.toString().trim().replace(System.lineSeparator(), "\n"));
}

//@@author G13nd0n
@Test
void testFindName_multipleOutputs() throws IOException {
command.add("add n/John Doe ic/S1234567A p/98765432 d/COVID-19 m/Paracetamol ha/RC4 dob/13-04-2000",
records);
command.add("add n/Will Smith ic/S7654321B p/91234567 d/AIDS m/Paracetamol ha/CAPT dob/18-06-2003",
records);
command.add("add n/John Smith ic/S2468024A p/87654321 d/Diabetes m/Insulin ha/CAPT dob/13-04-2002",
records);
String expectedOutput = "Patient John Doe with NRIC S1234567A added.\nPatient Will Smith with NRIC S7654321B " +
"added.\n" + "Patient John Smith with NRIC S2468024A added.\n"+
"Matching patients:\nName: Will Smith, NRIC: S7654321B, " +
"Phone: 91234567, Diagnosis: AIDS, Medication: [Paracetamol], Address: CAPT, DOB: 18-06-2003\n" +
"Name: John Smith, NRIC: S2468024A, Phone: 87654321, Diagnosis: Diabetes, Medication: [Insulin], " +
"Address: CAPT, DOB: 13-04-2002";
command.find("n/Smith", records);
assertEquals(expectedOutput,
outputStreamCaptor.toString().trim().replace(System.lineSeparator(), "\n"));
}

//@@author G13nd0n
@Test
void testFindAddress_multipleOutputs() throws IOException {
command.add("add n/John Doe ic/S1234567A p/98765432 d/COVID-19 m/Paracetamol ha/RC4 dob/13-04-2000",
records);
command.add("add n/Will Smith ic/S7654321B p/91234567 d/AIDS m/Paracetamol ha/CAPT dob/18-06-2003",
records);
command.add("add n/John Smith ic/S2468024A p/87654321 d/Diabetes m/Insulin ha/CAPT dob/13-04-2002",
records);
String expectedOutput = "Patient John Doe with NRIC S1234567A added.\nPatient Will Smith with NRIC S7654321B " +
"added.\n" + "Patient John Smith with NRIC S2468024A added.\n"+
"Matching patients:\nName: Will Smith, NRIC: S7654321B, " +
"Phone: 91234567, Diagnosis: AIDS, Medication: [Paracetamol], Address: CAPT, DOB: 18-06-2003\n" +
"Name: John Smith, NRIC: S2468024A, Phone: 87654321, Diagnosis: Diabetes, Medication: [Insulin], " +
"Address: CAPT, DOB: 13-04-2002";
command.find("ha/CAPT", records);
assertEquals(expectedOutput,
outputStreamCaptor.toString().trim().replace(System.lineSeparator(), "\n"));
}

//@@author yentheng0110
@Test
Expand Down
Loading