比较和从文本文件合并字符串数组 [英] Comparing and combining arrays of strings from text files

查看:115
本文介绍了比较和从文本文件合并字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

右键关闭我不认为这个问题的措辞是准确的蝙蝠,我真的不知道该写什么。

Right off the bat I dont think the wording of the question is accurate, I just dont really know what to write.

话虽这么说,我有我加载到这个节目,花花公子,曲调,并带3 txt文件。花花公子的格式如下这个名字|乐器,曲调像这样; SONGNAME |作曲家|带| coverArtists1 | coverArtists2 |等。和band这样; bandName | bandType | member1中| member2 |等。该|是我分割数据,使文本文件的每一行成为字符串数组。

That being said, I have 3 txt files that I am loading into this program, Dudes, Tunes, and Bands. Dudes is formatted like this name|instrument, Tunes like this; songName|composer|band|coverArtists1|coverArtists2|etc. And band like this; bandName|bandType|member1|member2|etc. The "|" is where I split the data so each line of the text files become arrays of strings.

什么我想现在要做的就是当用户输入一个乐队的名字,它会返回乐队,它的类型,而列表中的每个乐队成员,他们演奏乐器的名称。这个过程依赖于输入什么类型的乐队。例如,一个类型的摇滚乐队需要一个吉他手,鼓手,贝司兼主唱。每种带的是它自己的类即频带的一个子类

What I am trying to do now is when the user inputs the name of a band, it will return the name of the band, its type, and the list each band member and the instrument they play. This process depends on what type of band is inputted. For example a type RockBand needs a guitarist, drummer, bass, and vocalist. Each type of band is its own class that is a subclass of band.

    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 (subclass of Band)

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];
        }
    }

花花公子类

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.");
            }
        }
    }

我知道这是一个很多code的什么林pretty肯定是一个简单的问题,但我很诚实很迷茫,不知道有这一部分开始。谢谢你在前进,我很高兴提供任何事情作出澄清。

I know this is a lot of code for what Im pretty sure is a simple problem but I am honestly very confused and dont know where to begin with this part. Thank you in advance, Im happy to provide clarification on anything.

推荐答案

首先试图给变量和方法有意义的名称。像 tunesRepository T1 而不是 GetMusician 而不是案例2

First of all try to give variables and methods meaningful names. Like tunesRepository instead of t1 or GetMusician instead of Case2.

好像你无法理解如何管理数据之间的关系。例如,带可如何引用音乐家,如果他们在不同的文件。一个简单的解决方法是给乐队类的<​​code>音乐家类的引用。在创建带区对象时,然后可以通过名字查找音乐家:

It seems like you're having trouble understanding how to manage relationships between data. For example, how Band can reference musicians if they're in separate files. A simple solution to this is to give the Bands class a reference to the Musician class. You can then lookup the musicians by name when creating the band objects:

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);
}

您需要更新乐队类位为上述构造的工作:

You'll need to update the Band class a bit for the above constructor to work:

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; }
}

然后,你需要在你的方法来初始化像这样:

private static MusicianDatabase musicianDatabase;
private static BandDatabase bandDatabase;

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

和要求时,你可以再打印细节:

And you can then print the details when requested:

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);
}

我希望这是多一点的感觉。我试着在你应该明白方面构建了一切。但是,如果仍然有易混淆的地方,你让我知道。

I hope this makes a bit more sense. I've tried to structure everything in terms that you should understand. But if there's still parts that are confusing you let me know.

这篇关于比较和从文本文件合并字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆