Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This code takes a csv file and adds data from it from various points in it. How would I implement a tester class that would:

This code takes a csv file and adds data from it from various points in it. How would I implement a tester class that would:

  • An ability to query the system getting identifiers, a patient, a prediction for a patient, etc.
  • An ability to add, remove and maintain patients in the system

Would that be done in a tester class or would I need another backend class?

package project1;

import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.logging.Level; import java.util.logging.Logger;

public class Patient implements PatientCollectionADT{ String[] data; // Variable to store patient data Patient (){ // Patient constructor } public Patient getPatient(String id) throws IOException { Patient x; String row; // Variable of the csv file row BufferedReader csvReader = new BufferedReader(new FileReader("ourFile.csv")); // here we open our csv file and we replace "ourFile.csv" with our path while ((row = csvReader.readLine()) != null) { // To confirm that the file is not ended yet String[] data = row.split(","); // to split the row we red when we find a "," if(data[2] == id) // the third data is equal to the given id we construct a new patient with the information of this row and we return this patient { x = new Patient (); x.data = data; return x; } } csvReader.close(); // To close the opened file return null; } public Patient removePatient(String id) throws IOException{ ArrayList> rows = new ArrayList>(); // This variable is when we store our new data

FileWriter csvWriter = new FileWriter("data.csv");

String row; // Variable of the csv file row BufferedReader csvReader = new BufferedReader(new FileReader("ourFile.csv")); // here we open our csv file and we replace "ourFile.csv" with our path while ((row = csvReader.readLine()) != null) { // To confirm that the file is not ended yet String[] data = row.split(","); // to split the row we red when we find a "," ArrayList newData = new ArrayList(); // variable to store the neww data in it if(data[2] != id) // When the third element is not equal to the given id we do not copy the row { for(String past:data) { newData.add(past); } rows.add(newData); // We add the copied row to the others } else { Patient w = new Patient (); // Here we construct a patient and we return the removed patient w.data = data; return w; } } csvReader.close(); // To close the opened file for (ArrayList rowData : rows) { // to copy data from the list to the csv file csvWriter.append(String.join(",", rowData)); csvWriter.append(" "); }

csvWriter.flush(); csvWriter.close();// to close the file written return null; } public void setResultForPatient (String id, String result) throws IOException { ArrayList> rows = new ArrayList>(); // This variable is when we store our new data

FileWriter csvWriter = new FileWriter("ourFile.csv");

String row; // Variable of the csv file row BufferedReader csvReader = new BufferedReader(new FileReader("ourFile.csv")); // here we open our csv file and we replace "ourFile.csv" with our path while ((row = csvReader.readLine()) != null) { // To confirm that the file is not ended yet String[] data = row.split(","); // to split the row we red when we find a "," ArrayList newData = new ArrayList(); // variable to store the neww data in it if(data[2] != id) // When the third element is not equal to the given id we do not copy the row { for(String past:data) { newData.add(past); } rows.add(newData); // We add the copied row to the others } else { String[] newwData = result.split(","); // split the information separated by comma for(String past:newwData) { newData.add(past); } rows.add(newData); // We add the copied row to the others } } csvReader.close(); // To close the opened file for (ArrayList rowData : rows) { // to copy data from the list to the csv file csvWriter.append(String.join(",", rowData)); csvWriter.append(" "); }

csvWriter.flush(); csvWriter.close();// to close the file written }

public ArrayList getIds () throws IOException { ArrayList result = new ArrayList(); // to store the list of ids for(int i=1;i<=21;i++) { Patient x = getPatient(String.valueOf(i)); // get the patient with the id = i result.add(x.data[2]); // add the patient id to the list } return result; }

public String addPatientsFromFile (String fileName) throws FileNotFoundException, IOException{

FileWriter csvWriter = new FileWriter("ourFile.csv"); String output = ""; // to store the printed output int rowNumber = 0; // to detect where the error is (row) String row; // Variable of the csv file row BufferedReader csvReader = new BufferedReader(new FileReader(fileName)); // here we open our csv file while ((row = csvReader.readLine()) != null) { // To confirm that the file is not ended yet rowNumber++; String[] data = row.split(","); // to split the row we red when we find a "," ArrayList newData = new ArrayList(); // variable to store the neww data in it if(data.length == 4775 ) { output = output+" "+row+" "; for(String past:data) { newData.add(past); } csvWriter.append(String.join(",", newData)); csvWriter.append(" "); } else { output = output+" error in line "+rowNumber; } } csvReader.close(); // To close the opened file csvWriter.flush(); csvWriter.close();// to close the file written return output; }

public String toString (){ String result =""; // to store the list of patients for(int i=1;i<=21;i++) { Patient x = new Patient(); try { x = getPatient(String.valueOf(i)); // get the patient with the id = i } catch (IOException ex) { Logger.getLogger(Patient.class.getName()).log(Level.SEVERE, null, ex); } result = result + x.data[3696]+" "+x.data[3257] + " "; // store the patient data } return result; }

}

package project1; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList;

public interface PatientCollectionADT {

// TODO Auto-generated method stub public Patient getPatient (String id) throws IOException;

// Return the patient with the given id. Return void if the id does

// not exist in the collection

public Patient removePatient (String id) throws IOException;

// Remove and return the Patient with the given id. Return void if the id does not exist.

public void setResultForPatient (String id, String result) throws IOException;

// Set the result field for the patient with given id.

public ArrayList getIds () throws IOException;

public String addPatientsFromFile (String fileName) throws FileNotFoundException, IOException;

// Each line must contain a unique patient identifier followed by exactly 4776 doubles.

// If the line does not meet the criteria, it is not added to the patient collection,

// and an error message is included in the return String.

// The error message will indicate which line was in error and what the error was.

// expected line format

//id,protein1,protein2, ... , protein4776

public String toString ();

// Return a String representation of the collection.

// Only include the 3697th and 3258th values in that order.

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learning MySQL Get A Handle On Your Data

Authors: Seyed M M Tahaghoghi

1st Edition

0596529465, 9780596529468

More Books

Students also viewed these Databases questions