コード例 #1
0
        static void Main(string[] args)
        {
            GradeBook book = new GradeBook("Daniel's book");
            GradebookThatThrowsAwayLowestGrade book2 = new GradebookThatThrowsAwayLowestGrade("Toms's book");

            try
            {
                using (FileStream stream = File.Open("Grades.txt", FileMode.Open))
                    using (StreamReader reader = new StreamReader(stream))
                    {                        // when you exit "using" it calls "Dispose" method that close anything that was opened/created in "using"
                        string line = reader.ReadLine();
                        while (line != null) // if == null --> this means end of file has been achieved
                        {
                            float grade = float.Parse(line);
                            book.AddGrade(grade);
                            book2.AddGrade(grade);
                            line = reader.ReadLine(); // reads NEXT line of file into memory
                        }
                    }
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("Coldn't locate file Grades.txt");
                return;
            }
            catch (UnauthorizedAccessException)
            {
                Console.WriteLine("No access");
                return;
            }

            //book.AddGrade(91f);
            //book.AddGrade(89.1f);
            //book.AddGrade(75f);
            book.WriteGrades(Console.Out);
            book2.WriteGrades(Console.Out);


            //do
            //    try
            //    {
            //        {
            //            Console.WriteLine("Please enter a name for the book");
            //            book.Name = Console.ReadLine();
            //            Console.WriteLine("Please enter a name for the book that throws away lowest grade");
            //            book2.Name = Console.ReadLine();
            //        }
            //    }
            //    catch (ArgumentException ex)
            //    {
            //        Console.WriteLine("Invalid name");
            //        Console.WriteLine(ex.Message);
            //        Console.WriteLine();
            //    }

            //while (book.Name == "Daniel's book");

            GradeStatistics stats  = book.ComputeStatistic();
            GradeStatistics stats2 = book2.ComputeStatistic();

            Console.WriteLine(book.Name);
            WriteStatistics(stats);
            Console.WriteLine(book2.Name);
            WriteStatistics(stats2);



            //book.NameChanged += OnNameChanged;
            //book.Name = "Scott's book";
            //WriteNames(book.Name);

            //int number = 20;
            //WriteBytes(number);
            //WriteBytes(stats.AverageGrade);

            //Immutable();
            //ParamsAsValueOrAsReference();
        }