C#:搜索数组

时间:2012-12-07 22:42:30

标签: c# arrays search

我有一个存储dvds和蓝光的DVD应用程序,我想通过导演搜索数组。下面是库存类的代码我已经看到了很多不同的方法。似乎有一些争论是最好/最有效的方法来实现这一点,任何建议?

块引用

    namespace MovieInventoryApplication
{
class Inventory
{
    public Bluray[] BlurayMovies;
    public DVD[] DVDMovies;

    private int blurayCount;
    private int dvdCount;

    public Inventory()
    {
        BlurayMovies = new Bluray[5];
        DVDMovies = new DVD[5];

        blurayCount = 0;
        dvdCount = 0;
    }

    public void AddBluray()
    {
        String strTitle;
        int intReleaseYear;
        int intRunningTimeMinutes;
        String strDirector;
        int intPrice;
        int intRegionCode;

        try
        {
            Console.Write("Enter a title: ");
            strTitle = Console.ReadLine();

            Console.Write("Enter a release year: ");
            intReleaseYear = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the running time in minutes: ");
            intRunningTimeMinutes = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the directors name: ");
            strDirector = Console.ReadLine();

            Console.Write("Enter a rental price: ");
            intPrice = Convert.ToInt32(Console.ReadLine());

            BlurayMovies[blurayCount] = new Bluray(strTitle, intReleaseYear, intRunningTimeMinutes, strDirector, intPrice);
            blurayCount++;

            Console.Write("Enter the DVD region code: ");
            intRegionCode = Convert.ToInt32(Console.ReadLine());

            DVDMovies[dvdCount] = new DVD(strTitle, intReleaseYear, intRunningTimeMinutes, strDirector, intPrice, intRegionCode);
            dvdCount++;
        }
        catch (FormatException FormatException)
        {
            Console.WriteLine(FormatException.Message);
            Console.WriteLine("Please enter a number in this field.");
        }
    }

    public void AddDVD()
    {
        String strTitle;
        int intReleaseYear;
        int intRunningTimeMinutes;
        String strDirector;
        int intPrice;
        int intRegionCode;

        try
        {
            Console.Write("Enter a title: ");
            strTitle = Console.ReadLine();

            Console.Write("Enter a release year: ");
            intReleaseYear = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the running time in minutes: ");
            intRunningTimeMinutes = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the directors name: ");
            strDirector = Console.ReadLine();

            Console.Write("Enter a rental price: ");
            intPrice = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter the region code: ");
            intRegionCode = Convert.ToInt32(Console.ReadLine());

            DVDMovies[dvdCount] = new DVD(strTitle, intReleaseYear, intRunningTimeMinutes, strDirector, intPrice, intRegionCode);
            dvdCount++;
        }

        catch (FormatException FormatException)
        {
            Console.WriteLine(FormatException.Message);
            Console.WriteLine("Please enter a number in this field.");
        }
    }

    public void ListAllBluray()
    {
        int position = 0;

        while (BlurayMovies[position] != null)
        {
            Console.WriteLine(position + " " + BlurayMovies[position].strTitle);

            position++;
        }
    }

    public void ListAllDVD()
    {
        int position = 0;

        while (DVDMovies[position] != null)
        {
            //position + 1 + " " + 
            Console.WriteLine(position + " " + DVDMovies[position].strTitle);

            position++;
        }
    }

    public void BlurayInfo(int position)
    {
        Console.WriteLine("Title: {0}", DVDMovies[position].strTitle);
        Console.WriteLine("Release Year: {0}", DVDMovies[position].intReleaseYear);
        Console.WriteLine("Running Time (Minutes): {0}", DVDMovies[position].intRunningTimeMinutes);
        Console.WriteLine("Director: {0}", DVDMovies[position].strDirector);
        Console.WriteLine("Price: {0}", DVDMovies[position].intPrice);
    }

    public void DVDInfo(int position)
    {
        Console.WriteLine("Title: {0}", DVDMovies[position].strTitle);
        Console.WriteLine("Release Year: {0}", DVDMovies[position].intReleaseYear);
        Console.WriteLine("Running Time (Minutes): {0}", DVDMovies[position].intRunningTimeMinutes);
        Console.WriteLine("Director: {0}", DVDMovies[position].strDirector);
        Console.WriteLine("Price: {0}", DVDMovies[position].intPrice);
        Console.WriteLine("Region Code: {0}", DVDMovies[position].intRegionCode);
    }
}

}

2 个答案:

答案 0 :(得分:1)

我认为你的设计存在缺陷。

DVD和BluRay课程应该是:

  1. 具有Type属性的单个类,可能是包含DVD和BluRay的枚举。这样,一旦你获得新媒体,你可以在几年内向Enum添加一个值,你的应用程序将是最新的。
  2. 实现可以调用的自定义界面的两个不同的类,例如IMedia
  3. 另外,我强烈建议您利用C#中的List对象而不是数组。它非常快,您可以轻松添加/删除项目,而无需调整阵列大小。

    这是关于Linq的课程:http://www.functionx.com/csharp/linq/Lesson09.htm

    以下是我将如何创建Media类:

    public class Media
    {
        public enum MediaType
        {
            DVD,
            Bluray
        }
    
        public MediaType TypeOfMedia { get; set; }
        public string Director { get; set; }
        public string Title { get; set; }
    
        public Media(string Title, string Director, MediaType TypeOfMedia)
        {
            this.TypeOfMedia = TypeOfMedia;
            this.Director = Director;
            this.Title = Title;
        }
    }
    

    以下是如何使用它的示例:

            List<Media> data = new List<Media>();
    
            results.Add(new Media("Movie 1", "John D", Media.MediaType.DVD));
            results.Add(new Media("Movie 2", "John D", Media.MediaType.DVD));
            results.Add(new Media("Movie 3", "SomeOtherDirector", Media.MediaType.Bluray));
            results.Add(new Media("Movie 4", "John D", Media.MediaType.Bluray));
    
            IEnumerable<Media> listDirectors = from media in data
                                               where media.Director == "John D"
                                               select media;
    
            foreach (Media media in listDirectors)
                Console.WriteLine(media.Title);
    

    另一个例子,你的“List DVD”和“List Bluray”功能可能真的是一个单一的功能:

        private void ListMediaByType(List<Media> data, Media.MediaType type)
        {
            foreach (Media media in data.Where(media => media.TypeOfMedia == type))
                Console.WriteLine(media.Title);
        }
    

    如您所见,这些技术简化了LOT的查询。 :)

答案 1 :(得分:0)

最简单(imho)使用的是Linq:

string director = "Alfred Hitchcock";
IEnumerable<DVD> dvds = DVDMovies.Where(dvd => dvd.Director == director);
IEnumerable<DVD> blueRays = BlurayMovies.Where(br => br.Director == director);
List<DVD> allMatches = new List<DVD>();
allMatches.AddRange(dvds.Concat(blueRays));