Skip to content

Commit

Permalink
Merge pull request #91 from PrinceCatt/master
Browse files Browse the repository at this point in the history
Add exception, logging and JUnit test for File Handler
  • Loading branch information
coraleaf0602 authored Oct 13, 2024
2 parents 19f45a4 + 8e1d461 commit b9bfe0c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
6 changes: 0 additions & 6 deletions data/bookbob_data.txt
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
Name: James Allen | NRIC: S8976543A | Phone Number: | Date_Of_Birth: | Home Address: | Diagnosis: | Medication: ;
Name: James-Ho | NRIC: S9534567A | Phone Number: 13121995 | Date_Of_Birth: 91234567 | Home Address: NUS-PGPR | Diagnosis: Asthma | Medication: Albuterol;
Name: y-t | NRIC: T0367489A | Phone Number: | Date_Of_Birth: | Home Address: | Diagnosis: | Medication: ;
Name: glenda tan | NRIC: T0987653A | Phone Number: | Date_Of_Birth: | Home Address: | Diagnosis: | Medication:
Name: James Ho | NRIC: S9534567A | Phone Number: 91234567 | Date_Of_Birth: 13121995 | Home Address: NUS PGPR | Diagnosis: Asthma | Medication: Albuterol;
Name: John Doe | NRIC: S1234567Z | Phone Number: 97654321 | Date_Of_Birth: 13121995 | Home Address: Hougang Green | Diagnosis: Fever | Medication: Panadol;
17 changes: 14 additions & 3 deletions src/main/java/bookbob/functions/FileHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,39 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

public class FileHandler {

private static final Logger logger = Logger.getLogger(FileHandler.class.getName());
private static String filePath = "data" + File.separator + "bookbob_data.txt";

public static void initFile(Records records){
try {

String directoryName = "data";
String currentDirectory = System.getProperty("user.dir");
String directory = currentDirectory + File.separator + directoryName;
File directoryFile = new File(directory);

logger.log(Level.INFO, "Processing directory creation");
File directoryFile = new File(directory);
if(directoryFile.mkdirs()) { //directory was not created
logger.log(Level.INFO, "Directory created, at: " + directory);
File file = new File(filePath);
logger.log(Level.INFO, "File created, at: " + file.getAbsolutePath());
file.createNewFile(); //create new data file
} else { //directory already created
logger.log(Level.INFO, "Directory exsited, creating new file");
File file = new File(filePath);
if(file.createNewFile()) { //file was not created
logger.log(Level.INFO, "Directory exsited, creating new file");
} else {
logger.log(Level.INFO, "Retrieving data from bookbob_data.txt");
retrieveData(records);
}
}
} catch(Exception e){
System.out.println("An error occured");
logger.log(Level.WARNING, "Error initializing file", e);
e.printStackTrace();
}
}
Expand All @@ -60,6 +68,7 @@ public static void autosave(Records records) throws IOException {
fw.write(toWrite + "\n");
}
fw.close();
logger.log(Level.INFO, "Autosavd successfully");
}

public static void retrieveData(Records records){
Expand All @@ -84,7 +93,9 @@ public static void retrieveData(Records records){
dateOfBirth, homeAddress, diagnosis, medications);
records.addPatient(patient);
}
logger.log(Level.INFO, "Retrieved successfully");
} catch (FileNotFoundException e) {
logger.log(Level.WARNING, "File not found", e);
throw new RuntimeException(e);
}
}
Expand Down
41 changes: 41 additions & 0 deletions src/test/java/bookbob/BookBobTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package bookbob;

import bookbob.entity.Patient;
import bookbob.entity.Records;
import bookbob.functions.FileHandler;
import org.junit.jupiter.api.Test;

import bookbob.functions.CommandHandler;
Expand All @@ -10,12 +12,16 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

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


public class BookBobTest {
CommandHandler command = new CommandHandler();
FileHandler fileHandler;
Records records = new Records();
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
private final PrintStream standardOut = System.out;
Expand Down Expand Up @@ -351,4 +357,39 @@ void testAdd_addPatientWithoutNRIC_patientNotAdded() throws IOException {
assertEquals(expectedOutput,
outputStreamCaptor.toString().trim().replace(System.lineSeparator(), "\n"));
}

//@@author PrinceCatt
@Test
void testTextConverterFullInformation() {
List<String> medications = new ArrayList<>();
medications.add("Gaviscon");
Patient patient = new Patient("John", "S9765432T", "06071997", "87658976",
"Bukit Gombak", "Gastric", medications);
String output = fileHandler.convertPatientToOutputText(patient);
assertEquals(output, "Name: John | NRIC: S9765432T | Phone Number: 87658976 | " +
"Date_Of_Birth: 06071997 | Home Address: Bukit Gombak | Diagnosis: Gastric | Medication: Gaviscon;");
}

//@@author PrinceCatt
@Test
void testTextConverterPartialInformation() {
List<String> medications = new ArrayList<>();
Patient patient = new Patient("John", "S9765432T");
String output = fileHandler.convertPatientToOutputText(patient);
assertEquals(output, "Name: John | NRIC: S9765432T | Phone Number: | " +
"Date_Of_Birth: | Home Address: | Diagnosis: | Medication: ");
}

@Test
void testFileInitialization() throws IOException {
Records records = new Records(); //initialize a new record to clear file content
fileHandler.autosave(records);
command.add("add n/Jack Wong ic/S9765432T p/87658976 d/Gastric m/Gaviscon ha/Bukit Gombak dob/06071997",
records);
Patient patient = records.getPatients().get(0);
fileHandler.autosave(records);
fileHandler.initFile(records);
Patient newPatient = records.getPatients().get(0);
assertEquals(patient,newPatient);
}
}

0 comments on commit b9bfe0c

Please sign in to comment.