-2

我有一个存储 dvd 和蓝光的 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);
    }
}

}

4

2 回答 2

1

我认为你的设计有缺陷。

DVD 和 BluRay 类真的应该是:

  1. 具有Type属性的单个类,可能是包含 DVD 和 BluRay 的枚举。这样,几年后,一旦您获得新媒体,您只需向 Enum 添加一个值,您的应用程序就会保持最新。
  2. 实现自定义接口的两个不同的类,您可以调用它们,例如IMedia.

另外,我强烈建议您利用ListC# 中的对象而不是数组。它非常快,您可以轻松添加/删除项目,而无需调整数组的大小。

这是关于 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);
    }

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

于 2012-12-07T22:49:48.293 回答
0

最简单的(恕我直言)是使用 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));
于 2012-12-07T22:49:25.547 回答