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

Implement "find" methods #20

Merged
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
Next Next commit
Implement "find" methods
kaboomzxc committed Oct 1, 2024
commit 38bf4da1ce81f433d81275038b316ffa95e5ef71
79 changes: 40 additions & 39 deletions src/main/java/BookBob/entity/Patient.java
Original file line number Diff line number Diff line change
@@ -5,36 +5,37 @@

public class Patient {
private String name;
private String NRIC;
private String diagnosis;
private ArrayList<String> medication;
private int phoneNumber;
private String nric;
private String dateOfBirth;
private String phoneNumber;
private String homeAddress;
private String diagnosis;
private List<String> medication;

//default constructor only takes in name and NRIC
public Patient(String name, String NRIC) {
// default constructor only takes in name and NRIC
public Patient(String name, String nric) {
this.name = name;
this.NRIC = NRIC;
this.phoneNumber = 0;
this.nric = nric;
this.dateOfBirth = "";
this.phoneNumber = "";
this.homeAddress = "";
this.medication = new ArrayList<>();
this.diagnosis = "";
this.dateOfBirth = "";
this.medication = new ArrayList<>();
}

//constructor used in retrieving data
public Patient(String name, String NRIC, String phoneNumber, String dateOfBirth, String homeAddress, String diagnosis, ArrayList<String> medications) {
// constructor used in retrieving data
public Patient(String name, String nric, String dateOfBirth, String phoneNumber, String homeAddress,
String diagnosis, List<String> medications) {
this.name = name;
this.NRIC = NRIC;
this.phoneNumber = Integer.parseInt(phoneNumber);
this.nric = nric;
this.dateOfBirth = dateOfBirth;
this.phoneNumber = phoneNumber;
this.homeAddress = homeAddress;
this.diagnosis = diagnosis;
this.medication = medications;
}

//getters and setters
// getters and setters
public String getName() {
return name;
}
@@ -43,51 +44,51 @@ public void setName(String name) {
this.name = name;
}

public String getNRIC() {
return NRIC;
public String getNric() {
return nric;
}

public void setNRIC(String NRIC) {
this.NRIC = NRIC;
public void setNric(String nric) {
this.nric = nric;
}

public String getDiagnosis() {
return diagnosis;
public String getDateOfBirth() {
return dateOfBirth;
}

public void setDiagnosis(String diagnosis) {
this.diagnosis = diagnosis;
public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
}

public ArrayList<String> getMedication() {
return medication;
public String getPhoneNumber() {
return phoneNumber;
}

public void setMedication(ArrayList<String> medication) {
this.medication = medication;
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}

public int getPhoneNumber() {
return phoneNumber;
public String getHomeAddress() {
return homeAddress;
}

public void setPhoneNumber(int phoneNumber) {
this.phoneNumber = phoneNumber;
public void setHomeAddress(String homeAddress) {
this.homeAddress = homeAddress;
}

public String getDateOfBirth() {
return dateOfBirth;
public String getDiagnosis() {
return diagnosis;
}

public void setDateOfBirth(String dateOfBirth) {
this.dateOfBirth = dateOfBirth;
public void setDiagnosis(String diagnosis) {
this.diagnosis = diagnosis;
}

public String getHomeAddress() {
return homeAddress;
public List<String> getMedication() {
return medication;
}

public void setHomeAddress(String homeAddress) {
this.homeAddress = homeAddress;
public void setMedication(List<String> medication) {
this.medication = medication;
}
}
19 changes: 9 additions & 10 deletions src/main/java/BookBob/entity/Records.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
package BookBob.entity;

import java.util.ArrayList;
import java.util.List;

public class Records {
private List<Patient> patients;

public ArrayList<Patient> patients;

//default constructor: empty
// default constructor: empty
public Records() {
ArrayList<Patient> patients = new ArrayList<>();
this.patients = patients;
this.patients = new ArrayList<>();
}

//add a patient to records
// add a patient to records
public void addPatient(Patient patient) {
ArrayList<Patient> patients = this.getPatients();
List<Patient> patients = this.getPatients();
patients.add(patient);
this.setPatients(patients);
}

//setter and getters
public ArrayList<Patient> getPatients() {
// setter and getters
public List<Patient> getPatients() {
return patients;
}

public void setPatients(ArrayList<Patient> patients) {
public void setPatients(List<Patient> patients) {
this.patients = patients;
}
}
25 changes: 13 additions & 12 deletions src/main/java/BookBob/functions/CommandHandler.java
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
import BookBob.entity.Records;

import java.util.ArrayList;
import java.util.List;

public class CommandHandler {

@@ -43,19 +44,19 @@ public void add(String input, Records records) {
String[] inputParts = input.split(" ");
String name = "";
String NRIC = "";
int phoneNumber = 0;
String diagnosis = "";
ArrayList<String> medications = new ArrayList<>();
String homeAddress = "";
String dateOfBirth = "";
String phoneNumber = "";
String homeAddress = "";
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 = Integer.parseInt(part.substring(2));
phoneNumber = part.substring(2);
} else if (part.startsWith("d/")) {
diagnosis = part.substring(2);
} else if (part.startsWith("m/")) {
@@ -79,37 +80,37 @@ public void add(String input, Records records) {
}

public void list(Records records) {
ArrayList<Patient> patients = records.getPatients();
List<Patient> patients = records.getPatients();
if (patients.isEmpty()) {
System.out.println("No patients found.");
return;
}
for (Patient patient : patients) {
System.out.println("Name: " + patient.getName() + ", NRIC: " + patient.getNRIC() +
System.out.println("Name: " + patient.getName() + ", NRIC: " + patient.getNric() +
", Phone: " + patient.getPhoneNumber() + ", Diagnosis: " + patient.getDiagnosis() +
", Medication: " + patient.getMedication() + ", Address: " + patient.getHomeAddress() +
", DOB: " + patient.getDateOfBirth());
}
}

public void delete(String NRIC, Records records) {
ArrayList<Patient> patients = records.patients;
public void delete(String nric, Records records) {
List<Patient> patients = records.getPatients();
int initialPatientSize = patients.size();
if (initialPatientSize == 0) {
System.out.println("There are no patients in the record currently.");
return;
}
for (int i = 0; i < patients.size(); i++) {
Patient currentPatient = patients.get(i);
String patientNRIC = currentPatient.getNRIC();
if (patientNRIC.equals(NRIC)) {
String patientNRIC = currentPatient.getNric();
if (patientNRIC.equals(nric)) {
patients.remove(i);
System.out.println("Patient " + currentPatient.getName() + ", " + patientNRIC + ", has been deleted.");
break;
}
}
if (patients.size() == initialPatientSize) {
System.out.println("Patient " + NRIC + " not found");
System.out.println("Patient " + nric + " not found");
}
}

61 changes: 61 additions & 0 deletions src/main/java/BookBob/functions/Find.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package BookBob.functions;

import BookBob.entity.Patient;
import BookBob.entity.Records;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class Find {
public List<Patient> findPatientsByAttribute(Records records, String[] keywords,
FindPatientAttributeCheck attributeCheck) {
List<Patient> findList = new ArrayList<>();

for (Patient p : records.getPatients()) {
for (String keyword : keywords) {
if (attributeCheck.test(p, keyword)) {
findList.add(p);
break; // To avoid adding the same patient multiple times
}
}
}

return findList;
}

public List<Patient> findPatientsByName(Records records, String[] keywords) {
return findPatientsByAttribute(records, keywords, (p, keyword) -> p.getName().contains(keyword));
}

public List<Patient> findPatientsByNric(Records records, String[] keywords) {
return findPatientsByAttribute(records, keywords, (p, keyword) -> p.getNric().contains(keyword));
}

public List<Patient> findPatientsByDateOfBirth(Records records, String[] keywords) {
return findPatientsByAttribute(records, keywords, (p, keyword) -> {
DateTimeFormatter dateInputFormatter = DateTimeFormatter.ofPattern("ddMMyyyy");
LocalDate searchDate = LocalDate.parse(keyword, dateInputFormatter);
LocalDate patientDateOfBirth = LocalDate.parse(p.getDateOfBirth(), dateInputFormatter);
return patientDateOfBirth.equals(searchDate);

});
}

public List<Patient> findPatientsByPhoneNumber(Records records, String[] keywords) {
return findPatientsByAttribute(records, keywords, (p, keyword) -> p.getPhoneNumber().contains(keyword));
}

public List<Patient> findPatientsByHomeAddress(Records records, String[] keywords) {
return findPatientsByAttribute(records, keywords, (p, keyword) -> p.getHomeAddress().contains(keyword));
}

public List<Patient> findPatientsByDiagnosis(Records records, String[] keywords) {
return findPatientsByAttribute(records, keywords, (p, keyword) -> p.getDiagnosis().contains(keyword));
}

public List<Patient> findPatientsByMedication(Records records, String[] keywords) {
return findPatientsByAttribute(records,keywords, (p, keyword) -> p.getMedication().contains(keyword));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package BookBob.functions;

import BookBob.entity.Patient;

@FunctionalInterface
public interface FindPatientAttributeCheck {
boolean test(Patient patient, String keyword);
}
11 changes: 6 additions & 5 deletions src/main/java/BookBob/functions/SaveAndRetrieve.java
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class SaveAndRetrieve {
@@ -29,18 +30,18 @@ public static void initFile(Records records){

private static String convertPatientToOutputText(Patient patient) {
String output = "";
output += "Name: " + patient.getName() + " | " + "NRIC: " + patient.getNRIC() + " | " + "Phone Number: " + patient.getPhoneNumber() + " | "
output += "Name: " + patient.getName() + " | " + "NRIC: " + patient.getNric() + " | " + "Phone Number: " + patient.getPhoneNumber() + " | "
+ "Date_Of_Birth: " + patient.getDateOfBirth() + " | " + "Home Address: " + patient.getHomeAddress() + " | " + "Diagnosis: " + patient.getDiagnosis() + " | "
+ "Medication: ";
ArrayList<String> medications = patient.getMedication();
List<String> medications = patient.getMedication();
for(int i = 0; i < medications.size(); i++) {
output += medications.get(i) + ";";
}
return output;
}

public static void autosave(Records records) throws IOException {
ArrayList<Patient> patients = records.getPatients();
List<Patient> patients = records.getPatients();
FileWriter fw = new FileWriter("bookbob_data.txt");
for(int i = 0; i < patients.size(); i++) {
Patient currPatient = patients.get(i);
@@ -62,7 +63,7 @@ public static void retrieveData(Records records){
String line = reader.nextLine();
String[] data = line.split("\\|");
String name = data[0].trim();
String NRIC = data[1].trim();
String nric = data[1].trim();
String phoneNumber = data[2].trim();
String dateOfBirth = data[3].trim();
String homeAddress = data[4].trim();
@@ -72,7 +73,7 @@ public static void retrieveData(Records records){
for(int i = 0; i < rawMedications.length; i++) {
medications.add(rawMedications[i].trim());
}
Patient patient = new Patient(name, NRIC, phoneNumber, dateOfBirth, homeAddress, diagnosis, medications);
Patient patient = new Patient(name, nric, phoneNumber, dateOfBirth, homeAddress, diagnosis, medications);
records.addPatient(patient);
}
} catch (FileNotFoundException e) {