Created
December 12, 2015 03:31
-
-
Save refactorsaurusrex/78584d574b55d824e210 to your computer and use it in GitHub Desktop.
C# Pie!
This file contains 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; | |
using System.Collections.Generic; | |
using System.Linq; | |
namespace Confections | |
{ | |
public class Pie<T> : IReadOnlyCollection<Slice<T>> | |
{ | |
readonly List<Slice<T>> slices; | |
public Pie(IEnumerable<Slice<T>> slices) | |
{ | |
this.slices = slices.ToList(); | |
if (Math.Abs(this.slices.Sum(s => s.Percent) - 100) > 0.1) | |
throw new ArgumentOutOfRangeException("Slices must add up to 100%"); | |
} | |
public IEnumerator<Slice<T>> GetEnumerator() | |
{ | |
return slices.GetEnumerator(); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return GetEnumerator(); | |
} | |
public int Count | |
{ | |
get { return slices.Count; } | |
} | |
} | |
public class Slice<T> | |
{ | |
public Slice(double percent, string name) | |
{ | |
if (percent > 100.0 || percent < 1.0) | |
throw new ArgumentOutOfRangeException("Size must be between 1 and 100."); | |
Percent = percent; | |
Name = name; | |
} | |
public double Percent { get; private set; } | |
public string Name { get; private set; } | |
public List<T> Filling { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment