Skip to content

Instantly share code, notes, and snippets.

@refactorsaurusrex
Created December 12, 2015 03:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save refactorsaurusrex/78584d574b55d824e210 to your computer and use it in GitHub Desktop.
Save refactorsaurusrex/78584d574b55d824e210 to your computer and use it in GitHub Desktop.
C# Pie!
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