Link to home
Create AccountLog in
Avatar of Stacie
StacieFlag for United States of America

asked on

Code Explanation

I'm getting a little lost. I was wondering if someone can give me line by line explanation of the code bellow.


  class Pineapple
    {
        const string d = @"c:\chow\deliveryg.txt";
        public enum Fargo { North, South, East, West, Flamingo }
        public static void Main()
        {
            StreamWriter o = new StreamWriter(@"c:\chow\orderg.txt");
            Pizza pz = new Pizza(new StreamWriter(d, true));
            pz.Idaho(Fargo.Flamingo);
            for (int w = 3; w >= 0; w--)
            {
                Pizza i = new Pizza(new StreamWriter(d, false));
                i.Idaho((Fargo)w);
                Party p = new Party(new StreamReader(d));
                p.HowMuch(o);
            }
            o.WriteLine("That’s all folks!");
            o.Close();
        }
    }

    class Pizza
    {
        private StreamWriter writer;
        public Pizza(StreamWriter writer)
        {
            this.writer = writer;
        }
        public void Idaho(Pineapple.Fargo f)
        {
            writer.WriteLine(f);
            writer.Close();
        }
    }

    class Party
    {
        private StreamReader reader;
        public Party(StreamReader reader)
        {
            this.reader = reader;
        }
        public void HowMuch(StreamWriter q)
        {
            q.WriteLine(reader.ReadLine());
            reader.Close();
        }
    }

Open in new window

Avatar of piattnd
piattnd

I'm looking through it right now, but where did you get this code?  At first glance it looks really confusing for what it actually does....
it looks like its writing the pizza order to a text file (most likely printing a receipt for the order)
ASKER CERTIFIED SOLUTION
Avatar of plusone3055
plusone3055
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Because I have very limited knowledge of HOW this code is used, this is the best I could do for interpreting it.


class Pineapple
    {
        //set d to the path of a target text file
        const string d = @"c:\chow\deliveryg.txt";

        //establish an enumeration with values.  This will be called later by typing Fargo.Item
        public enum Fargo { North, South, East, West, Flamingo }

        //This is your main method of the pineapple class
        public static void Main()
        {
            //open a stream writer so you can write to the text file shown in the ""s
            StreamWriter o = new StreamWriter(@"c:\chow\orderg.txt");

            //Initiate a new instance of the Pizza class.  The pizza class requires a StreamWriter, so you pass it a new instance of a stream writer
            //You pass in the path stored in string 'd' and say true to inidicate overwriting is enabled.
            Pizza pz = new Pizza(new StreamWriter(d, true));

            //You call the Idaho method and pass the Fargo.Flamingo as the parameter.  You can see the Idaho method requires a Pineapple.Fargo reference when you call it.
            //Calling pz.Idaho will result in writing the value of the enum object Fargo.Flamingo to a text file established in your pz instance creation above
            pz.Idaho(Fargo.Flamingo);

            //Set w to a value of 3 and run through this code until w = 0.  Each time it runs through the code, w will be subtracted by 1, so it should run 3 times.
            for (int w = 3; w >= 0; w--)
            {
                //Initiate a new instance of the Pizza class and use the same path as before to write to, but disable overwriting.
                Pizza i = new Pizza(new StreamWriter(d, false));
                //Call the Idaho method inside the pizza class again and pass the Pineapple.Fargo value at a particular location using the number value of w.
                //I would assume the first pass through would use "west", the second would use "east" and third "south".
                i.Idaho((Fargo)w);
                //Initiate a new instance of the Party class and pass it a stream reader to read the file established in d.
                Party p = new Party(new StreamReader(d));
                //Execute the HowMuch method, which requires a stream writer.  You pass in streamwriter o that writes to the c:\chow\orderg.txt file.
                p.HowMuch(o);
            }
            //write this text to the C:\chow\orderg.txt file and close the file.
            o.WriteLine("That’s all folks!");
            o.Close();
        }
    }

    class Pizza
    {
        //establish writer as a streamwriter type for later use
        private StreamWriter writer;

        //Associate the writer with the Pizza class you initialize
        public Pizza(StreamWriter writer)
        {
            this.writer = writer;
        }

        //Idaho method that expects a Pineapple.Fargo (enumeration) and writes to the file as indicated when you initiated the Pizza class.  Closes the file.
        public void Idaho(Pineapple.Fargo f)
        {
            writer.WriteLine(f);
            writer.Close();
        }
    }

    class Party
    {
        //establish reader as a streamreader for later use   
        private StreamReader reader;

        //Associate the reader with the Party class you initialize
        public Party(StreamReader reader)
        {
            this.reader = reader;
        }

        //HowMuch method which expects a StreamWriter to be passed to it when you call it.
        public void HowMuch(StreamWriter q)
        {
            //write the value that was inside of the line read in the reader to the file designated in the call to this method and close the reader file.
            q.WriteLine(reader.ReadLine());
            reader.Close();
        }
    } 

Open in new window