Last active
October 14, 2021 19:15
-
-
Save steviegt6/7eccab758e1ade85c7bc561edf317076 to your computer and use it in GitHub Desktop.
FizzBuzzEnterpriseEdition-inspirted short .NET program displaying an over-engineered a FizzBuzz program. Made in a geography class because I was bored.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| public class Language | |
| { | |
| public Container<string> LanguageKey { get; } | |
| public Language(string languageKey) | |
| { | |
| LanguageKey = languageKey; | |
| } | |
| public string GetFizz() => Constants.Fizz; | |
| public string GetBuzz() => Constants.Buzz; | |
| public string GetFizzBuzz() => Constants.FizzBuzz; | |
| } | |
| public static class Localizer | |
| { | |
| public static Container<List<Language>> Languages { get; } | |
| public static Container<Language> SelectedLanguage { get; } | |
| static Localizer() | |
| { | |
| Languages = new List<Language>() | |
| { | |
| new Language(Constants.enUS) | |
| }; | |
| SelectedLanguage = Languages.Get().First(); | |
| } | |
| } | |
| public static class Constants | |
| { | |
| public const string F = "F"; | |
| public const string i = "i"; | |
| public const string z = "z"; | |
| public const string B = "B"; | |
| public const string u = "u"; | |
| public const string Fizz = F + i + z + z; | |
| public const string Buzz = B + u + z + z; | |
| public const string FizzBuzz = Fizz + Buzz; | |
| public const string enUS = "en-US"; | |
| } | |
| public class Container<TType> | |
| { | |
| private TType Instance { get; set; } | |
| public void Set(TType instance) => Instance = instance; | |
| public TType Get() => Instance; | |
| public override string ToString() => Get().ToString(); | |
| public static implicit operator Container<TType>(TType instance) | |
| { | |
| Container<TType> container = new Container<TType>(); | |
| container.Set(instance); | |
| return container; | |
| } | |
| public static implicit operator TType(Container<TType> instance) => instance.Get(); | |
| } | |
| public static class Program | |
| { | |
| public static void Main() | |
| { | |
| Container<int> i = 0; | |
| Container<int> cap = int.Parse(new UserInputRetriever().RetrieveInput().Get()) + 1; | |
| for (;i < cap;) | |
| { | |
| Container<FizzBuzzNumberBuilder> numB = new FizzBuzzNumberBuilder(); | |
| Container<FizzBuzzFizzBuilder> fizzB = new FizzBuzzFizzBuilder(); | |
| Container<FizzBuzzBuzzBuilder> buzzB = new FizzBuzzBuzzBuilder(); | |
| Container<StringBuilder> stringB = new StringBuilder(); | |
| numB.Get().Add(stringB, i); | |
| fizzB.Get().Add(stringB, i); | |
| buzzB.Get().Add(stringB, i); | |
| Container<string> output = stringB.ToString(); | |
| //if (output.Get().Contains('\n')) | |
| // output.Set(output.Get().Remove(output.Get().Length - 2, 2)); | |
| Console.WriteLine(output); | |
| i++; | |
| } | |
| } | |
| } | |
| public interface IFizzBuzzBuilder<TType> | |
| { | |
| bool Valid(TType value); | |
| void Add(StringBuilder builder, TType value); | |
| } | |
| public sealed class FizzBuzzNumberBuilder : IFizzBuzzBuilder<int> | |
| { | |
| public bool Valid(int type) => type.Equals(0) || (type % 5 != 0 && type % 3 != 0); | |
| public void Add(StringBuilder builder, int value) | |
| { | |
| if (Valid(value)) | |
| builder.Append(value.ToString()); | |
| } | |
| } | |
| public sealed class FizzBuzzFizzBuilder : IFizzBuzzBuilder<int> | |
| { | |
| public bool Valid(int type) => type % 3 == 0 && !type.Equals(0); | |
| public void Add(StringBuilder builder, int value) | |
| { | |
| if (Valid(value)) | |
| builder.Append(Localizer.SelectedLanguage.Get().GetFizz()); | |
| } | |
| } | |
| public sealed class FizzBuzzBuzzBuilder : IFizzBuzzBuilder<int> | |
| { | |
| public bool Valid(int type) => type % 5 == 0 && !type.Equals(0); | |
| public void Add(StringBuilder builder, int value) | |
| { | |
| if (Valid(value)) | |
| builder.Append(Localizer.SelectedLanguage.Get().GetBuzz()); | |
| } | |
| } | |
| public interface IInputRetriever<TType> | |
| { | |
| TType RetrieveInput(); | |
| } | |
| public class UserInputRetriever : IInputRetriever<Container<string>> | |
| { | |
| public Container<string> RetrieveInput() => Console.ReadLine(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment