lundi 4 mai 2015

C# Class Libraries and how to use them, need help on this

Okay, here's the challenge:

1) Print out the numbers from 1 to 100. If the number is divisible by 3, print "Fizz" instead. If by 5, print "Buzz." If by both, print "FizzBuzz."

2) Modify your program so that you have a program and a reusable library for rendering FizzBuzz.

3) Modify your library to support an arbitrary numeric range.

4) Extend your solution to include tests to demonstrate the correctness of your algorithm.

I'm having a problem getting #2, 3, and 4 to work.

This is what I have so far:

class Program

{
    static void Main(string[] args)
    {
        FizzBuzz(15, 25, "Fizz", "Buzz");
        Console.WriteLine(FizzBuzz);
        //Set the size of your data. In this case, it is 100.
       // int x = 1; (Represented by int i = 1)
        int y = 100;
        //Variable to represent the strings
        string fizz = "Fizz";
        string buzz = "Buzz";
        //StringBuilder to stringify the data, including numbers
        StringBuilder myStringer = new StringBuilder();

        //The for loop will scroll through the data. "Y" is the size of the data set.
        for (int i = 1; i <= y; i++)
        {   //The if statement creates conditions to look for while the data is being anlyzed, and can execute specific code
            //when specific conditions have been met.
            if (i % 3 == 0 && i % 5 == 0)
            { //For example, when the number represented by "i" can be divided by 3 and 5 with no remainder, then format 
              //everything into a string.
                myStringer.Append(string.Format("\n{0} {1}", fizz, buzz));
            }
            else if (i % 3 == 0)
            {
                myStringer.Append(string.Format("\n{0}", fizz));
            }
            else if (i % 5 == 0)
            {
                myStringer.Append(string.Format("\n{0}", buzz));
            }
            else
            {
                myStringer.Append(string.Format("\n{0}", i));
            }
        }
        Console.WriteLine(myStringer.ToString());
        Console.ReadLine();
    }

    private static void FizzBuzz(int p1, int p2, string p3, string p4)
    {
        throw new NotImplementedException();
    }

}
}

As far as my class library goes, this is what I put in it:

namespace FizzBuzzLibrary

{
public class FizzBuzz

{
    public int x { get; set; }
    public int y { get; set; }
    public string fizz { get; set; }
    public string buzz { get; set; }
    public FizzBuzz() {x = 0; y = 0; fizz = "Fizz"; buzz = "Buzz";}

  public FizzBuzz(int x, int y, string fizz, string buzz)
    {
        this.x = x;
        this.y = y;
        this.fizz = (string.Compare(fizz, "", true) == 0 ? this.fizz : fizz);
        this.buzz = (string.Compare(buzz, "", true) == 0 ? this.buzz : buzz);
        if (x > y) throw new Exception("invalid range"); 
        Process(x);
    }

    public void Process(int i) {
        StringBuilder myStringer4 = new StringBuilder();
        try {
            if (i% 3 == 0 && i % 5 == 0)
                myStringer4.Append(string.Format("\n{0} {1} {2}", i, fizz, buzz));
            else if (i % 3 == 0)
                myStringer4.Append(string.Format("\n{0} {1}", i, fizz));
            else if (i % 5 == 0)
                myStringer4.Append(string.Format("\n{0} {1}", i, buzz));
            else
                myStringer4.Append(string.Format("\n{0}", i));
            if (i == y) return;
            else Process(++i);
        }
        catch (Exception e) { throw new Exception(e.ToString()); }
    }

    public void Ots() {
        StringBuilder myStringer5 = new StringBuilder();
        Console.WriteLine(myStringer5.ToString());
        Console.ReadLine();
    }
}

}

Aucun commentaire:

Enregistrer un commentaire