Search code examples
javabluej

Please pay attention to the FILENAME and how to print out the filename that I choose from my computer?


it is a simple question, how to print out the selected file name, thanks.

public class CSVMAX {
public CSVRecord hottestInManyDays() {
//select many csv files from my computer
   DirectoryResource dr = new DirectoryResource();
   CSVRecord largestSoFar = null;
//read every row and implement  the method we just define
   for(File f : dr.selectedFiles()) {
    FileResource fr = new FileResource(f);
    CSVRecord currentRow = hottestHourInFile(fr.getCSVParser());
    if (largestSoFar == null) {
       largestSoFar = currentRow;
    }
    else {
    double currentTemp = Double.parseDouble(currentRow.get("TemperatureF"));
        double largestTemp = Double.parseDouble(largestSoFar.get("TemperatureF"));
        //Check if currentRow’s temperature > largestSoFar’s
    if (currentTemp > largestTemp) {
                //If so update largestSoFar to currentRow
            largestSoFar = currentRow;
    }
   }
}
return largestSoFar;        
}

here I want to print out the file name but I dont know how to do that.

public void testHottestInManyDay () {
    CSVRecord largest = hottestInManyDays();
    System.out.println("hottest temperature on that day was in file " + ***FILENAME*** + largest.get("TemperatureF") +
               " at " + largest.get("TimeEST"));
}
}

Solution

  • Ultimately, it seems that hottestInManyDays() will need to return this information.

    Does CSVRecord have a property for that?

    Something like this:

    CSVRecord currentRow = hottestHourInFile(fr.getCSVParser());
    currentRow.setFileName(f.getName());
    

    If not, can such a property be added to it?

    Maybe CSVRecord doesn't have that property. But it can be added?:

    private String _fileName;
    
    public void setFileName(String fileName) {
        this._fileName = fileName;
    }
    
    public String getFileName() {
        return this._fileName;
    }
    

    If not, can you create a wrapper class for both pieces of information?

    If you can't modify CSVRecord and it doesn't have a place for the information you want, wrap it in a class which does. Something as simple as this:

    class CSVWrapper {
    
        private CSVRecord _csvRecord;
        private String _fileName;
    
        // getters and setters for the above
        // maybe also a constructor?  make them final?  your call
    
    }
    

    Then return that from hottestInManyDays() instead of a CSVRecord. Something like this:

    CSVWrapper csvWrapper = new csvWrapper();
    csvWrapper.setCSVRecord(currentRow);
    csvWrapper.setFileName(f.getName());
    

    Changing the method signature and return value as needed, of course.


    However you do it, once it's on the return value from hottestInManyDays() you can use it in the method which consumes that:

    CSVWrapper largest = hottestInManyDays();
    System.out.println("hottest temperature on that day was in file " + largest.getFileName() + largest.getCSVRecord().get("TemperatureF") +
               " at " + largest.getCSVRecord().get("TimeEST"));
    

    (Note: If the bits at the very end there don't sit right as a Law Of Demeter violation, then feel free to extend the wrapper to include pass-thru operations as needed. Maybe even have it share a common interface with CSVRecord so it can be used as a drop-in replacement for one as needed elsewhere in the system.)