Fundamentals
Strings
Data Types
Logic
Maintainable version of FizzBuzz
public class FizzBuzz { public string GetOutput(int number) { var ret = string.Empty; if (number % 3 == 0) ret += "Fizz"; if (number % 5 == 0) ret += "Buzz"; if (ret.Length == 0) ret = number.ToString(); return ret; } }
- public class FizzBuzz
- {
- public string GetOutput(int number)
- {
bool fizz = number % 3 == 0;bool buzz = number % 5 == 0;return fizz && buzz ? "FizzBuzz" : fizz ? "Fizz" : buzz ? "Buzz" : number.ToString();- var ret = string.Empty;
- if (number % 3 == 0)
- ret += "Fizz";
- if (number % 5 == 0)
- ret += "Buzz";
- if (ret.Length == 0)
- ret = number.ToString();
- return ret;
- }
- }
namespace Solution { using NUnit.Framework; [TestFixture] public class SolutionTest { [Test] public void GetOutput_InputIs4_Return4() { var fizzbuzz = new FizzBuzz(); var result = fizzbuzz.GetOutput(4); Assert.That(result, Is.EqualTo("4")); } [Test] public void GetOutput_InputIsDivisibleBy3_ReturnFizz() { var fizzbuzz = new FizzBuzz(); var result = fizzbuzz.GetOutput(3); Assert.That(result, Is.EqualTo("Fizz")); } [Test] public void GetOutput_InputIsDivisibleBy5_ReturnBuzz() { var fizzbuzz = new FizzBuzz(); var result = fizzbuzz.GetOutput(5); Assert.That(result, Is.EqualTo("Buzz")); } [Test] public void GetOutput_InputIsDivisibleBy5And3_ReturnFizzBuzz() { var fizzbuzz = new FizzBuzz(); var result = fizzbuzz.GetOutput(15); Assert.That(result, Is.EqualTo("FizzBuzz")); } } }
- namespace Solution {
- using NUnit.Framework;
- [TestFixture]
- public class SolutionTest
- {
- [Test]
- public void GetOutput_InputIs4_Return4()
- {
- var fizzbuzz = new FizzBuzz();
- var result = fizzbuzz.GetOutput(4);
- Assert.That(result, Is.EqualTo("4"));
- }
- [Test]
- public void GetOutput_InputIsDivisibleBy3_ReturnFizz()
- {
- var fizzbuzz = new FizzBuzz();
- var result = fizzbuzz.GetOutput(3);
- Assert.That(result, Is.EqualTo("Fizz"));
- }
- [Test]
- public void GetOutput_InputIsDivisibleBy5_ReturnBuzz()
- {
- var fizzbuzz = new FizzBuzz();
- var result = fizzbuzz.GetOutput(5);
- Assert.That(result, Is.EqualTo("Buzz"));
- }
- [Test]
- public void GetOutput_InputIsDivisibleBy5And3_ReturnFizzBuzz()
- {
- var fizzbuzz = new FizzBuzz();
- var result = fizzbuzz.GetOutput(15);
- Assert.That(result, Is.EqualTo("FizzBuzz"));
- }
- }
- }