Skip to content

Commit

Permalink
Merge pull request #21 from yentheng0110/master
Browse files Browse the repository at this point in the history
Fix bug for add command
  • Loading branch information
coraleaf0602 authored Oct 3, 2024
2 parents 613d738 + 2f7e78c commit f700faa
Showing 1 changed file with 44 additions and 16 deletions.
60 changes: 44 additions & 16 deletions src/main/java/BookBob/functions/CommandHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public void help() {
}

public void add(String input, Records records) {
String[] inputParts = input.split(" ");
String name = "";
String NRIC = "";
String dateOfBirth = "";
Expand All @@ -50,24 +49,52 @@ public void add(String input, Records records) {
String diagnosis = "";
List<String> medications = new ArrayList<>();

for (String part : inputParts) {
if (part.startsWith("n/")) {
name = part.substring(2);
} else if (part.startsWith("ic/")) {
NRIC = part.substring(3);
} else if (part.startsWith("p/")) {
phoneNumber = part.substring(2);
} else if (part.startsWith("d/")) {
diagnosis = part.substring(2);
} else if (part.startsWith("m/")) {
medications.add(part.substring(2));
} else if (part.startsWith("ha/")) {
homeAddress = part.substring(3);
} else if (part.startsWith("dob/")) {
dateOfBirth = part.substring(4);
// Extract name
int nameStart = input.indexOf("n/");
int NRICStart = input.indexOf("ic/");
if (nameStart != -1 && NRICStart != -1) {
name = input.substring(nameStart + 2, NRICStart).trim();
}

// Extract NRIC
int phoneStart = input.indexOf("p/");
if (NRICStart != -1 && phoneStart != -1) {
NRIC = input.substring(NRICStart + 3, phoneStart).trim();
}

// Extract phone number
int diagnosisStart = input.indexOf("d/");
if (phoneStart != -1 && diagnosisStart != -1) {
phoneNumber = Integer.parseInt(input.substring(phoneStart + 2, diagnosisStart).trim());
}

// Extract diagnosis
int medicationStart = input.indexOf("m/");
if (diagnosisStart != -1 && medicationStart != -1) {
diagnosis = input.substring(diagnosisStart + 2, medicationStart).trim();
}

// Extract medications (split by comma)
int homeAddressStart = input.indexOf("ha/");
if (medicationStart != -1 && homeAddressStart != -1) {
String meds = input.substring(medicationStart + 2, homeAddressStart).trim();
String[] medsArray = meds.split(",\\s*");
for (String med : medsArray) {
medications.add(med.trim());
}
}

// Extract home address
int dobStart = input.indexOf("dob/");
if (homeAddressStart != -1 && dobStart != -1) {
homeAddress = input.substring(homeAddressStart + 3, dobStart).trim();
}

// Extract date of birth
if (dobStart != -1) {
dateOfBirth = input.substring(dobStart + 4).trim();
}

Patient patient = new Patient(name, NRIC);
patient.setPhoneNumber(phoneNumber);
patient.setDiagnosis(diagnosis);
Expand All @@ -79,6 +106,7 @@ public void add(String input, Records records) {
System.out.println("Patient " + name + " with NRIC " + NRIC + " added.");
}


public void list(Records records) {
List<Patient> patients = records.getPatients();
if (patients.isEmpty()) {
Expand Down

0 comments on commit f700faa

Please sign in to comment.