Skip to content

Commit

Permalink
Merge pull request #36 from coraleaf0602/master
Browse files Browse the repository at this point in the history
Restore deleted file and modify code according to checkstyle
  • Loading branch information
kaboomzxc authored Oct 8, 2024
2 parents 28e5357 + 77e873b commit 11e60ed
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package BookBob;
import BookBob.entity.Records;
import BookBob.functions.CommandHandler;
import BookBob.functions.SaveAndRetrieve;
package bookbob;
import bookbob.entity.Records;
import bookbob.functions.CommandHandler;
import bookbob.functions.SaveAndRetrieve;
import java.util.Scanner;

public class Main {
Expand Down Expand Up @@ -33,8 +33,8 @@ public static void main(String[] args) {

case "delete":
if (inputArr.length > 1) {
String NRIC = inputArr[1].trim();
commandHandler.delete(NRIC, records);
String nric = inputArr[1].trim();
commandHandler.delete(nric, records);
} else {
System.out.println("Please specify an NRIC to delete.");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package BookBob.entity;
package bookbob.entity;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package BookBob.entity;
package bookbob.entity;

import java.util.ArrayList;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package BookBob.functions;
package bookbob.functions;

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

import java.util.ArrayList;
import java.util.List;
Expand All @@ -10,39 +10,40 @@ public class CommandHandler {

// Prints output for help command
public void help() {
System.out.println("+-----------+---------------------------------------+---------------------------------+\n" +
"| Action | Format | Example |\n" +
"+-----------+---------------------------------------+---------------------------------+\n" +
"| Help | help | help |\n" +
"+-----------+---------------------------------------+---------------------------------+\n" +
"| Add | add n/NAME ic/NRIC [p/PHONE_NUMBER] | add n/James Ho ic/S9534567A |\n" +
"| | [d/DIAGNOSIS] [m/MEDICATION] | p/91234567 d/Asthma m/Albuterol |\n" +
"| | [ha/HOME_ADDRESS] [dob/DATE_OF_BIRTH] | ha/NUS-PGPR dob/13121995 |\n" +
"+-----------+---------------------------------------+---------------------------------+\n" +
"| List | list | list |\n" +
"+-----------+---------------------------------------+---------------------------------+\n" +
"| Find | find NAME [KEYWORDS] OR | find NRIC S1234 |\n" +
"| | find NRIC [KEYWORDS] OR | |\n" +
"| | find PHONE_NUMBER [KEYWORDS] OR | |\n" +
"| | find DIAGNOSIS [KEYWORDS] OR | |\n" +
"| | find MEDICATION [KEYWORDS] OR | |\n" +
"| | find HOME_ADDRESS [KEYWORDS] OR | |\n" +
"| | find DATE_OF_BIRTH [KEYWORDS] | |\n" +
"+-----------+---------------------------------------+---------------------------------+\n" +
"| Delete | delete NRIC | delete S9534567A |\n" +
"+-----------+---------------------------------------+---------------------------------+\n" +
"| Save | save(automatic) | save |\n" +
"+-----------+---------------------------------------+---------------------------------+\n" +
"| Retrieve/ | retrieve or import | retrieve |\n" +
"| Import | (automatic) | |\n" +
"+-----------+---------------------------------------+---------------------------------+\n" +
"| Exit | exit | exit |\n" +
"+-----------+---------------------------------------+---------------------------------+\n");
System.out.println("""
+-----------+---------------------------------------+---------------------------------+
| Action | Format | Example |
+-----------+---------------------------------------+---------------------------------+
| Help | help | help |
+-----------+---------------------------------------+---------------------------------+
| Add | add n/NAME ic/NRIC [p/PHONE_NUMBER] | add n/James Ho ic/S9534567A |
| | [d/DIAGNOSIS] [m/MEDICATION] | p/91234567 d/Asthma m/Albuterol |
| | [ha/HOME_ADDRESS] [dob/DATE_OF_BIRTH] | ha/NUS-PGPR dob/13121995 |
+-----------+---------------------------------------+---------------------------------+
| List | list | list |
+-----------+---------------------------------------+---------------------------------+
| Find | find NAME [KEYWORDS] OR | find NRIC S1234 |
| | find NRIC [KEYWORDS] OR | |
| | find PHONE_NUMBER [KEYWORDS] OR | |
| | find DIAGNOSIS [KEYWORDS] OR | |
| | find MEDICATION [KEYWORDS] OR | |
| | find HOME_ADDRESS [KEYWORDS] OR | |
| | find DATE_OF_BIRTH [KEYWORDS] | |
+-----------+---------------------------------------+---------------------------------+
| Delete | delete NRIC | delete S9534567A |
+-----------+---------------------------------------+---------------------------------+
| Save | save(automatic) | save |
+-----------+---------------------------------------+---------------------------------+
| Retrieve/ | retrieve or import | retrieve |
| Import | (automatic) | |
+-----------+---------------------------------------+---------------------------------+
| Exit | exit | exit |
+-----------+---------------------------------------+---------------------------------+""");
}

public void add(String input, Records records) {
String name = "";
String NRIC = "";
String nric = "";
String dateOfBirth = "";
String phoneNumber = "";
String homeAddress = "";
Expand All @@ -51,9 +52,9 @@ public void add(String input, Records records) {

// Extract name
int nameStart = input.indexOf("n/");
int NRICStart = input.indexOf("ic/");
if (nameStart != -1 && NRICStart != -1) {
name = input.substring(nameStart + 2, NRICStart).trim();
int nricStart = input.indexOf("ic/");
if (nameStart != -1 && nricStart != -1) {
name = input.substring(nameStart + 2, nricStart).trim();
} else if (nameStart != -1) { // Handle case where only name is provided
name = input.substring(nameStart + 2).trim();
System.out.println("Please provide the NRIC for the patient named " + name +
Expand All @@ -63,10 +64,10 @@ public void add(String input, Records records) {

// Extract NRIC
int phoneStart = input.indexOf("p/");
if (NRICStart != -1 && phoneStart != -1) {
NRIC = input.substring(NRICStart + 3, phoneStart).trim();
} else if (NRICStart != -1) { // Handle case where there is no phone number
NRIC = input.substring(NRICStart + 3).trim();
if (nricStart != -1 && phoneStart != -1) {
nric = input.substring(nricStart + 3, phoneStart).trim();
} else if (nricStart != -1) { // Handle case where there is no phone number
nric = input.substring(nricStart + 3).trim();
}

// Extract phone number
Expand Down Expand Up @@ -115,15 +116,15 @@ public void add(String input, Records records) {
dateOfBirth = input.substring(dobStart + 4).trim();
}

Patient patient = new Patient(name, NRIC);
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.");
System.out.println("Patient " + name + " with NRIC " + nric + " added.");
}

// Utility method to find the start of the next field or the end of the input string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package BookBob.functions;
package bookbob.functions;

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

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -17,12 +17,10 @@ public static void initFile(Records records){
try {
File file = new File("bookbob_data.txt");
if(file.createNewFile()) {
}
else {
} else {
retrieveData(records);
}
}
catch(Exception e){
} catch(Exception e){
System.out.println("An error occured");
e.printStackTrace();
}
Expand Down
83 changes: 83 additions & 0 deletions src/test/java/seedu/bookbob/BookBobTest.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,95 @@
package seedu.bookbob;

import bookbob.entity.Records;
import org.junit.jupiter.api.Test;

import bookbob.functions.CommandHandler;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import static org.junit.jupiter.api.Assertions.assertTrue;

public class BookBobTest {
CommandHandler command = new CommandHandler();
Records records = new Records();

@Test
public void sampleTest() {
assertTrue(true);
}

private final PrintStream standardOut = System.out;
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();

@BeforeEach
public void setUp() {
System.setOut(new PrintStream(outputStreamCaptor));
}

@Test
void testHelp() {
command.help();
assertEquals("+-----------+---------------------------------------+---------------------------------+\n" +
"| Action | Format | Example |\n" +
"+-----------+---------------------------------------+---------------------------------+\n" +
"| Help | help | help |\n" +
"+-----------+---------------------------------------+---------------------------------+\n" +
"| Add | add n/NAME ic/NRIC [p/PHONE_NUMBER] | add n/James Ho ic/S9534567A |\n" +
"| | [d/DIAGNOSIS] [m/MEDICATION] | p/91234567 d/Asthma m/Albuterol |\n" +
"| | [ha/HOME_ADDRESS] [dob/DATE_OF_BIRTH] | ha/NUS-PGPR dob/13121995 |\n" +
"+-----------+---------------------------------------+---------------------------------+\n" +
"| List | list | list |\n" +
"+-----------+---------------------------------------+---------------------------------+\n" +
"| Find | find NAME [KEYWORDS] OR | find NRIC S1234 |\n" +
"| | find NRIC [KEYWORDS] OR | |\n" +
"| | find PHONE_NUMBER [KEYWORDS] OR | |\n" +
"| | find DIAGNOSIS [KEYWORDS] OR | |\n" +
"| | find MEDICATION [KEYWORDS] OR | |\n" +
"| | find HOME_ADDRESS [KEYWORDS] OR | |\n" +
"| | find DATE_OF_BIRTH [KEYWORDS] | |\n" +
"+-----------+---------------------------------------+---------------------------------+\n" +
"| Delete | delete NRIC | delete S9534567A |\n" +
"+-----------+---------------------------------------+---------------------------------+\n" +
"| Save | save(automatic) | save |\n" +
"+-----------+---------------------------------------+---------------------------------+\n" +
"| Retrieve/ | retrieve or import | retrieve |\n" +
"| Import | (automatic) | |\n" +
"+-----------+---------------------------------------+---------------------------------+\n" +
"| Exit | exit | exit |\n" +
"+-----------+---------------------------------------+---------------------------------+\n".trim(), outputStreamCaptor.toString().trim());
}

@AfterEach
public void tearDown() {
System.setOut(standardOut);
}

@Test
void testAdd() {
command.add("add n/James-Ho ic/S9534567A p/91234567 d/Asthma m/Albuterol ha/NUS-PGPR dob/13121995", records);
assertEquals("Patient James-Ho with NRIC S9534567A added.".trim(), outputStreamCaptor.toString().trim());
}

@Test
void testDelete() {
command.add("add n/James-Ho ic/S9534567A p/91234567 d/Asthma m/Albuterol ha/NUS-PGPR dob/13121995", records);
command.delete("S9534567A", records);
assertEquals("Patient James-Ho with NRIC S9534567A added.\n" +
"Patient James-Ho, S9534567A, has been deleted.", outputStreamCaptor.toString().trim());
}

@Test
void testList() {
command.add("add n/James-Ho ic/S9534567A p/91234567 d/Asthma m/Albuterol ha/NUS-PGPR dob/13121995", records);
command.list(records);
assertEquals("Patient James-Ho with NRIC S9534567A added.\n" +
"Name: James-Ho, NRIC: S9534567A, Phone: 91234567, Diagnosis: Asthma, Medication: [Albuterol], " +
"Address: NUS-PGPR, DOB: 13121995", outputStreamCaptor.toString().trim());
}

}

0 comments on commit 11e60ed

Please sign in to comment.