Beispiel #1
0
        /// <summary>
        /// Opens file and identifies its header
        /// </summary>
        private void FindValAttributeIndex()
        {
            // String representing line of the file
            string attributeline;

            try
            {
                // READS CSV FILE
                using (StreamReader sr = new StreamReader(file))
                {
                    // Saves document's first line
                    do
                    {
                        attributeline = sr.ReadLine(); firstValLine++;
                    }
                    // Skips lines that start with '#' or that are empty strings
                    // Ends with line holding column contents
                    while (attributeline[0] == '#' || attributeline == "");

                    // Create array from columns' line
                    string[] attribs = attributeline.Split(',');
                    // Saves number of columns on file header in class variable
                    totalAttColl = attribs.Length;

                    // Starts FindVallAttributes with the array containing the
                    // items of the header
                    FindValAttributes(attribs);
                }
            }
            catch (Exception)
            {
                ExceptionManager.ExceptionControl(ErrorCodes.NoFileFound);
            }
        }
Beispiel #2
0
        /// <summary>
        /// Method that verifies the arguments from the user and adds them
        /// to their respective dictionary
        /// </summary>
        private void Options()
        {
            for (int i = 0; i < args.Length; i++)
            {
                if (boolArgs.ContainsKey(args[i].ToLower()))
                {
                    boolArgs[args[i]] = true;
                }
                try
                {
                    if (stringArgs.ContainsKey(args[i].ToLower()))
                    {
                        stringArgs[args[i]] = args[i + 1];
                    }

                    if (floatArgs.ContainsKey(args[i].ToLower()))
                    {
                        floatArgs[args[i]] = Single.Parse(args[i + 1], NumberStyles.Any,
                                                          CultureInfo.InvariantCulture);
                    }
                }
                catch (Exception)
                {
                    ExceptionManager.ExceptionControl(ErrorCodes.NoArgGivenToString);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// Method that verifies exceptions in the user arguments
        /// </summary>
        public void CheckForArgumentExceptions()
        {
            // IncompatibleOptions
            if (boolArgs["-search-planet"] &&
                boolArgs["-search-star"])
            {
                ExceptionManager.ExceptionControl(
                    ErrorCodes.IncompatibleOptions);
            }

            // No Search Exception
            if (!boolArgs["-search-planet"] &&
                !boolArgs["-search-star"] &&
                !boolArgs["-help"])
            {
                ExceptionManager.ExceptionControl(ErrorCodes.NoSearchOption);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Creates a collection of Planets from the given csv file
        /// </summary>
        private void CreatePlanetCollection()
        {
            // Array that holds one line's important attributes as strings
            string[] planetAttributes = new string[8];
            // String representing line of the file
            string line;

            try
            {
                // READS CSV FILE
                using (StreamReader sr = new StreamReader(file))
                {
                    // Skip unwanted lines of the file
                    for (int i = 0; i <= firstValLine; i++)
                    {
                        sr.ReadLine();
                    }

                    // Read through every line until reaching empty line (end)
                    while ((line = sr.ReadLine()) != null)
                    {
                        // Turn line into string array (split csv line on ',')
                        string[] attribs = line.Split(',');

                        // Stops program and sends error message that a
                        // line in the file didn't have the same number
                        // of elements as header
                        if (attribs.Length != totalAttColl)
                        {
                            Console.WriteLine(line);
                            ExceptionManager.ExceptionControl(
                                ErrorCodes.AttribNumFluct);
                        }

                        /*
                         * Select attributes significant to Planet and
                         * add them to planetAttributes
                         */

                        // Name
                        if (!nameFound)
                        {
                            planetAttributes[0] = null;
                        }
                        else
                        {
                            planetAttributes[0] =
                                attribs.ElementAt(valAttPos[
                                                      (int)AttribPos.pl_name]) != "" ?

                                attribs.ElementAt(
                                    valAttPos[(int)AttribPos.pl_name]) :
                                "[MISSING]";
                        }

                        // Host Name
                        if (!hostNameFound)
                        {
                            planetAttributes[1] = null;
                        }
                        else
                        {
                            planetAttributes[1] =
                                attribs.ElementAt(valAttPos[
                                                      (int)AttribPos.pl_hostName]) != "" ?

                                attribs.ElementAt(
                                    valAttPos[(int)AttribPos.pl_hostName]) :
                                "[MISSING]";
                        }

                        // Discovery Method
                        if (!discMethodFound)
                        {
                            planetAttributes[2] = null;
                        }
                        else
                        {
                            planetAttributes[2] =
                                attribs.ElementAt(valAttPos[
                                                      (int)AttribPos.pl_discMethod]) != "" ?

                                attribs.ElementAt(
                                    valAttPos[(int)AttribPos.pl_discMethod]) :
                                "[MISSING]";
                        }

                        // Discovery Year
                        if (!discYearFound)
                        {
                            planetAttributes[3] = null;
                        }
                        else
                        {
                            planetAttributes[3] =
                                attribs.ElementAt(valAttPos[
                                                      (int)AttribPos.pl_discYear]) != "" ?

                                attribs.ElementAt(
                                    valAttPos[(int)AttribPos.pl_discYear]) :
                                "0";
                        }

                        // Orbit Period
                        if (!orbPerFound)
                        {
                            planetAttributes[4] = null;
                        }
                        else
                        {
                            planetAttributes[4] =
                                attribs.ElementAt(valAttPos[
                                                      (int)AttribPos.pl_orbPer]) != "" ?

                                attribs.ElementAt(
                                    valAttPos[(int)AttribPos.pl_orbPer]) :
                                "0";
                        }

                        // Radius
                        if (!plRadFound)
                        {
                            planetAttributes[5] = null;
                        }
                        else
                        {
                            planetAttributes[5] =
                                attribs.ElementAt(valAttPos[
                                                      (int)AttribPos.pl_rade]) != "" ?

                                attribs.ElementAt(
                                    valAttPos[(int)AttribPos.pl_rade]) :
                                "0";
                        }

                        // Mass
                        if (!plMassFound)
                        {
                            planetAttributes[6] = null;
                        }
                        else
                        {
                            planetAttributes[6] =
                                attribs.ElementAt(valAttPos[
                                                      (int)AttribPos.pl_mass]) != "" ?

                                attribs.ElementAt(
                                    valAttPos[(int)AttribPos.pl_mass]) :
                                "0";
                        }

                        // Equilibrium Temperature
                        if (!eqTempFound)
                        {
                            planetAttributes[7] = null;
                        }
                        else
                        {
                            planetAttributes[7] =
                                attribs.ElementAt(valAttPos[
                                                      (int)AttribPos.pl_eqt]) != "" ?

                                attribs.ElementAt(
                                    valAttPos[(int)AttribPos.pl_eqt]) :
                                "0";
                        }

                        Planet p = new Planet(

                            planetAttributes[0].Trim(), planetAttributes[1].Trim(),
                            planetAttributes[2].Trim(), planetAttributes[3].Trim(),
                            planetAttributes[4].Trim(), planetAttributes[5].Trim(),
                            planetAttributes[6].Trim(), planetAttributes[7].Trim());

                        HashSetPL.Add(p);
                    }
                }
            }
            catch (Exception)
            {
                ExceptionManager.ExceptionControl(ErrorCodes.NoFileFound);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Searches the file header to find the positions of the Valuable
        /// Attributes, saving the positions of the ones that are found.
        /// </summary>
        /// <param name="atributeLine">csv file header</param>
        private void FindValAttributes(string[] atributeLine)
        {
            for (int i = 0; i < atributeLine.Length; i++)
            {
                switch (atributeLine[i].Trim())
                {
                case "pl_name":
                    valAttPos[(int)AttribPos.pl_name] = i;
                    nameFound = true;
                    break;

                case "hostname":
                    valAttPos[(int)AttribPos.pl_hostName] = i;
                    hostNameFound = true;
                    break;

                case "discoverymethod":
                    valAttPos[(int)AttribPos.pl_discMethod] = i;
                    discMethodFound = true;
                    break;

                case "disc_year":
                    valAttPos[(int)AttribPos.pl_discYear] = i;
                    discYearFound = true;
                    break;

                case "pl_orbper":
                    valAttPos[(int)AttribPos.pl_orbPer] = i;
                    orbPerFound = true;
                    break;

                case "pl_rade":
                    valAttPos[(int)AttribPos.pl_rade] = i;
                    plRadFound = true;
                    break;

                case "pl_masse":
                    valAttPos[(int)AttribPos.pl_mass] = i;
                    plMassFound = true;
                    break;

                case "pl_eqt":
                    valAttPos[(int)AttribPos.pl_eqt] = i;
                    eqTempFound = true;
                    break;

                case "st_teff":
                    valAttPos[(int)AttribPos.st_teff] = i;
                    effTempFound = true;
                    break;

                case "st_rad":
                    valAttPos[(int)AttribPos.st_rad] = i;
                    stRadFound = true;
                    break;

                case "st_mass":
                    valAttPos[(int)AttribPos.st_mass] = i;
                    stMassFound = true;
                    break;

                case "st_age":
                    valAttPos[(int)AttribPos.st_age] = i;
                    ageFound = true;
                    break;

                case "st_vsin":
                    valAttPos[(int)AttribPos.st_vsin] = i;
                    rotVelFound = true;
                    break;

                case "st_rotp":
                    valAttPos[(int)AttribPos.st_rotp] = i;
                    rotPerFound = true;
                    break;

                case "sy_dist":
                    valAttPos[(int)AttribPos.sy_dist] = i;
                    distSunFound = true;
                    break;
                }
            }

            // Stops program and sends error message that
            // the file is missing atleast one of the main attributes
            // 'pl_name' or 'hostname'
            if (!nameFound || !hostNameFound)
            {
                ExceptionManager.ExceptionControl(
                    ErrorCodes.AttribsMissing);
            }
        }
Beispiel #6
0
        /// <summary>
        /// Creates a collection of Stars from the given csv file
        /// </summary>
        private void CreateStarCollection()
        {
            // Array that holds one line's important attributes as strings
            string[] starAttributes = new string[9];
            // String representing line of the file
            string line;

            try
            {
                // READS CSV FILE
                using (StreamReader sr = new StreamReader(file))
                {
                    // Skip unwanted lines of the file
                    for (int i = 0; i < firstValLine; i++)
                    {
                        sr.ReadLine();
                    }

                    // Read through every line until reaching end of file
                    while ((line = sr.ReadLine()) != null)
                    {
                        // Turn line into string array (split csv line on ',')
                        string[] attribs = line.Split(',');

                        // Stops program and sends error message that a
                        // line in the file didn't have the same number
                        // of elements as header
                        if (attribs.Length < totalAttColl)
                        {
                            ExceptionManager.ExceptionControl(
                                ErrorCodes.AttribNumFluct);
                        }

                        /*
                         * Select attributes significant to Star and
                         * add them to starAttributes
                         */

                        // Star Name (Host)
                        if (!hostNameFound)
                        {
                            starAttributes[0] = null;
                        }
                        else
                        {
                            starAttributes[0] =
                                attribs.ElementAt(valAttPos[
                                                      (int)AttribPos.pl_hostName]) != "" ?

                                attribs.ElementAt(
                                    valAttPos[(int)AttribPos.pl_hostName]) :
                                "[MISSING]";
                        }

                        // Effective Temperature
                        if (!effTempFound)
                        {
                            starAttributes[1] = null;
                        }
                        else
                        {
                            starAttributes[1] =
                                attribs.ElementAt(valAttPos[
                                                      (int)AttribPos.st_teff]) != "" ?

                                attribs.ElementAt(
                                    valAttPos[(int)AttribPos.st_teff]) :
                                "0";
                        }

                        // Star Radius
                        if (!stRadFound)
                        {
                            starAttributes[2] = null;
                        }
                        else
                        {
                            starAttributes[2] =
                                attribs.ElementAt(valAttPos[
                                                      (int)AttribPos.st_rad]) != "" ?

                                attribs.ElementAt(
                                    valAttPos[(int)AttribPos.st_rad]) :
                                "0";
                        }

                        // Star Mass
                        if (!stMassFound)
                        {
                            starAttributes[3] = null;
                        }
                        else
                        {
                            starAttributes[3] =
                                attribs.ElementAt(valAttPos[
                                                      (int)AttribPos.st_mass]) != "" ?

                                attribs.ElementAt(
                                    valAttPos[(int)AttribPos.st_mass]) :
                                "0";
                        }

                        // Star Age
                        if (!ageFound)
                        {
                            starAttributes[4] = null;
                        }
                        else
                        {
                            starAttributes[4] =
                                attribs.ElementAt(valAttPos[
                                                      (int)AttribPos.st_age]) != "" ?

                                attribs.ElementAt(
                                    valAttPos[(int)AttribPos.st_age]) :
                                "0";
                        }

                        // Star Rotation Velocity
                        if (!rotVelFound)
                        {
                            starAttributes[5] = null;
                        }
                        else
                        {
                            starAttributes[5] =
                                attribs.ElementAt(valAttPos[
                                                      (int)AttribPos.st_vsin]) != "" ?

                                attribs.ElementAt(
                                    valAttPos[(int)AttribPos.st_vsin]) :
                                "0";
                        }

                        // Star Rotation Period
                        if (!rotPerFound)
                        {
                            starAttributes[6] = null;
                        }
                        else
                        {
                            starAttributes[6] =
                                attribs.ElementAt(valAttPos[
                                                      (int)AttribPos.st_rotp]) != "" ?

                                attribs.ElementAt(
                                    valAttPos[(int)AttribPos.st_rotp]) :
                                "0";
                        }

                        // Distance to Sun
                        if (!distSunFound)
                        {
                            starAttributes[7] = null;
                        }
                        else
                        {
                            starAttributes[7] =
                                attribs.ElementAt(valAttPos[
                                                      (int)AttribPos.sy_dist]) != "" ?

                                attribs.ElementAt(
                                    valAttPos[(int)AttribPos.sy_dist]) :
                                "0";
                        }

                        Star s = new Star(
                            starAttributes[0].Trim(), starAttributes[1].Trim(),
                            starAttributes[2].Trim(), starAttributes[3].Trim(),
                            starAttributes[4].Trim(), starAttributes[5].Trim(),
                            starAttributes[6].Trim(), starAttributes[7].Trim());


                        HashSetST.Add(s);
                    }
                }
            }
            catch (Exception)
            {
                ExceptionManager.ExceptionControl(ErrorCodes.NoFileFound);
            }
        }
Beispiel #7
0
        /// <summary>
        /// Method that shows the final filtered collection, to the user
        /// </summary>
        public void ShowCollection()
        {
            if (boolArgs["-help"] == true)
            {
                ExceptionManager.ShowHelp();
            }

            if (boolArgs["-search-planet"] == true)
            {
                if (fs.FilteredPlanetCollection.Count() == 0)
                {
                    ExceptionManager.ExceptionControl(ErrorCodes.NoDataFound);
                }

                IEnumerable <Planet> planetEqualInfo =
                    from planet in fs.FilteredPlanetCollection
                    // the string given by the player is the planet name
                    where planet.Name.ToLower().Equals(
                        stringArgs["-planet-name"].ToLower().Trim())
                    select planet;

                if (planetEqualInfo.Count() >= 1)
                {
                    Planet finalPlanet = planetEqualInfo.ElementAt(0);

                    foreach (Planet p in planetEqualInfo)
                    {
                        finalPlanet += p;
                    }
                    finalPlanet.ConvertFloatablesToDefault();
                    Console.WriteLine(finalPlanet.ToString(boolArgs["-csv"]));
                }
                else
                {
                    foreach (Planet p in fs.FilteredPlanetCollection)
                    {
                        p.ConvertFloatablesToDefault();
                        Console.WriteLine(p.ToString(boolArgs["-csv"]));
                    }
                }
            }

            else if (boolArgs["-search-star"] == true)
            {
                if (fs.FilteredStarCollection.Count() == 0)
                {
                    ExceptionManager.ExceptionControl(ErrorCodes.NoDataFound);
                }

                IEnumerable <Star> starEqualInfo =
                    from star in fs.FilteredStarCollection
                    // the string given by the player is the star name
                    where star.StarName.ToLower().Equals(
                        stringArgs["-host-name"].ToLower().Trim())
                    select star;

                if (starEqualInfo.Count() >= 1)
                {
                    Star finalStar = starEqualInfo.ElementAt(0);

                    for (int i = 0; i < starEqualInfo.Count(); i++)
                    {
                        finalStar += starEqualInfo.ElementAt(i);
                    }

                    finalStar.ConvertFloatablesToDefault();
                    Console.WriteLine(finalStar.ToString(boolArgs["-csv"]));
                    Console.WriteLine("Number of Planets: " + starEqualInfo.Count());
                }
                else
                {
                    foreach (Star s in fs.FilteredStarCollection)
                    {
                        s.ConvertFloatablesToDefault();
                        Console.WriteLine(s.ToString(boolArgs["-csv"]));
                    }
                }
            }
        }