BillHung.Net


powered by FreeFind     sms

Lab 02 Movie ArrayList

24 Oct 2006

Output
using System;

namespace Lab02_Movie_ArrayList
{
    //Bill Chun Wai Hung
    //10/24/2006
    // learn file input and outputs
    // used string split to get string tokens and ICompare to sort

    class Program
    {
        private System.IO.StreamReader reader = null;
        private System.IO.StreamWriter writer = null;
        private System.Collections.ArrayList aList = new System.Collections.ArrayList();
        private CompareYear yrCompare = new CompareYear();

        static void Main(string[] args)
        {
            Program p = new Program();

            p.ListFile();
            p.CheckFileExists("movieshtml.txt");
            p.ReadFile("movieshtml.txt");
            p.WriteFile("movieshtml.output.txt");
            p.SortMovieYear();
            p.WriteFile("movieshtml.sorted.txt");
            p.HoldtheConsoleWindow();
        }

        //check if the file exists with the file name "filename"
        public void CheckFileExists(string filename)
        {
            if (System.IO.File.Exists(filename))
                Console.WriteLine("{0} exists.\n", filename);
            else
                Console.WriteLine("{0} not found.\n", filename);
        }

        //hold the display of the Console Window during debugging (pressing F5)
        public void HoldtheConsoleWindow()
        {
            Console.WriteLine("Press any keys to continue...");
            Console.ReadLine();
        }

        //list the files contained in the current directory
        public void ListFile()
        {
            System.IO.DirectoryInfo datadir = new System.IO.DirectoryInfo("."); //. for current directory
            System.IO.FileInfo[] files = datadir.GetFiles("*.*");

            //Print out the folder name and the list of files the folder contains
            Console.WriteLine("LIST FILES");
            Console.WriteLine("Folder (DirectoryInfo.Name): {0}", datadir.Name);
            foreach (System.IO.FileInfo f in files)
                Console.WriteLine("{0} \t{1} bytes", f.Name, f.Length);
            Console.WriteLine();

        }

        //read a file for input data and store the data into an ArrayList
        public void ReadFile(string sfilename)
        {
            string delimitStr = "\"<,";

            char[] delimiter = delimitStr.ToCharArray();
            string[] strLine = null;

            try
            {
                reader = new System.IO.StreamReader(sfilename);
            }
            catch
            {
                Console.WriteLine("open file {0} error\n", sfilename);
            }

            for (string line = reader.ReadLine(); line != null; line = reader.ReadLine())
            {
                Console.WriteLine(line);

                strLine = line.Split(delimiter);
                if (strLine[1] == "tr>")
                {
                    aList.Add(new Movie(strLine[3], strLine[5]));
                }
            }
            reader.Close();
        }

        //sort the ArrayList
        public void SortMovieYear()
        {
            aList.Sort(yrCompare);
        }

        //write the movie data from the array list to an output file
        public void WriteFile(string sfilename)
        {
            try
            {
                writer = new System.IO.StreamWriter(sfilename);
            }
            catch
            {
                Console.WriteLine("open file {0} error\n", sfilename);
            }

            foreach (Movie movie in aList)
            {
                Console.WriteLine("{0}\t{1}", movie.getName(), movie.getYear());
                writer.WriteLine("{0}\t{1}", movie.getName(), movie.getYear());
            }
            writer.Close();

        }

        
    }

    class Movie
    {
        String moviename = null;
        int year = 0;

        public Movie() { }
        public Movie(String strMoviename, String strYear)
        {
            moviename = strMoviename;
            year = System.Convert.ToInt32(strYear);
        }

        public String getName() { return moviename; }
        public int getYear() { return year; }
    }

    class CompareYear : System.Collections.IComparer
    {
        int System.Collections.IComparer.Compare(Object x, Object y)
        {
            Movie mx = (Movie)x;
            Movie my = (Movie)y;
            return (mx.getYear() - my.getYear());
        }
        
    }
    
}