Skip to content

Commit

Permalink
Merge pull request #11 from yentheng0110/master
Browse files Browse the repository at this point in the history
Implement the add and list commands
  • Loading branch information
coraleaf0602 authored Oct 1, 2024
2 parents dac92b3 + 941e92e commit ff68a56
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 42 deletions.
58 changes: 46 additions & 12 deletions src/main/java/BookBob/Main.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,55 @@
package BookBob;

import BookBob.entity.Records;
import BookBob.functions.CommandHandler;
import BookBob.functions.SaveAndRetrieve;
import java.util.Scanner;

public class Main {
/**
* Main entry-point for the java.duke.Duke application.
*/

public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
System.out.println("What is your name?");
System.out.println("Welcome to BookBob, Dr. Bob!");

Scanner in = new Scanner(System.in);
System.out.println("Hello " + in.nextLine());
Records records = new Records();
SaveAndRetrieve.initFile(records);
CommandHandler commandHandler = new CommandHandler();

while (true) {
String input = in.nextLine();
String[] inputArr = input.split(" ", 2);
String command = inputArr[0];

switch (command) {
case "exit":
commandHandler.exit(input);
break;

case "add":
commandHandler.add(input, records);
break;

case "list":
commandHandler.list(records);
break;

case "delete":
if (inputArr.length > 1) {
String NRIC = inputArr[1].trim();
commandHandler.delete(NRIC, records);
} else {
System.out.println("Please specify an NRIC to delete.");
}
break;

case "help":
commandHandler.help();
break;

default:
System.out.println("Unknown command. Type 'help' for a list of commands.");
break;
}
}
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/BookBob/entity/Patient.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class Patient {
public Patient(String name, String NRIC) {
this.name = name;
this.NRIC = NRIC;
this.phoneNumber = 0;
this.homeAddress = "";
this.medication = new ArrayList<>();
this.diagnosis = "";
Expand All @@ -26,6 +27,7 @@ public Patient(String name, String NRIC) {
public Patient(String name, String NRIC, String phoneNumber, String dateOfBirth, String homeAddress, String diagnosis, ArrayList<String> medications) {
this.name = name;
this.NRIC = NRIC;
this.phoneNumber = Integer.parseInt(phoneNumber);
this.dateOfBirth = dateOfBirth;
this.homeAddress = homeAddress;
this.diagnosis = diagnosis;
Expand Down
29 changes: 0 additions & 29 deletions src/main/java/BookBob/functions/CRUD.java

This file was deleted.

81 changes: 80 additions & 1 deletion src/main/java/BookBob/functions/CommandHandler.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package BookBob.functions;

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

import java.util.ArrayList;

public class CommandHandler {

// Prints output for help command
Expand Down Expand Up @@ -34,11 +39,85 @@ public void help() {
"+-----------+---------------------------------------+---------------------------------+\n");
}

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 = "";

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);
}
}

Patient patient = new Patient(name, NRIC);
patient.setPhoneNumber(phoneNumber);
patient.setDiagnosis(diagnosis);
patient.setHomeAddress(homeAddress);
patient.setDateOfBirth(dateOfBirth);
patient.setMedication(medications);

records.addPatient(patient);
System.out.println("Patient " + name + " with NRIC " + NRIC + " added.");
}

public void list(Records records) {
ArrayList<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() +
", 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;
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)) {
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");
}
}

// Takes in an input string and determines whether to exit the program
public void exit(String input) {
if(input.equalsIgnoreCase("exit")) {
System.exit(0);
}
}

}

0 comments on commit ff68a56

Please sign in to comment.