首页 > 解决方案 > 从一个位置访问代码中的潜在更改

问题描述

我是一名新程序员,学习 C# 作为爱好。我正在编写一个图书馆导航器,用户可以在其中按名称、流派、作者等搜索书籍。我正在接近这个项目的结尾,到目前为止,我已经解决了所有问题并自己解决了它们。但我现在被困住了,因此在 StackOverflow 上写作以寻求专家的帮助。我会尽可能清楚地解释我的问题。

我的节目目前有 4 种书籍类型:恐怖、小说、奇幻和神秘。

因为将来我可能会添加更多类型,所以我不想发现自己浏览数百行代码来编辑 if/case 语句并添加新类型。

有没有一种实用的方法可以将我将来可能编辑的所有信息集中到一个地方?

以下是我宣布的清单:

    public static List<string> bookList = new List<string> {"shindler's list", "it", "maltese falcon", "bfg", "hobbit"};
    public static List<string> fiction = new List<string> {"shindler's list"};
    public static List<string> fantasy = new List<string> {"bfg", "hobbit"};
    public static List<string> horror = new List<string> {"it"};
    public static List<string> mystery = new List<string> {"maltese falcon"};

现在,这些列表是从代码的各个部分调用的,例如:

case "1":
                Seperation();
                Console.Clear();

                Console.Write("Enter book name: ");
                var bookTitleUserInput = Console.ReadLine();

                Console.WriteLine("You searched for: " + bookTitleUserInput);

                foreach (string s in bookList)
                {
                    if (s.ToLowerInvariant() == bookTitleUserInput.ToLowerInvariant())
                    {
                        Console.WriteLine("\n");
                        Seperation();
                        OutputBookSummary(bookTitleUserInput.ToLowerInvariant());
                        Seperation();
                        if (fiction.Contains(bookTitleUserInput.ToLowerInvariant()))
                        {
                            Console.WriteLine("Genre: Fiction");
                            Console.WriteLine("\n");
                        }
                        else if (horror.Contains(bookTitleUserInput.ToLowerInvariant()))
                        {
                            Console.WriteLine("Genre: Horror");
                            Console.WriteLine("\n");
                        }
                        else if (mystery.Contains(bookTitleUserInput.ToLowerInvariant()))
                        {
                            Console.WriteLine("Genre: Mystery");
                            Console.WriteLine("\n");
                        }
                        else if (fantasy.Contains(bookTitleUserInput.ToLowerInvariant()))
                        {
                            Console.WriteLine("Genre: Fantasy");
                            Console.WriteLine("\n");
                        }
                        else
                        {
                            Console.WriteLine("");
                        }

                        break;
                    }
                }

和像这样的代码:

                case "4":
                Console.Clear();
                Console.WriteLine("Settings Menu");
                Seperation();
                Console.WriteLine("1) Search By Genre");
                Console.WriteLine("2) Search By Author");
                Console.WriteLine("3) Search By Book Name");
                Console.WriteLine("4) Back to Main Menu");
                Seperation();

                Console.Write("Choice: ");
                var settingsMenuSelection = Console.ReadLine();

                switch (settingsMenuSelection)
                {
                    case "1":
                        Console.WriteLine("1) Horror");
                        Console.WriteLine("2) Mystery");
                        Console.WriteLine("3) Fiction");
                        Console.WriteLine("4) Fantasy");
                        var genreMenuSelection = Console.ReadLine();

                        break;

所以像这样,如果我必须编辑代码以添加更多类型,我将不得不滚动浏览我的代码并单独编辑它们。更不用说这会造成的混乱和混乱..

我曾尝试将 if 语句块case 1:放在单独的方法中,但它不起作用。我还尝试将所有内容放在另一个类中case 1:case 4:从那里调用块,但这也没有用。

通过不工作,我Genre: 的意思是 + 流派没有在控制台上输出。

那么,简而言之,有没有办法集中所有上述代码,所以如果出现新书/流派/作者,我就不必导航了?因为我认为那将是不好的做法。

在此先感谢,很抱歉发了这么长的帖子!

标签: c#liststatic-methodscentralized

解决方案


典型的解决方案是使用面向对象将一本书与其类型相关联。像这样的东西

public enum Genre
{
    Horror, Fiction, Fantasy, Mystery
}

public class Book
{
    public string Title { get; }
    public Genre Genre { get; }
    public Book(string title, Genre genre) => (Title, Genre) = (title, genre);
}

您可以按类型过滤书籍或按标题搜索,如下所示:

        var books = new[]
        {
            new Book("bfg", Genre.Fiction),
            new Book("it", Genre.Horror),
        };
        var horrorBooks = books.Where(b => b.Genre == Genre.Horror);
        var foundByname = books.FirstOrDefault(b => b.Title.Contains("MySearchTerm"));

将来,您可能希望扩展书籍以拥有类型列表而不是单个类型。您还可以继续添加作者、ISBN 等属性。可以将书籍列表序列化为 json 文件进行存储,或者使用实体框架之类的东西将书籍存储在数据库中。确切的方法取决于您对系统的要求。我鼓励您阅读一些有关数据库的文章,以更深入地了解该主题,尤其是数据库规范化。


推荐阅读