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

Fix bug for add command #21

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Changes from 1 commit
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
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 @@ -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;
Expand All @@ -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);
Expand All @@ -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<Patient> patients = records.getPatients();
if (patients.isEmpty()) {
Expand Down
Loading