コード例 #1
0
ファイル: Test.cs プロジェクト: steffdimitrov/TelerikAcademy
        public static void Main()
        {
            Point3D coordinateBeginning = Point3D.O;

            Console.WriteLine(coordinateBeginning);

            Point3D firstPoint = new Point3D(3, 4, 3.75);

            Console.WriteLine(Distance.CalculateDistance(firstPoint, coordinateBeginning));

            Point3D[] samplePath = new Point3D[10];
            for (int i = 0; i < samplePath.Length; i++)
            {
                // calculate some strange coordinates just for testing
                double x = (3 * i * i) - (4 * i) - 5;
                double y = (i * i) - (2 * i) + 8;
                double z = (4 * i) - 10;
                samplePath[i] = new Point3D(x, y, z);
            }

            Path journey = new Path("My trip", samplePath);

            journey.AddPointsToPath(firstPoint);
            Console.WriteLine(journey);

            //Console.WriteLine(journey[15]); // Out of range exeption

            PathStorage.SavePath(journey, "test.txt");

            Path secondJourney = PathStorage.OpenPath("test.txt");

            // Path thirdJourney = PathStorage.OpenPath("tes.txt"); // File not found exception
        }
コード例 #2
0
        public static Path OpenPath(string fileName)
        {
            Path output = new Path();

            try
            {
                StreamReader reader = new StreamReader(fileName);
                using (reader)
                {
                    output.Name = reader.ReadLine();

                    int pathLenght = int.Parse(reader.ReadLine());
                    for (int i = 0; i < pathLenght; i++)
                    {
                        string[] pointCoordinates = reader.ReadLine().Split(' ');
                        Point3D  point            = new Point3D();
                        point.X = double.Parse(pointCoordinates[0]);
                        point.Y = double.Parse(pointCoordinates[1]);
                        point.Z = double.Parse(pointCoordinates[2]);
                        output.AddPointsToPath(point);
                    }
                }
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("File name cannot be empty string.");
                Console.WriteLine(e.Message);
            }
            catch (ArgumentException e)
            {
                Console.WriteLine("Wrong or incorect file name");
                Console.WriteLine(e.Message);
            }
            catch (DirectoryNotFoundException e)
            {
                Console.WriteLine("Wrong or missing directory.");
                Console.WriteLine(e.Message);
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine("File not found.");
                Console.WriteLine(e.Message);
            }
            catch (IOException e)
            {
                Console.WriteLine("File not saved.");
                Console.WriteLine(e.Message);
            }

            return(output);
        }