Package data.handlers

Class CSVHandler

java.lang.Object
data.handlers.CSVHandler

public final class CSVHandler extends Object
  • Constructor Details

    • CSVHandler

      public CSVHandler(String csvFile_OR_FolderPath)
      Creates a new DataHandler object, given the database path.
      Parameters:
      csvFile_OR_FolderPath - The path to the directory where CSV files are stored.
  • Method Details

    • setCsvFile_OR_FolderPath

      public void setCsvFile_OR_FolderPath(String csvFile_OR_FolderPath)
      Sets the path to the CSV file or folder.
      Parameters:
      csvFile_OR_FolderPath - The path to the directory where CSV files are stored.
    • findAttributeIndex

      public int findAttributeIndex(String attributeName)
      This method searches for a specific attribute (header) in a CSV file.
      Parameters:
      attributeName - The attribute to search for, for example: "First Name".
      Returns:
      The index of the attribute if found, or -1 if not found. Example:
      
       DataHandler dataHandler = new DataHandler("path/to/csv");
       int index = dataHandler.findAttributeIndex("First Name");
       
    • findDataIndex

      public int findDataIndex(String attributeName, String dataValue)
      This method searches for a specific data's index in a CSV file.
      Parameters:
      attributeName - The name of the data to search for, for example: "First Name".
      dataValue - The value of the data to search for, for example: "John".
      Returns:
      The index of the data if found, or -1 if not found. Example:
      
       DataHandler dataHandler = new DataHandler("path/to/csv");
       int index = dataHandler.findDataIndex("First Name", "John");
       
    • retrieveSingleData

      public String retrieveSingleData(String identifierAttributeName, String identifierValue, String dataName)
      Retrieve a single data value based on the given identifierAttributeName and data name.
      Parameters:
      identifierAttributeName - The name of the identifierAttribute attribute (column header).
      identifierValue - The value of the identifierAttribute attribute to match.
      dataName - The name of the data attribute to retrieve.
      Returns:
      the retrieved data value
    • retrieveRowData

      public String[] retrieveRowData(String identifierAttributeName, String identifierValue)
      Retrieves row data from the CSV file based on the provided data identifierAttributeName and data name.
      Parameters:
      identifierAttributeName - The name of the identifierAttribute to search for, for example: "Employee ID".
      identifierValue - The value of the identifierAttribute to search for, for example: "123".
      Returns:
      The retrieved data value, or null if not found.
    • retrieveColumnData_AsInt

      public Integer[] retrieveColumnData_AsInt(String identifierAttributeName)
      Retrieve column data based on the header name of the identifier.
      Parameters:
      identifierAttributeName - the name of the header to identify the column data
      Returns:
      an array of String containing the column data
    • retrieveMultipleData

      public List<String[]> retrieveMultipleData(String identifierAttributeName, String identifierValue)
      Retrieves multiple data rows from the CSV file based on the specified identifierAttribute.
      Parameters:
      identifierAttributeName - The name of the identifierAttribute attribute (column header).
      identifierValue - The value of the identifierAttribute attribute to match.
      Returns:
      A list of String arrays, each containing the data of the specified identifierAttribute. Returns null if the identifierAttribute is not found in the CSV file.
    • retrieveAllData

      public List<String[]> retrieveAllData()
      Retrieves all data from a CSV file.
      Returns:
      a list of string arrays containing the data
    • updateData

      public void updateData(String identifierAttributeName, String identifierValue, String dataName, String newData)
      Update the specified data in the CSV file based on the provided data identifierAttribute name and value, and new data.
      Parameters:
      identifierAttributeName - The name of the identifierAttribute attribute (column header).
      identifierValue - The value of the identifierAttribute attribute to match.
      dataName - The name of the attribute to be updated.
      newData - The new data value to be written.
    • createData

      public void createData(String[] dataToAdd)
      A method to create data in a CSV file.
      Parameters:
      dataToAdd - the data to be added to the CSV file
    • updateRowData

      public void updateRowData(String identifierAttributeName, String identifierValue, String[] newValues)
      Updates a row of data in the CSV file.
      Parameters:
      identifierAttributeName - The name of the identifier attribute to search for.
      identifierValue - The value of the identifier attribute.
      newValues - The new values to replace the old ones in the row.
    • deleteRowData

      public void deleteRowData(String identifierAttributeName, String identifierValue)
      Deletes a row of data from the CSV file.
      Parameters:
      identifierAttributeName - The name of the identifier attribute to search for.
      identifierValue - The value of the identifier attribute.
    • createCSVFile

      public void createCSVFile(List<String[]> rowLists, String[] headers, String filePath)
      Creates a new CSV file with provided rows and headers.
      Parameters:
      rowLists - The list of rows to be added to the CSV file.
      headers - The headers for the CSV file.
      filePath - The path where the new CSV file will be created.
    • insertDataFromCSV

      public void insertDataFromCSV(String employeeCSVPath)
      Inserts data from a CSV file into the current CSV file.
      Parameters:
      employeeCSVPath - The path of the CSV file containing data to be inserted.
    • getCsvSize

      public int getCsvSize()
      Retrieves the number of rows in the CSV file.
      Returns:
      The number of rows in the CSV file.