Skip to content

Commit

Permalink
Merge pull request #366 from G13nd0n/master
Browse files Browse the repository at this point in the history
Fix bugs
  • Loading branch information
yentheng0110 authored Nov 10, 2024
2 parents 34b62b9 + 541e5e3 commit ce8c9c8
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 26 deletions.
5 changes: 3 additions & 2 deletions docs/UserGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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`
```
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/bookbob/entity/AppointmentRecord.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -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.");

}
}
Expand Down
38 changes: 35 additions & 3 deletions src/main/java/bookbob/entity/Records.java
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -31,6 +32,10 @@ public void addPatient(Patient patient) {
public void addPatient(String name, String nric, ArrayList<Visit> visits, String sex, LocalDate dateOfBirth,
String phoneNumber, String homeAddress, ArrayList<String> allergies,
ArrayList<String> medicalHistories) {
String searchInput = "ic/" + nric;
if (!checkExistingRecords(name, nric, searchInput)) {
return;
}
Patient patient = new Patient(name, nric, visits);
patient.setSex(sex);
patient.setDateOfBirth(dateOfBirth);
Expand All @@ -42,6 +47,31 @@ public void addPatient(String name, String nric, ArrayList<Visit> visits, String
System.out.println("Patient " + name + " with NRIC " + nric + " added.");
}

private boolean checkExistingRecords(String name, String nric, String searchInput) {
String originalNric = nric;
List<Patient> 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<Patient> getPatients() {
return patients;
}
Expand Down Expand Up @@ -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.");
Expand All @@ -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;
}
}
Expand Down
40 changes: 23 additions & 17 deletions src/main/java/bookbob/functions/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down Expand Up @@ -774,7 +774,8 @@ private ArrayList<String> 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());
Expand Down Expand Up @@ -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 " +
Expand Down Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/bookbob/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit ce8c9c8

Please sign in to comment.