RainfallAnalyser / src / main / java / project / rainanalyser / betarelease / RainfallData.java
RainfallData.java
Raw
package project.rainanalyser.betarelease;

import java.io.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class RainfallData {
    private static List<RainfallRecord> records;
    private static final String HEADER = "Year,Month,Total Rainfall,Minimum Daily Rainfall,Maximum Daily Rainfall";
    private File file_info;

    public RainfallData(File file) {
        this.file_info = file;
        Map<String, Double> monthlyRainfallTotal = new TreeMap<>();
        Map<String, Double> minDailyRainfall = new TreeMap<>();
        Map<String, Double> maxDailyRainfall = new TreeMap<>();


        processFile(file, monthlyRainfallTotal, minDailyRainfall, maxDailyRainfall);

    }

    private static void processFile(File file, Map<String, Double> monthlyRainfallTotal,
                                    Map<String, Double> minDailyRainfall, Map<String, Double> maxDailyRainfall) {
        records = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
            skipHeaderLine(reader);

            String line;
            while ((line = reader.readLine()) != null) {
                String[] data = line.split(",");
                if (data.length >= 7) {
                    String year = data[2].trim();
                    String month = data[3].trim();
                    String rainfall = data[5].trim();
                    if (isValidRainfallData(rainfall)) {
                        double rainfallAmount = Double.parseDouble(rainfall);
                        String monthKey = year + "-" + month;
                        updateMaps(monthKey, rainfallAmount, monthlyRainfallTotal, minDailyRainfall, maxDailyRainfall);
                        LocalDate date = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), 1);
                        RainfallRecord record = new RainfallRecord(date, rainfallAmount);
                        records.add(record);
                    }
                }
            }
        } catch (FileNotFoundException e) {
            System.err.println("File not found: " + file);
        } catch (IOException e) {
            System.err.println("Error reading file: " + file);
        } catch (NumberFormatException e) {
            System.err.println("Error parsing rainfall data in file: " + file);
        }
    }

    private static void skipHeaderLine(BufferedReader reader) throws IOException {
        reader.readLine(); // Skip the header line
    }

    private static void updateMaps(String monthKey, double rainfallAmount,
                                   Map<String, Double> monthlyRainfallTotal,
                                   Map<String, Double> minDailyRainfall,
                                   Map<String, Double> maxDailyRainfall) {
        monthlyRainfallTotal.merge(monthKey, rainfallAmount, Double::sum);
        minDailyRainfall.merge(monthKey, rainfallAmount, Double::min);
        maxDailyRainfall.merge(monthKey, rainfallAmount, Double::max);
    }

    private static boolean isValidRainfallData(String rainfall) {
        return !rainfall.isEmpty() && !rainfall.equals("NaN");
    }

    public List<RainfallRecord> getRecords() {
        return records;
    }

    public double getTotalRainfall() {
        double total = 0;
        for (RainfallRecord record : records) {
            total += record.getRainfall();
        }
        return total;
    }

    public double getAverageRainfall() {
        double total = getTotalRainfall();
        return total / records.size();
    }

    public double getMaximumRainfall() {
        double max = Double.MIN_VALUE;
        for (RainfallRecord record : records) {
            if (record.getRainfall() > max) {
                max = record.getRainfall();
            }
        }
        return max;
    }

    public double getMinimumRainfall() {
        double min = Double.MAX_VALUE;
        for (RainfallRecord record : records) {
            if (record.getRainfall() < min) {
                min = record.getRainfall();
            }
        }
        return min;
    }

    public File getFile() {
        return file_info;
    }

}