Commit d4a9b16

mo k <mo@mokhan.ca>
2012-04-20 20:44:54
create range to iterate through a period of months.
1 parent 99fcf30
src/domain/GasPlant.cs
@@ -3,6 +3,7 @@ namespace domain
   using System;
   using System.Linq;
   using System.Collections.Generic;
+  using utility;
 
   public class GasPlant
   {
@@ -37,7 +38,25 @@ namespace domain
 
     public IEnumerable<Month> MonthsOverAvailableCapacity(IRange<Month> months)
     {
-      return Enumerable.Empty<Month>();
+      var results = new List<Month>();
+      months.Accept(month =>
+      {
+        var production = TotalProductionFor(month);
+        Console.Out.WriteLine(production);
+        if( capacity.For(month).IsGreaterThan(production) ){
+          results.Add(month);
+        }
+      });
+      return results;
+    }
+    IQuantity TotalProductionFor(Month month)
+    {
+      IQuantity result = new Quantity(0, new BOED());
+      wells.Each(x =>
+      {
+        result = result.Plus( x.GrossProductionFor<Gas>(month));
+      });
+      return result;
     }
   }
 
@@ -60,15 +79,21 @@ namespace domain
       this.increases.Add(new Increase(quantity, month));
     }
 
+    public IQuantity For(Month month)
+    {
+      return null;
+    }
+
     class Increase
     {
+      IQuantity quantity;
+      Month month;
+
       public Increase(IQuantity quantity, Month month)
       {
+        this.quantity = quantity;
+        this.month = month;
       }
     }
   }
-
-  public interface IRange<T> where T : IComparable<T>
-  {
-  }
 }
src/domain/Month.cs
@@ -2,7 +2,7 @@ namespace domain
 {
   using System;
 
-  public class Month : IComparable<Month>
+  public class Month : IComparable<Month>, IIncrementable<Month>
   {
     int year;
     int month;
@@ -15,8 +15,17 @@ namespace domain
 
     public Month Plus(int months)
     {
-      var newMonth = new DateTime(year, month, 01).AddMonths(months);
-      return new Month(newMonth.Year, newMonth.Month);
+      var newMonth = ToDateTime().AddMonths(months);
+      return ToMonth(newMonth);
+    }
+
+    DateTime ToDateTime()
+    {
+      return new DateTime(year, month, 01);
+    }
+    Month ToMonth(DateTime date)
+    {
+      return new Month(date.Year, date.Month);
     }
 
     static public Month Now()
@@ -30,6 +39,11 @@ namespace domain
       return new DateTime(year, month, 01).CompareTo(new DateTime(other.year, other.month, 01));
     }
 
+    public Month Next()
+    {
+      return Plus(1);
+    }
+
     public bool Equals(Month other)
     {
       if (ReferenceEquals(null, other)) return false;
@@ -58,6 +72,7 @@ namespace domain
       return string.Format("{0} {1}", year, month);
     }
   }
+
   public static class Months
   {
     public static Month January(this int year)
src/domain/Quantity.cs
@@ -4,6 +4,7 @@ namespace domain
   {
     IQuantity Plus(IQuantity other);
     IQuantity ConvertTo(IUnitOfMeasure units);
+    bool IsGreaterThan(IQuantity other);
     decimal Amount { get; }
     IUnitOfMeasure Units { get; }
   }
@@ -30,6 +31,11 @@ namespace domain
       return new Quantity(unitOfMeasure.Convert(Amount, this.Units), unitOfMeasure);
     }
 
+    public bool IsGreaterThan(IQuantity other)
+    {
+      return true;
+    }
+
     public override string ToString()
     {
       return string.Format("{0} {1}", Amount, Units);
src/domain/Range.cs
@@ -4,9 +4,42 @@ namespace domain
 
   public static class Ranges
   {
-    public static IRange<T> UpTo<T>(this T start, T end) where T: IComparable<T>
+    public static IRange<T> UpTo<T>(this T start, T end) where T: IComparable<T>, IIncrementable<T>
     {
-      return null;
+      return new Range<T>(start, end);
     }
   }
+
+  public interface IRange<T> where T : IComparable<T>
+  {
+    void Accept(Action<T> action);
+  }
+
+  public class Range<T> : IRange<T> where T : IComparable<T>, IIncrementable<T>
+  {
+    T start;
+    T end;
+
+    public Range(T start, T end)
+    {
+      this.start = start;
+      this.end = end;
+    }
+
+    public void Accept(Action<T> visitor)
+    {
+      var next = this.start;
+      var last = this.end.Next();
+      while(!next.Equals(last))
+      {
+        visitor(next);
+        next = next.Next();
+      }
+    }
+  }
+
+  public interface IIncrementable<T>
+  {
+    T Next();
+  }
 }
src/test/RangeSpecs.cs
@@ -0,0 +1,34 @@
+namespace test
+{
+  using System.Collections.Generic;
+  using Machine.Specifications;
+  using domain;
+
+  public class RangeSpecs
+  {
+    Establish context = () =>
+    {
+      sut = new Range<Month>(new Month(2012, 01), new Month(2012, 03));
+    };
+
+    static IRange<Month> sut;
+
+    public class when_iterating_through_each_item_in_a_range
+    {
+      It should_visit_each_item_in_the_range =()=>
+      {
+        results.ShouldContainOnly(new Month(2012, 01),new Month(2012, 02),new Month(2012, 03));
+      };
+
+      Because of = () =>
+      {
+        sut.Accept(x =>
+        {
+          results.Add(x);
+        });
+      };
+
+      static IList<Month> results = new List<Month>();
+    }
+  }
+}
src/test/test.csproj
@@ -50,9 +50,10 @@
     <Compile Include="GreetingSpecs.cs" />
     <Compile Include="CalculatorSpecs.cs" />
     <Compile Include="GasPlantSpecs.cs" />
+    <Compile Include="Mock.cs" />
     <Compile Include="MonthSpecs.cs" />
     <Compile Include="ProductionScheduleSpecs.cs" />
-    <Compile Include="Mock.cs" />
+    <Compile Include="RangeSpecs.cs" />
   </ItemGroup>
   <ItemGroup>
     <None Include="packages.config" />
continuous_testing
@@ -83,12 +83,15 @@ def notify(build_message)
   #args.each {|key,value| command_line += " /#{key}:\"#{value}\""}
   #`#{command_line} "#{build_message.message}"`
 
+  begin
   GNTP.notify({
     :app_name => 'c# practice',
     :title => build_message.successful ? 'PASS' : 'FAIL',
     :text => build_message.message,
     :icon => build_message.successful ? 'green.jpg' : 'red.jpg'
   })
+  rescue StandardError
+  end
 end
 
 def monitor