Real Test Driven Development – Thinking of Money 3

 

We now have an object to store each transaction, this will also allow us to display each transaction, so next we have to do the add, and then subtract, these should also update the balance. 

Once we have done that we can think about accessing the history stuff.

right so my methods will want to create a money transaction and complete its details, then update the balance, my initial thoughts where that I would have an add, and then subtract function, in the engine class I have created already with that in mind I started writing an add transaction function, the test would be simply to call this with the correct parameters, and then I would check the balance to ensure it was updating correctly.

        public bool AddTransaction(double amount, string category, string description, DateTime transactionDate)
        {
            MoneyTransaction trans = new MoneyTransaction();

            trans.TransactionAmount = amount;
            trans.TransactionCategory = category;
            trans.TransactionDescription = description;
            trans.TransType = TransactionType.AdditionTransaction;
            trans.TransactionDate = transactionDate;

            _Balance += trans.TransactionAmount;
        }
and the subtraction method
        public bool SubtractTransaction(double amount, string category, string description, 
                                        DateTime transactionDate)
        {
            MoneyTransaction trans = new MoneyTransaction();

            trans.TransactionAmount = amount;
            trans.TransactionCategory = category;
            trans.TransactionDescription = description;
            trans.TransType = TransactionType.SubtractionTransaction;
            trans.TransactionDate = transactionDate;

            _Balance -= trans.TransactionAmount;
        }
now I need a couple of unit tests to ensure I am not going completely mad, this is one of those times that I have
done the coding before the tests, I couldn't decide if I wanted one or two methods, finally deciding on two so
any future enchantments could be added without overhead, keeping it simple sometimes a bit more code at the start.
ok the tests, added to the engine test class
        [Test]
        public void AddTransaction()
        {
            _Engine.AddTransaction(10.10, "test category", "test description", DateTime.Now);

            Assert.AreEqual(10.10, _Engine.Balance);
        }

        [Test]
        public void SubtractTransaction()
        {
            _Engine.SubtractTransaction(10, "test category", "test description", DateTime.Now);

            Assert.AreEqual(-10, _Engine.Balance);
        }

Well that failed on the build completely, teach me to program within live writer, after some changes the two
engine methods now look like.
      public bool AddTransaction(double amount, string category, string description, DateTime transactionDate)
        {
            MoneyTransaction trans = new MoneyTransaction(amount, category, description, transactionDate, 
                                                          TransactionType.AdditionTransaction);

            _Balance += trans.TransactionAmount;

            return true;
        }

        public bool SubtractTransaction(double amount, string category, string description, 
                                        DateTime transactionDate)
        {
            MoneyTransaction trans = new MoneyTransaction(amount, category, description, transactionDate, 
                                                          TransactionType.SubtractionTransaction);

            _Balance -= trans.TransactionAmount;

            return true;
        }

ok so now the project builds and the balance is updated as expected. apart from the small error above, where I got carried away creating the transaction object then filling it in one property at a time instead of using the constructor as the object required, would writing a test before had solved that, probably not, but I always feel safe as I won’t write more than a couple of functions like the above without tests, ensuring that I have a green before proceeding.

Also notice we have no save or display methods its all been putting stuff in and then getting stuff out, next we tackle the history problem, we want to store each transaction then get it back in order, by date?

Advertisements

About Duncan Butler

Trying to be a very agile software developer, working in C# with Specflow, Nunit and Machine Specifications, and in the evening having fun with Ruby and Rails
This entry was posted in Projects. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s