I'm a self-taught (on the job) programmer, always looking for ways to expand my skills. Seeing all the FizzBuzz examples with hard-coded logic checks made my head hurt, so I thought I'd try my hand at it. Leaning heavily on KISS, I'm ignoring exception handling and such, and all that's required to add new fizzbuzzers is a call to Catalog.Add(Divisor,Output).
I'd appreciate your feedback, whether on style or substance. This is part of my journey to adopting SOLID principles for better OO development, but I felt that not much could be applied here without over-complicating it.
using System; using System.Collections.Generic; public class Program { public static void Main() { var catalog = new Catalog(); catalog.Add(Divisor: 3, Output: "Fizz"); catalog.Add(Divisor: 5, Output: "Buzz"); catalog.Add(Divisor: 10, Output: "Pozz"); var counter = new Counter(Min: 1, Max: 100, Catalog: catalog); counter.Output(); Console.ReadLine(); } } internal class Counter { private int min; private int max; private Catalog catalog; internal Counter(int Min, int Max, Catalog Catalog) { this.min = Min; this.max = Max; this.catalog = Catalog; } internal void Output() { for(int i = min; i <= max; i++) { Console.WriteLine(catalog.ToString(i)); } } } internal class Catalog { private List<Spec> specs; internal class Spec { internal int divisor; internal string output; } internal Catalog() { this.specs = new List<Spec>(); } internal void Add(int Divisor, string Output) { this.specs.Add(new Spec() { divisor = Divisor, output = Output }); } internal string ToString(int Number) { string outputstring = ""; foreach (var x in specs) { outputstring += Number % x.divisor == 0 ? x.output : ""; } return String.IsNullOrWhiteSpace(outputstring) ? Number.ToString() : outputstring; } }