Exemplo n.º 1
0
        /// <summary>
        /// Creates the Data Structure with the desired Headers
        /// </summary>
        /// <param name="headersLine">Line with headers in file</param>
        /// <returns></returns>
        public static EXODataStructure Parse(string headersLine)
        {
            //Creates a new list of EXODataHeaders
            List <EXODataHeader> headers = new List <EXODataHeader>();

            //Creates an Array with all headers foun in line
            string[] headersInFile = headersLine.Split(',');

            //Creates a new List with all the Relevant Headers
            List <string> relevantHeaders =
                new List <string>(Constants.RelevantHeaders.Split(','));

            //Cycle that adds a valid header to the EXODataHeaders list
            for (short i = 0; i < headersInFile.Length; i++)
            {
                //Creates a new EXODataHeader based on the current
                //header being read
                EXODataHeader header = new EXODataHeader(headersInFile[i].Trim(), i);

                //If the header doesn't have an ID...
                if (string.IsNullOrEmpty(header.Id))
                {
                    //...throws an Invalid Header Exception
                    throw new Exception("Invalid column header.");
                }


                //Checks if the relevantHeaders list has an header
                //with the current header ID...
                if (relevantHeaders.Contains(header.Id))
                {
                    //...if it has it checks if the EXODataHeader List
                    //already has the current header...
                    if (headers.Contains(header))
                    {
                        //...and if so throws a Repeated Column Exception
                        throw new Exception("Repeated columns.");
                    }

                    //else it adds the current header to EXODataHeader List
                    headers.Add(header);
                }
            }

            //Checks if the EXODataHeaders list doesn't contain the headers
            //with the corresponding Constant...
            if (!headers.ContainsHeaderName(Constants.PlanetNameHeader) ||
                !headers.ContainsHeaderName(Constants.HostNameHeader))
            {
                //...and throws an Mandatory Column Missing Exception
                throw new Exception("Mandatory column missing.");
            }

            //return the new EXODataStructure with the EXODataHeaders list
            //into an array
            return(new EXODataStructure(headers.ToArray()));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Method used to Set the Planet's Information into the corresponding
        /// Data fields
        /// </summary>
        /// <param name="header">Data Header</param>
        /// <param name="planet">Planet</param>
        /// <param name="values">Data Header information</param>
        private void SetDataField(EXODataHeader header, Planet planet, string[] values)
        {
            string value = values[header.PositionIndex].Trim();

            //if the data field is empty doesn't add it
            if (string.IsNullOrEmpty(value))
            {
                return;
            }

            //Verifys which DataField is going to be set based on the Header's ID
            switch (header.Id)
            {
            case Constants.DiscoveryMethod:
                planet.DiscoveryMethod = value;
                break;

            case Constants.DiscoveryYear:
                planet.DiscoveryYear = short.Parse(value,
                                                   NumberStyles.Any, CultureInfo.InvariantCulture);
                break;

            case Constants.OrbitalPeriod:
                planet.OrbitalPeriod = float.Parse(value,
                                                   NumberStyles.Any, CultureInfo.InvariantCulture);
                break;

            case Constants.PlanetRadius:
                planet.Radius = float.Parse(value,
                                            NumberStyles.Any, CultureInfo.InvariantCulture);
                break;

            case Constants.PlanetMass:
                planet.Mass = float.Parse(value,
                                          NumberStyles.Any, CultureInfo.InvariantCulture);
                break;

            case Constants.EquilibriumTemperature:
                planet.EquilibriumTemperature = float.Parse(value,
                                                            NumberStyles.Any, CultureInfo.InvariantCulture);
                break;

            case Constants.StellarEffectiveTemperature:
                planet.Host.EffectiveTemperature = float.Parse(value,
                                                               NumberStyles.Any, CultureInfo.InvariantCulture);
                break;

            case Constants.StellarRadius:
                planet.Host.Radius = float.Parse(value,
                                                 NumberStyles.Any, CultureInfo.InvariantCulture);
                break;

            case Constants.StellarMass:
                planet.Host.Mass = float.Parse(value,
                                               NumberStyles.Any, CultureInfo.InvariantCulture);
                break;

            case Constants.StellarAge:
                planet.Host.Age = float.Parse(value,
                                              NumberStyles.Any, CultureInfo.InvariantCulture);
                break;

            case Constants.StellarRotationSpeed:
                planet.Host.RotationSpeed = float.Parse(value,
                                                        NumberStyles.Any, CultureInfo.InvariantCulture);
                break;

            case Constants.StellarRotationPeriod:
                planet.Host.RotationPeriod = float.Parse(value,
                                                         NumberStyles.Any, CultureInfo.InvariantCulture);
                break;

            case Constants.SunDistance:
                planet.Host.SunDistance = float.Parse(value,
                                                      NumberStyles.Any, CultureInfo.InvariantCulture);
                break;
            }
        }