比较和组合文本文件中的字符串数组

时间:2015-03-18 22:45:19

标签: c# arrays parent-child streamreader

马上我不认为问题的措辞是准确的,我只是不知道该写些什么。

话虽这么说,我有3个txt文件,我正在加载到这个程序,Dudes,Tunes和Bands。 Dudes的格式就像这个名字|乐器,Tunes就是这样; SONGNAME |作曲家|带| coverArtists1 | coverArtists2 |等。像这样的乐队; bandName | bandType | member1中| member2 |等。 " |"是我分割数据的地方,因此文本文件的每一行都成为字符串数组。

我现在要做的是当用户输入乐队的名称时,它将返回乐队的名称,类型以及每个乐队成员和他们演奏乐器的列表。该过程取决于输入的带的类型。例如,RockBand类型需要吉他手,鼓手,贝司和歌手。每种类型的波段都是它自己的类,它是波段的子类。

    class Program
    {
       static Tunes t1 = new Tunes();
       static Dudes d1 = new Dudes();
       static Bands b1 = new Bands();


        static void Main(string[] args)
        {

            do
            {

                Console.WriteLine();


            } while (DoAQuery() != "0");

        }

        static string DoAQuery()
        {
            string prompts = "0: Quit \n" +
                             "1: Who wrote <song name> \n" +
                             "2: What does <musician name> play \n" +
                             "3: What songs were written by <composer> \n" +
                             "4: Who plays in the <band name> \n" +
                             "5: Who's recorded <song name> \n" +
                             "6: What songs has the <band name> recorded \n" +
                             "7: Has the <band name> recorded <song name> \n";

            Console.WriteLine(prompts);

            Console.Write("Enter a command number: ");
            string cmd = Console.ReadLine();


            switch (cmd)
            {
                case "0" :
                    return cmd;

                case "1" :
                    Case1();
                    return cmd;

                case "2" :
                    Case2();
                    return cmd;

                case "3":
                    Case3();
                    return cmd;

                case "4":
                    Case4();
                    return cmd;

                case "5":
                    Case5();
                    return cmd;

                case "6":
                    Case6();
                    return cmd;

                case "7":
                    Case7();
                    return cmd;

                default:
                    Console.WriteLine("!!Command must be a number 0-7!!");
                    return "1";
            }


        }

        static void Case1()
        {
            Console.Write("Enter a song name: ");
            string songName = Console.ReadLine();
            t1.Case1(songName);
        }

        static void Case2()
        {
            Console.Write("Enter a musician's name: ");
            string musName = Console.ReadLine();
            d1.Case2(musName);
        }

        static void Case3()
        {
            Console.Write("Enter a composers name: ");
            string compName = Console.ReadLine();
            t1.Case3(compName);


        }

        static void Case4()
        {
            Console.Write("Enter a band name: ");
            string bandName = Console.ReadLine();
            b1.Case4(bandName);

        }

乐队班级

class Band
    {
        protected Tunes t1 = new Tunes();
        protected Dudes d1 = new Dudes();

        protected string name;
        protected string type;
        protected List<Tune> recordings = new List<Tune>();

        public string Name
        {
            get { return name; }
        }

        public List<Tune> Recordings
        {
            get { return recordings; }
        }

        public string Type
        {
            get { return type; }
        }

        public Band(string[] lineAra)
        {
            name = lineAra[0];
            type = lineAra[1];
            //recordings = t1.for4(name);
        }

    }

乐队班

class Bands
    {
        private List<Band> bands = new List<Band>();
        private Dictionary<string, Band> bandsByName = new Dictionary<string, Band>();

        public Bands()
        {
            string fileName = @"C:\Users\Lkvideorang\Documents\Visual Studio 2013\Projects\KernRadio\KernRadio\bin\Debug\bands.txt";

            try
            {
                using (StreamReader myRdr = new StreamReader(fileName))
                {
                    string line;
                    while ((line = myRdr.ReadLine()) != null)
                    {
                        string[] lineAra = line.Split('|');

                        switch(lineAra[1])
                        {

                            case "RockBand":
                                {
                                    RockBand newBand = new RockBand(lineAra);
                                    bands.Add(newBand);
                                    bandsByName.Add(newBand.Name, newBand);
                                    break;
                                }
                            case "JazzCombo":
                                {
                                    JazzCombo newBand = new JazzCombo(lineAra);
                                    bands.Add(newBand);
                                    bandsByName.Add(newBand.Name, newBand);
                                    break;
                                }
                            case "SoloAct":
                                {
                                    SoloAct newBand = new SoloAct(lineAra);
                                    bands.Add(newBand);
                                    bandsByName.Add(newBand.Name, newBand);
                                    break;
                                }
                            default : 
                                {
                                     Band newBand = new Band(lineAra);
                                     bands.Add(newBand);
                                     bandsByName.Add(newBand.Name, newBand);
                                     break;
                                }

                        }
                        //Band newBand = new Band(lineAra);
                        //bands.Add(newBand);
                        //bandsByName.Add(newBand.Name, newBand);

                    }
                }
                Console.WriteLine("loaded " + bands.Count + " bands");
            }
            catch
            {
                Console.WriteLine("Error reading file! Read " + bands.Count + " tunes.");
            }
        }


        public void Case4(string bandName)
        {
            if (bandsByName.ContainsKey(bandName))
            {
                Console.WriteLine(bandsByName[bandName].Name + " is a " + bandsByName[bandName].Type);
                Console.WriteLine();
            }
            else
            {
                Console.WriteLine("No band with that name found.");
            }
        }
    }

RockBand(乐队的子类)

class RockBand : Band
    {


        private Musician vocalist;
        private Musician bass;
        private Musician drums;
        private Musician guitar;


        public RockBand (string[] lineAra) : base (lineAra)
        {

        //I would assign values to four members here

        }
    }

音乐家班

    class Musician
    {
        string name;
        string instrument;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Instrument
        {
            get { return instrument; }
            set { instrument = value; }
        }

       public Musician(string [] lineAra)
        {
            name = lineAra[0];
            instrument = lineAra[1];
        }
    }

Dudes班

class Dudes
    {
        static List<Musician> dudes = new List<Musician>();
        Dictionary<string, Musician> dudesByName = new Dictionary<string, Musician>();

        public Dudes()
        {
            string fileName = @"C:\Users\Lkvideorang\Documents\Visual Studio 2013\Projects\KernRadio\KernRadio\bin\Debug\dudes.txt";

            try
            {
                using (StreamReader myRdr = new StreamReader(fileName))
                {
                    string line;
                    while ((line = myRdr.ReadLine()) != null)
                    {
                        string[] lineAra = line.Split('|');
                        Musician newDude = new Musician(lineAra);
                        dudes.Add(newDude);
                        dudesByName.Add(newDude.Name, newDude);

                    }
                }
                Console.WriteLine("loaded " + dudes.Count + " dudes");
            }
            catch
            {
                Console.WriteLine("Error reading file! Read " + dudes.Count + " tunes.");
            }
        }

        public void Case2(string musName)
        {

            if (dudesByName.ContainsKey(musName))
            {
                Console.WriteLine(musName + " plays " + dudesByName[musName].Instrument);
            }
            else
            {
                Console.WriteLine("No musician with that name found.");
            }
        }
    }

我知道这是很多代码,我确信这是一个简单的问题,但老实说我很困惑,不知道从哪一步开始。提前谢谢你,我很乐意澄清任何事情。

2 个答案:

答案 0 :(得分:0)

您是否有理由每次都要加载文件,而不是将信息存储在简单的数据库中?如果您将数据存储在一个简单的数据库中,您可以快速轻松地以较低的开销返回信息。

你甚至可以使用像实体框架这样的东西来提供与数据表匹配的对象。

答案 1 :(得分:0)

首先尝试给变量和方法赋予有意义的名称。与tunesRepository代替t1GetMusician代替Case2

您似乎无法理解如何管理数据之间的关系。例如,如果乐队在单独的文件中,他们如何引用音乐家。对此的一个简单解决方案是为Bands类提供对Musician类的引用。然后,您可以在创建乐队对象时按名称查找音乐家:

public BandDatabase(MusicianDatabase musicianDatabase)
{
    this.musicianDatabase = musicianDatabase;

    string fileName = @"C:\Code\Sandbox\ConsoleApplication1\input.txt";

    string[] allLines;

    try
    {
        allLines = File.ReadAllLines(fileName);
    }
    catch (Exception ex)
    {
        Console.WriteLine("Error reading file! Exception: " + ex.Message);
        return;
    }

    bands = new List<Band>();

    foreach (var line in allLines)
    {
        try
        {
            string[] lineAra = line.Split('|');

            if (lineAra.Length < 2) continue;

            switch (lineAra[1])
            {
                case "RockBand":
                    bands.Add(CreateRockBand(lineAra));
                    break;
                // Rest of cases
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error parsing line {0}. Exception: {1}", line, ex.Message);
        }
    }

    bandsByName = bands.ToList().ToDictionary(x => x.Name, x => x);

    Console.WriteLine("loaded " + bands.Count + " bands");
}

private RockBand CreateRockBand(string[] lineAra)
{
    Musician vocalist = null;
    Musician bass = null;
    Musician drums = null;
    Musician guitar = null;

    if (lineAra.Length >= 3) 
        vocalist = musicianDatabase.GetMusicianByName(lineAra[2]);

    if (lineAra.Length >= 4)
        bass = musicianDatabase.GetMusicianByName(lineAra[3]);

    if (lineAra.Length >= 5)
        drums = musicianDatabase.GetMusicianByName(lineAra[4]);

    if (lineAra.Length >= 6)
        guitar = musicianDatabase.GetMusicianByName(lineAra[5]);

    return new RockBand(lineAra, vocalist, bass, drums, guitar);
}

您需要稍微更新Band类才能使上述构造函数正常工作:

public class RockBand : Band
{
    public RockBand(string[] lineAra, Musician vocalist, Musician bass, Musician drums, Musician guitar)
        : base(lineAra)
    {
        Vocalist = vocalist;
        BassPlayer = bass;
        Drummer = drums;
        GuitarPlayer = guitar;
    }

    public Musician Vocalist { get; set; }

    private Musician BassPlayer { get; set; }

    private Musician Drummer { get; set; }

    private Musician GuitarPlayer { get; set; }
}

然后您需要在Main方法中初始化,如下所示:

private static MusicianDatabase musicianDatabase;
private static BandDatabase bandDatabase;

static void Main(string[] args)
{
    musicianDatabase = new MusicianDatabase();
    bandDatabase = new BandDatabase(musicianDatabase);
}

然后,您可以在要求时打印详细信息:

static void PrintBandDetails()
{
    Console.Write("Enter a band name: ");
    string bandName = Console.ReadLine();

    var band = bandDatabase.GetBand(bandName);

    if (band == null)
    {
        Console.WriteLine("Invalid band name")
        return;
    }

    Console.WriteLine("Guitarist was " + band.GuitarPlayer);
    // etc.

    var tunes = tuneDatabase.GetByBand(bandName);

    Console.WriteLine("Tunes:");

    foreach(var t in tunes)
        Console.WriteLine(t);
}

我希望这更有意义。我试图以你应该理解的方式构建一切。但是,如果仍有令人困惑的部分,请告诉我。