Commit a7cacb3

mokhan <mokhan@ce5e1baf-6525-42e4-a1b2-857ea38da20a>
2009-03-16 19:10:07
added a couple of observations around percentages.
git-svn-id: https://svn.xp-dev.com/svn/mokhan-mo.money@81 ce5e1baf-6525-42e4-a1b2-857ea38da20a
1 parent c720e3f
Changed files (4)
trunk
product
trunk/product/MyMoney/Domain/Core/Percent.cs
@@ -1,30 +1,34 @@
+using System;
+using System.Globalization;
+
 namespace MoMoney.Domain.Core
 {
     public class Percent
     {
-        readonly long whole;
-        readonly long fraction;
+        readonly double percentage;
 
-        public Percent(long whole) : this(whole, 0)
+        public Percent(double percentage)
         {
+            this.percentage = percentage;
         }
 
-        public Percent(long whole, int fraction)
+        public Percent(double portion_of_total, double total)
         {
-            this.whole = whole;
-            this.fraction = fraction;
+            percentage = portion_of_total/total;
+            percentage *= 100;
+            percentage = Math.Round(percentage, 1);
         }
 
-        static public implicit operator Percent(int whole)
+        static public implicit operator Percent(double percentage)
         {
-            return new Percent(whole);
+            return new Percent(percentage);
         }
 
         public bool Equals(Percent other)
         {
             if (ReferenceEquals(null, other)) return false;
             if (ReferenceEquals(this, other)) return true;
-            return other.whole == whole && other.fraction == fraction;
+            return other.percentage == percentage;
         }
 
         public override bool Equals(object obj)
@@ -37,10 +41,12 @@ namespace MoMoney.Domain.Core
 
         public override int GetHashCode()
         {
-            unchecked
-            {
-                return (whole.GetHashCode()*397) ^ fraction.GetHashCode();
-            }
+            return percentage.GetHashCode();
+        }
+
+        public override string ToString()
+        {
+            return percentage.ToString(CultureInfo.InvariantCulture);
         }
     }
 }
\ No newline at end of file
trunk/product/MyMoney/Domain/Core/PercentSpecs.cs
@@ -0,0 +1,16 @@
+using developwithpassion.bdd.contexts;
+using MoMoney.Testing.spechelpers.contexts;
+using MoMoney.Testing.spechelpers.core;
+
+namespace MoMoney.Domain.Core
+{
+    public class when_comparing_fifty_divided_by_one_hundred_to_fifty_percent : concerns
+    {
+        it they_should_be_equal = () => new Percent(50, 100).should_be_equal_to<Percent>(50);
+    }
+
+    public class when_calculating_a_fractional_percentage : concerns
+    {
+        it should_return_the_correct_percentage = () => new Percent(30, 90).should_be_equal_to<Percent>(33.3);
+    }
+}
\ No newline at end of file
trunk/product/MyMoney/Tasks/infrastructure/UpdateTasks.cs
@@ -48,7 +48,7 @@ namespace MoMoney.Tasks.infrastructure
 
         public void grab_the_latest_version(ICallback<Percent> callback)
         {
-            deployment.UpdateProgressChanged += (o, e) => callback.complete(new Percent(e.BytesCompleted/e.BytesTotal));
+            deployment.UpdateProgressChanged += (o, e) => callback.complete(new Percent(e.BytesCompleted, e.BytesTotal));
             deployment.UpdateCompleted += (sender, args) => callback.complete(new Percent(100));
             deployment.UpdateAsync();
         }
trunk/product/MyMoney/MyMoney.csproj
@@ -190,6 +190,7 @@
     <Compile Include="Domain\Core\MoneyExtensionsSpecs.cs" />
     <Compile Include="Domain\Core\MoneySpecs.cs" />
     <Compile Include="Domain\Core\Percent.cs" />
+    <Compile Include="Domain\Core\PercentSpecs.cs" />
     <Compile Include="Domain\Core\range.cs" />
     <Compile Include="Domain\Core\range_specs.cs" />
     <Compile Include="Domain\repositories\IBillRepository.cs" />