Java-City-of-Mississauga-Sewer-Assets / Mississauga Sewer Analysis GH / SewerAnalysis / MississaugaSewers.java
MississaugaSewers.java
Raw
package SewerAnalysis;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

/**
 * Class MississaugaSewers
 */
public class MississaugaSewers {

    public ArrayList<Sewer> sewers = new ArrayList<>();

    /**
     * MississaugaSewers Constructor
     * Reads nLines from the file sewers-mississauga.csv (after skipping the header row).
     * Uses each row of data to instantiate a Sewer object, and places the new Sewer in
     * this.sewers for storage
     *
     * @param nLines, the number of sewers to read from the input file (sewers-mississauga.csv)
     */
    public MississaugaSewers(int nLines) throws IOException {
        // check that the number of sewers to read is within valid bounds (as 82036 is the max size of the .csv)
        assert  nLines > 0 && nLines <= 82036;

        // read .csv file
        BufferedReader fileReader = new BufferedReader(new FileReader("SewerAnalysis/sewers-mississauga.csv"));
        String line;
        int counter = 0;
        boolean firstLineSkipped = false;

        while ((line = fileReader.readLine()) != null) {
            if (Objects.equals(counter - 1, nLines)) {
                break;
            }
            // if we've skipped the header row or not in .csv
            if (!firstLineSkipped) {
                firstLineSkipped = true;
                counter += 1;
                continue;
            }
            counter += 1;

            /* .csv file follows a certain line format row-by-row. split each row into strings
            based on a comma delimiter to separate info needed to instantiate a Sewer object */
            String[] sewerParts = line.split(",");

            long assetIDParsed = Long.parseLong(sewerParts[0]);
            String objectType = sewerParts[1];
            String installDate = sewerParts[2];
            String region = sewerParts[3];
            short wardNumberParsed = Short.parseShort(sewerParts[4]);
            double xCoordParsed = Double.parseDouble(sewerParts[5]);
            double yCoordParsed = Double.parseDouble(sewerParts[6]);
            Sewer sewer = new Sewer
                    (assetIDParsed, objectType, installDate, region, wardNumberParsed, xCoordParsed, yCoordParsed);

            // add each Sewer in .csv to a list of Sewers attribute
            this.sewers.add(sewer);
        }
    }

    /**
     * Creates a list of Sewers in a given ward and worksYard
     *
     * @param worksYard the worksYard to filter by
     * @param ward the ward to filter by
     * @return a list of Sewers in the given worksYard and ward
     */
    public ArrayList<Sewer> filterWardWorksYard(String worksYard, short ward){
        ArrayList<Sewer> worksYard_Ward = new ArrayList<>();
        // loop through all Sewers and return those in given works yard and ward
        for (Sewer sewer : sewers) {
            if (Objects.equals(sewer.worksYard, worksYard) && sewer.ward == ward) {
                worksYard_Ward.add(sewer);
            }
        }
        return worksYard_Ward;
    }

    /**
     * Method to fetch a Sewer with a given assetId from this.sewers
     *
     * @param assetId the assetId to fetch
     * @return the Sewer with the given assetId, or null if no such Sewer exists
     */
    public Sewer fetchSewer(long assetId){
        // loop through all Sewers and return Sewer with matching ID
        for (Sewer sewer : sewers) {
            if (sewer.assetId == assetId) {
                return sewer;
            }
        }
        return null;
    }
}