1. dfjones
  2. Let's Play TDD .NET

Commits

dfjones  committed 5016b79

End of episode 83

  • Participants
  • Parent commits ffd0c66
  • Branches default

Comments (0)

Files changed (4)

File Finances.Domain.Tests/TestDollars.cs

View file
         }
 
         [Test]
+        public void Parse()
+        {
+            Assert.AreEqual(new Dollars(0), Dollars.Parse(""), "empty string");
+            Assert.AreEqual(new Dollars(42), Dollars.Parse("42"), "just a number");
+            Assert.AreEqual(new Dollars(42), Dollars.Parse("$42"), "beginning dollar sign");
+            Assert.AreEqual(new Dollars(0), Dollars.Parse("$"), "dollar sign only");
+            Assert.AreEqual(new Dollars(42.13), Dollars.Parse("42.13"), "decimals");
+            Assert.AreEqual(new Dollars(1234), Dollars.Parse("1,234"), "one comma");
+            Assert.AreEqual(new Dollars(1234567), Dollars.Parse("1,234,567"), "several commas");
+            Assert.AreEqual(new Dollars(42), Dollars.Parse(",,,4,,,,,,2,,,"), "dusfunctional commas");
+
+        }
+
+        [Test]
         public void ToStringIgnoresPennies()
         {
             Assert.AreEqual("$10", new Dollars(10.10).ToString(), "should round down");
         }
 
         [Test]
+        public void ToStringFormatsNegativeNumbersWithParentheses()
+        {
+            Assert.AreEqual("($500)", new Dollars(-500).ToString());
+        }
+
+        [Test]
         public void ToStringFormatsInTheUsaStyleEvenWhenInDifferenceLocales()
         {
             var defaultCulture = System.Threading.Thread.CurrentThread.CurrentCulture;

File Finances.Domain/Dollars.cs

View file
     {
         private readonly double amount;
 
+        public static Dollars Parse(string text)
+        {
+            if (text.StartsWith("$")) text = text.Substring(1);
+            if (string.IsNullOrEmpty(text)) return new Dollars(0);
+            if (text.Equals("-")) return new Dollars(0);
+            text = text.Replace(",", "");
+
+            return new Dollars(Double.Parse(text));
+        }
+
         public Dollars(int amount)
         {
             this.amount = amount;
             return new Dollars(Math.Min(value1.amount, value2.amount));
         }
 
+        private Boolean IsNegative()
+        {
+            return amount < 0;
+        }
+
         private long RoundOffPennies()
         {
             return (long)Math.Round(amount, MidpointRounding.AwayFromZero);
 
         public override String ToString()
         {
-            string formattedNumber = RoundOffPennies().ToString("C", new CultureInfo("en-US"));
+            if (IsNegative())
+                return "(" + ConvertNumberToString() + ")";
+            else
+            {
+                return ConvertNumberToString();
+            }
+            
+        }
+
+        private string ConvertNumberToString()
+        {
+            long roundedAmount = RoundOffPennies();
+            roundedAmount = Math.Abs(roundedAmount);
+            string formattedNumber = roundedAmount.ToString("C", new CultureInfo("en-US"));
             return formattedNumber.Remove(formattedNumber.Length - 3, 3);
         }
 

File Finances.UI.Tests/TestDollarsTextField.cs

View file
-using System.Windows.Forms;
-using Finances.Domain;
+using Finances.Domain;
 using NUnit.Framework;
 
 namespace Finances.UI.Tests
         }
 
         [Test]
-        public void TextMayStartWithDollarSign()
-        {
-            field.Text = "$42";
-            Assert.AreEqual(new Dollars(42), field.GetDollars());
-
-            field.Text = "$";
-            Assert.AreEqual(new Dollars(0), field.GetDollars());
-        }
-
-        [Test]
-        public void EmptyStringIsZeroDollars()
-        {
-            field.Text = "";
-            Assert.AreEqual(new Dollars(0), field.GetDollars());
-        }
-
-        [Test]
-        public void DecimalsWork()
-        {
-            field.Text = "1.23";
-            Assert.AreEqual(new Dollars(1.23), field.GetDollars());
-        }
-
-        [Test]
         public void CommasAreOkay()
         {
             field.Text = "1,234";

File Finances.UI/DollarsTextField.cs

View file
 
         public Dollars GetDollars()
         {
-            string text = Text;
-            if (text.StartsWith("$")) text = text.Substring(1);
-            if (string.IsNullOrEmpty(text)) return new Dollars(0);
-            if (text == "-") return new Dollars(0);
-            text = text.Replace(",", "");
-
-            return new Dollars(double.Parse(text));
-
+            return Dollars.Parse(Text);
         }
     }
 }