Ejemplo n.º 1
0
 /// <summary>
 /// Constructor for setting up the Trip class for use
 /// </summary>
 /// <param name="stations">An array of Station representing the stations to be sorted</param>
 /// <param name="plane">A instance of a plane class that contains the plane specifications</param>
 /// <param name="time">An instance of TripTime that has been setup for the trip</param>
 /// <param name="outPath">A string representing the output path</param>
 public Trip(Station[] stations, Plane plane, TripTime time, string outPath)
 {
     this.stations = stations;
     this.plane    = plane;
     this.time     = time;
     this.outPath  = outPath;
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Method <c>ParseArgs</c> handles parsing the input files and checking for any bad
        /// inputs within the files. <c>ValidateArgs</c> is run first to ensure the arguments
        /// and files bring parsed exist and are valid.
        ///
        /// </summary>
        /// <param name="args">The command line argument array to be checked and parsed</param>
        public void ParseArgs(string[] args)
        {
            //Run args array though the ValidateArgs method to validate arguments
            try
            {
                ValidateArgs(args);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return;
            }

            var STATION = args[0];
            var PLANE   = args[1];
            var TIME    = args[2];

            const char DELIMITER = ' ';

            string[] fields;

            //Parse station input
            var stationCount = File.ReadLines(STATION).Count();

            _stations = new Station[stationCount];
            var stationFile   = new FileStream(STATION, FileMode.Open, FileAccess.Read);
            var stationReader = new StreamReader(stationFile);
            var recordIn      = stationReader.ReadLine();

            //Iterate over the mail file line by line and convert into a Station object,
            //if the line is not valid catch the exception and return to the main program.
            for (var i = 0; i < _stations.Length; i++)
            {
                if (recordIn == null)
                {
                    continue;
                }
                fields = recordIn.Split(DELIMITER);
                try
                {
                    _stations[i] = new Station(fields[0],
                                               Convert.ToInt32(fields[1]),
                                               Convert.ToInt32(fields[2]));
                    recordIn = stationReader.ReadLine();
                }
                catch (Exception)
                {
                    Console.WriteLine("Input on line {0} in {1} is not the correct format \n" +
                                      "Format need: StationName (int)X-pos (int)Y-pos", i + 1,
                                      Path.GetFileName(STATION));
                    return;
                }
            }
            stationFile.Close();
            stationReader.Close();

            //Parse plane input
            var planeSpecs  = new FileStream(PLANE, FileMode.Open, FileAccess.Read);
            var specsReader = new StreamReader(planeSpecs);

            recordIn = specsReader.ReadLine();

            //If line exists then convert to Plane object, if the line is not valid catch
            //the exception and return to the main program.
            if (recordIn != null)
            {
                fields = recordIn.Split(DELIMITER);

                try
                {
                    _plane = new Plane
                    {
                        Range       = Convert.ToSingle(fields[0]),
                        Speed       = Convert.ToInt32(fields[1]),
                        TakeoffTime = Convert.ToInt32(fields[2]),
                        LandingTime = Convert.ToInt32(fields[3]),
                        RefuelTime  = Convert.ToInt32(fields[4])
                    };
                }
                catch (Exception)
                {
                    Console.WriteLine("Input in {0} is not the correct format \n" +
                                      "Format needed: (int)Range (int)Speed (int)Take-Off-Time " +
                                      "(int)Landing-Time (int)Refuel-Time", Path.GetFileName(PLANE));
                    return;
                }
            }

            planeSpecs.Close();
            specsReader.Close();

            //Time input Argument returns an instance of the setup time class
            _time = new TripTime(TIME);

            //Output
            try
            {
                if (args.Length == 5)
                {
                    outPath = args[4];
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            _parseCleared = true;
        }