简体   繁体   中英

Comparing and combining arrays of strings from text files

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

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

        }

Band class

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

    }

bands class

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

        }
    }

Musician Class

    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

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

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.

is there a reason why you want to load the files every time, rather than storing the information in a simple database ? If you stored the data in a simple database you could quickly and easily return the information with a lower overhead.

You could even use something like entity framework which would provide objects that match the data tables.

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

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

Then you would need to initialize in your Main method like so:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM