diff --git a/docs/UserGuide.md b/docs/UserGuide.md index 618c5043e0..fbe0505517 100644 --- a/docs/UserGuide.md +++ b/docs/UserGuide.md @@ -428,7 +428,7 @@ If the selected appointment slot has already been taken, it will prompt the next Format: appointment n/NAME ic/NRIC date/DATE time/TIME #### Extra Information: Date format is in DD-MM-YYYY and Time format is in HH:mm\ -All past appointments will be removed from the system upon exiting. +All past appointments will be removed from the system upon exiting.\ Appointments before today's date and time will not be added. Example: `appointment n/John Doe ic/S1234567A date/18-11-2024 time/18:00` @@ -451,7 +451,8 @@ Format: appointment ic/NRIC date/DATE time/TIME \ Note: Deleting a Patient Appointment is case-insensitive. #### Extra Information -Date format is in DD-MM-YYYY and Time format is in HH:mm +Date format is in DD-MM-YYYY and Time format is in HH:mm\ +The nric is case-insensitive Example: `deleteAppointment ic/S1234567A date/18-11-2024 time/18:00` ``` diff --git a/src/main/java/bookbob/entity/AppointmentRecord.java b/src/main/java/bookbob/entity/AppointmentRecord.java index d688afc84b..0a234fd242 100644 --- a/src/main/java/bookbob/entity/AppointmentRecord.java +++ b/src/main/java/bookbob/entity/AppointmentRecord.java @@ -66,7 +66,7 @@ private static boolean checkDateTime(LocalDate availableDate, LocalTime availabl if (availableDate.isBefore(todayDate)) { System.out.println("Date given is before today, please select another date"); return false; - } else if (availableTime.isBefore(timeNow)) { + } else if (availableDate.isEqual(todayDate) && availableTime.isBefore(timeNow)) { System.out.println("Time given is before current time, please select another time"); return false; } @@ -324,14 +324,16 @@ public void removePastAppointments() { //@@author G13nd0n public void deleteAppointment(String nric, String date, String time) { String patientName = ""; + String originalNric = nric; int initialAppointmentSize = appointments.size(); for (int i = 0; i < initialAppointmentSize; i++) { Appointment appointment = appointments.get(i); patientName = appointment.getPatientName(); - String patientNric = appointment.getPatientNric(); + String patientNric = appointment.getPatientNric().toLowerCase(); String patientDate = appointment.getDate().format(formatter); String patientTime = appointment.getTime().toString(); + nric = nric.toLowerCase(); if (!patientNric.equals(nric)) { continue; } @@ -346,11 +348,11 @@ public void deleteAppointment(String nric, String date, String time) { } if (appointments.size() == initialAppointmentSize) { - System.out.println("Patient with " + nric + " do not have appointment on the given date and time."); + System.out.println("Patient with " + originalNric + " do not have appointment on the given date and time."); return; } System.out.println("Appointment on " + date + " " + time + " with Patient " + patientName + ", " + - nric + " has been deleted."); + originalNric + " has been deleted."); } } diff --git a/src/main/java/bookbob/entity/Records.java b/src/main/java/bookbob/entity/Records.java index 7f7eb8d7c5..16d6d07328 100644 --- a/src/main/java/bookbob/entity/Records.java +++ b/src/main/java/bookbob/entity/Records.java @@ -10,6 +10,7 @@ import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.util.ArrayList; +import java.util.List; import java.util.Scanner; public class Records implements FileOperation{ @@ -31,6 +32,10 @@ public void addPatient(Patient patient) { public void addPatient(String name, String nric, ArrayList visits, String sex, LocalDate dateOfBirth, String phoneNumber, String homeAddress, ArrayList allergies, ArrayList medicalHistories) { + String searchInput = "ic/" + nric; + if (!checkExistingRecords(name, nric, searchInput)) { + return; + } Patient patient = new Patient(name, nric, visits); patient.setSex(sex); patient.setDateOfBirth(dateOfBirth); @@ -42,6 +47,31 @@ public void addPatient(String name, String nric, ArrayList visits, String System.out.println("Patient " + name + " with NRIC " + nric + " added."); } + private boolean checkExistingRecords(String name, String nric, String searchInput) { + String originalNric = nric; + List existingRecords = new ArrayList<>(); + for (int i = 0; i < patients.size(); i++) { + Patient patient = patients.get(i); + String patientNRIC = patient.getNric().toLowerCase(); + nric = nric.toLowerCase(); + if (patientNRIC.equals(nric)) { + existingRecords.add(patient); + } + } + if (existingRecords.isEmpty()) { + return true; + } + Patient existingPatient = existingRecords.get(0); + String patientName = existingPatient.getName(); + if (!patientName.equals(name)) { + System.out.println("Please check if the name is correct"); + System.out.println("Appointment made previously with patient nric, " + originalNric + ", has the name " + + patientName); + return false; + } + return true; + } + public ArrayList getPatients() { return patients; } @@ -176,7 +206,7 @@ public void retrieveData(String filePath) { public void delete(String nric) { assert nric != null : "Please provide a valid NRIC"; - + String originalNric = nric; double initialSize = patients.size(); if (patients.isEmpty()) { System.out.println("No patients found."); @@ -190,9 +220,11 @@ public void delete(String nric) { } for (int i = 0; i < patients.size(); i++) { Patient patient = patients.get(i); - if (patient.getNric().equals(nric)) { + String patientNRIC = patient.getNric().toLowerCase(); + nric = nric.toLowerCase(); + if (patientNRIC.equals(nric)) { patients.remove(i); - System.out.println("Patient " + patient.getName() + ", " + nric + ", has been deleted."); + System.out.println("Patient " + patient.getName() + ", " + originalNric + ", has been deleted."); break; } } diff --git a/src/main/java/bookbob/functions/CommandHandler.java b/src/main/java/bookbob/functions/CommandHandler.java index 38c0ad10d9..58c319c695 100644 --- a/src/main/java/bookbob/functions/CommandHandler.java +++ b/src/main/java/bookbob/functions/CommandHandler.java @@ -123,14 +123,14 @@ public void add(String input, Records records) throws IOException { // Check if name is missing or invalid if (name.isEmpty()) { errorMessages.append("Please provide the patient's name\n"); + } else if (name.equals("Invalid name")) { + errorMessages.append("Please provde a valid patient's name\n"); } // Check if NRIC is missing or invalid if (nric.isEmpty()) { errorMessages.append("Please provide the patient's NRIC\n"); - } - - if (nric.equals("Invalid NRIC")) { + } else if (nric.equals("Invalid NRIC")) { errorMessages.append("Please provide a valid patient's NRIC\n"); } @@ -774,7 +774,8 @@ private ArrayList extractDiagnoses(String input) { int diagnosisStart = input.indexOf("d/"); if (diagnosisStart != -1) { int diagnosisEnd = findNextFieldStart(input, diagnosisStart + lengthOfDiagnosesIndicator); - String diagnosisInput = input.substring(diagnosisStart + lengthOfDiagnosesIndicator, diagnosisEnd).trim(); + String diagnosisInput = input.substring(diagnosisStart + lengthOfDiagnosesIndicator, diagnosisEnd) + .trim(); String[] diagnosisArray = diagnosisInput.split(",\\s*"); for (String diagnosis : diagnosisArray) { diagnoses.add(diagnosis.trim()); @@ -807,7 +808,6 @@ private String extractPhoneNumber(String input) { if (phoneStart != -1) { int phoneEnd = findNextFieldStart(input, phoneStart + lengthOfPhoneNumberIndicator); phoneNumber = input.substring(phoneStart + lengthOfPhoneNumberIndicator, phoneEnd).trim(); - // Check if the phone number is exactly 8 digits long, starts with 8 or 9, and contains only digits if (!phoneNumber.matches("[89]\\d{7}")) { System.out.println("Please provide a valid local phone number " + @@ -944,25 +944,26 @@ private String extractNric(String input) { } //@@author G13nd0n - private String extractNewNric(String updates) { + private String extractNewNric(String input) { int lenghtOfNewNricIndicator = 6; int lengthOfNric = 9; String newNRIC = ""; - int newNRICStart = updates.indexOf("newic/"); + int newNRICStart = input.indexOf("newic/"); if (newNRICStart != -1) { - int newNRICEnd = findNextFieldStart(updates, newNRICStart + lenghtOfNewNricIndicator); - newNRIC = updates.substring(newNRICStart + lenghtOfNewNricIndicator, newNRICEnd).trim(); + int newNRICEnd = findNextFieldStart(input, newNRICStart + lenghtOfNewNricIndicator); + newNRIC = input.substring(newNRICStart + lenghtOfNewNricIndicator, newNRICEnd).trim(); - if (newNRIC.length() != lengthOfNric) { - System.out.println("Please provide a valid patient's NRIC"); + if (newNRIC.isEmpty()) { + return ""; + } else if (newNRIC.length() != lengthOfNric) { + return "Invalid NRIC"; } String newNRICFirstLetter = newNRIC.substring(0, 1); String newNRICLastLetter = newNRIC.substring(8); String newNRICNumber = newNRIC.substring(1, 8); if (!newNRICFirstLetter.matches("[A-Za-z]+") || !newNRICLastLetter.matches("[A-Za-z]+") || !newNRICNumber.matches("[0-9]+")) { - System.out.println("Please provide a valid patient's NRIC"); - return ""; + return "Invalid NRIC"; } } return newNRIC; @@ -978,8 +979,11 @@ private String extractName(String input) { } int nameEnd = findNextFieldStart(input, nameStart + lengthOfNameIndicator); String name = input.substring(nameStart + lengthOfNameIndicator, nameEnd).trim(); - if (name.isEmpty() || !name.matches("[A-Za-z,\\s/-]+")) { - System.out.println("Please provide a valid patient's name"); + if (name.isEmpty()) { + return ""; + } + if (!name.matches("[A-Za-z,\\s/-]+")) { + return "Invalid name"; } return name; } @@ -994,8 +998,10 @@ private String extractNewName(String input) { } int nameEnd = findNextFieldStart(input, nameStart + lengthOfNameIndicator); name = input.substring(nameStart + lengthOfNameIndicator, nameEnd).trim(); - if (!name.matches("[A-Za-z,\\s/-]+")) { - System.out.println("Please provide a valid patient's name"); + if (name.isEmpty()) { + return ""; + } else if (!name.matches("[A-Za-z,\\s/-]+")) { + return "Invalid name"; } return name; } diff --git a/src/main/java/bookbob/parser/Parser.java b/src/main/java/bookbob/parser/Parser.java index 91061f272c..11f16d7652 100644 --- a/src/main/java/bookbob/parser/Parser.java +++ b/src/main/java/bookbob/parser/Parser.java @@ -30,6 +30,11 @@ public static void handleCommand(String input, CommandHandler commandHandler, Re switch (command) { case "help": + if (inputArray.length > 1) { + System.out.println("Unknown command. Type 'help' for a list of commands."); + logger.log(Level.INFO, "Unknown command received: {0}", command); + break; + } logAndExecute("help", commandHandler::help); break; @@ -60,6 +65,11 @@ public static void handleCommand(String input, CommandHandler commandHandler, Re break; case "list": + if (inputArray.length > 1) { + System.out.println("Unknown command. Type 'help' for a list of commands."); + logger.log(Level.INFO, "Unknown command received: {0}", command); + break; + } logAndExecute("list", () -> commandHandler.list(records)); break; @@ -108,6 +118,11 @@ public static void handleCommand(String input, CommandHandler commandHandler, Re break; case "listAppointments": + if (inputArray.length > 1) { + System.out.println("Unknown command. Type 'help' for a list of commands."); + logger.log(Level.INFO, "Unknown command received: {0}", command); + break; + } logAndExecute("listAppointments", () -> commandHandler.listAppointments(appointmentRecord)); break; @@ -159,6 +174,11 @@ public static void handleCommand(String input, CommandHandler commandHandler, Re break; case "exit": + if (inputArray.length > 1) { + System.out.println("Unknown command. Type 'help' for a list of commands."); + logger.log(Level.INFO, "Unknown command received: {0}", command); + break; + } logAndExecute("exit", () -> { try { commandHandler.removePastAppointments(appointmentRecord);