public void ReadFile()
        {
            // Variables to be filled when the file is read line by line
            string locationName, streetNumAndName, county, postCode, latitude, longitude;
            string yearDescription, year;
            string monthID, maxTemp, minTemp, numDaysAirFrost, monthRainfallMillimetres, monthHoursSunshine;

            int numYears, numLocations;
            int numMonths = 12;

            try
            {
                StreamReader inputStream = new StreamReader(FileName);

                // Number of locations read at the top of the file
                numLocations = Convert.ToInt32(inputStream.ReadLine());

                // Check locations
                if (numLocations > 0)
                {
                    Locations = new List <Location>();
                }
                else
                {
                    inputStream.Close();
                    Locations = null;
                    throw new Exception("There are no locations in the file " + FileName);
                }

                // Iterates through locations
                for (int i = 0; i < numLocations; i++)
                {
                    locationName     = inputStream.ReadLine();
                    streetNumAndName = inputStream.ReadLine();
                    county           = inputStream.ReadLine();
                    postCode         = inputStream.ReadLine();
                    latitude         = inputStream.ReadLine();
                    longitude        = inputStream.ReadLine();

                    Location newLocation = CreateLocation(locationName, streetNumAndName, county, postCode, latitude, longitude);

                    numYears = Convert.ToInt32(inputStream.ReadLine());
                    List <Year> years = new List <Year>();

                    // Iterates through years
                    for (int j = 0; j < numYears; j++)
                    {
                        yearDescription = inputStream.ReadLine();
                        year            = inputStream.ReadLine();

                        Year newYear = CreateYear(year, yearDescription);

                        Month[] months = new Month[numMonths];

                        // Iterates through months
                        for (int k = 0; k < numMonths; k++)
                        {
                            monthID                  = inputStream.ReadLine();
                            maxTemp                  = inputStream.ReadLine();
                            minTemp                  = inputStream.ReadLine();
                            numDaysAirFrost          = inputStream.ReadLine();
                            monthRainfallMillimetres = inputStream.ReadLine();
                            monthHoursSunshine       = inputStream.ReadLine();

                            // Last month (month ID = 12) does not have year assigned, skips reading the year
                            if (k != numMonths - 1)
                            {
                                year = inputStream.ReadLine();
                            }

                            Month newMonth = new Month(monthID, maxTemp, minTemp, numDaysAirFrost, monthRainfallMillimetres, monthHoursSunshine);

                            months[k] = newMonth;
                        }

                        newYear.MonthObs = months;
                        years.Add(newYear);
                    }

                    newLocation.Years = years;
                    Locations.Add(newLocation);
                }

                inputStream.Close();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
예제 #2
0
        private void SetUpLocations()
        {
            //Variables to be filled when the file is read line by line
            string locationName, streetNumAndName, county, postCode, latitude, longitude;
            string yearDescription, year;
            string monthID, maxTemp, minTemp, numDaysAirFrost, monthRainfallMillimetres, monthHoursSunshine;

            //Number of locations and number of years within each location stored to determine number of for loop cycles
            int numYears, numLocations;

            try
            {
                //Input stream created to get information from file to the application
                StreamReader inputStream = new StreamReader(fileName);

                //Number of locations read at the top of the file
                numLocations = Convert.ToInt32(inputStream.ReadLine());

                //Array of locations in Data class created
                Data.storedLocations = new Location[1];

                //for loop cycles through every location in the file
                for (int i = 0; i < numLocations; i++)
                {
                    //All location details collected temporarily in variables
                    locationName     = inputStream.ReadLine();
                    streetNumAndName = inputStream.ReadLine();
                    county           = inputStream.ReadLine();
                    postCode         = inputStream.ReadLine();
                    latitude         = inputStream.ReadLine();
                    longitude        = inputStream.ReadLine();

                    //Number of years read from file
                    numYears = Convert.ToInt32(inputStream.ReadLine());

                    //Location object created and location details set by inputing the variables as arguments in the constructor
                    Location newLocation = new Location(locationName, streetNumAndName, county, postCode, latitude, longitude);

                    //Years array initialised and created
                    Year[] years = new Year[1];

                    //for loop cycles through every year of the location in the file
                    for (int j = 0; j < numYears; j++)
                    {
                        //All year details collected temporarily in variables
                        yearDescription = inputStream.ReadLine();
                        year            = inputStream.ReadLine();

                        //Year object created and year details set
                        Year newYear = new Year(year, yearDescription);

                        //Months array initialised and created
                        Month[] months = new Month[1];

                        //for loop cycles through every month in the year of the location in the file
                        for (int k = 0; k < 12; k++)
                        {
                            //All month details collected temporarily in variables
                            monthID                  = inputStream.ReadLine();
                            maxTemp                  = inputStream.ReadLine();
                            minTemp                  = inputStream.ReadLine();
                            numDaysAirFrost          = inputStream.ReadLine();
                            monthRainfallMillimetres = inputStream.ReadLine();
                            monthHoursSunshine       = inputStream.ReadLine();

                            //Last month (month ID = 12) in the file does not have year assigned to it so skip reading the year
                            if (k != 11)
                            {
                                year = inputStream.ReadLine();
                            }

                            //Month object created and month details set
                            Month newMonth = new Month(monthID, maxTemp, minTemp, numDaysAirFrost, monthRainfallMillimetres, monthHoursSunshine);

                            //Months array length increased by one so that another month can be added in the next iteration
                            Array.Resize(ref months, months.Length + 1);
                            //Stores month in appropriate position of the months array
                            months[k] = newMonth;
                        }

                        //When all months have been added to the months array the array length is decreased by one because no more months will be added
                        //This is done to prevent a null reference exception
                        Array.Resize(ref months, months.Length - 1);

                        //Months array assigned to monthly observations array in the year instance
                        newYear.MonthlyObservations = months;

                        //Years array length increased by one so that another year can be added in the next iteration
                        Array.Resize(ref years, years.Length + 1);
                        //Stores year in appropriate position of the years array
                        years[j] = newYear;
                    }

                    //Prevents null reference exception
                    Array.Resize(ref years, years.Length - 1);

                    //Years array assigned to years array in the location instance
                    newLocation.Years = years;

                    //Stored locations array increased by one so that another location can be added in the next iteration
                    Array.Resize(ref Data.storedLocations, Data.storedLocations.Length + 1);
                    //Stores location instance in appropriate position of the stored locations array in the data class
                    Data.storedLocations[i] = newLocation;
                }

                //Prevents null reference exception
                Array.Resize(ref Data.storedLocations, Data.storedLocations.Length - 1);
            }
            //Handles any exceptions that may occur when reading from the file
            catch (Exception e)
            {
                //Displays error message to the user
                MessageBox.Show(e.Message);
            }
        }