From c875e56b6882eb072a2a92af2f642cec7c47343b Mon Sep 17 00:00:00 2001 From: "e0968962@u.nus.edu" Date: Thu, 3 Oct 2024 00:05:46 +0800 Subject: [PATCH] Fix bug for add command - make sure that inputs with multiple words separated by spaces are taken in as a whole, rather than just the first word Add multiple medications to the medications list (medications are separated by commas in the input) --- .../BookBob/functions/CommandHandler.java | 60 ++++++++++++++----- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/src/main/java/BookBob/functions/CommandHandler.java b/src/main/java/BookBob/functions/CommandHandler.java index 1d603769de..d91f3c829d 100644 --- a/src/main/java/BookBob/functions/CommandHandler.java +++ b/src/main/java/BookBob/functions/CommandHandler.java @@ -40,7 +40,6 @@ public void help() { } public void add(String input, Records records) { - String[] inputParts = input.split(" "); String name = ""; String NRIC = ""; int phoneNumber = 0; @@ -49,24 +48,52 @@ public void add(String input, Records records) { String homeAddress = ""; String dateOfBirth = ""; - 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 = Integer.parseInt(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); @@ -78,6 +105,7 @@ public void add(String input, Records records) { System.out.println("Patient " + name + " with NRIC " + NRIC + " added."); } + public void list(Records records) { ArrayList patients = records.getPatients(); if (patients.isEmpty()) {