Real Test Driven Development – Thinking of Money 15

next on the list  is to do the add transaction part of the update, this should simply be a case of passing the various form controls to the controller object, getting it to extract the various values and checking that we have a valid transaction, adding the transaction to the collection, and calling save.  I am hoping that because we have bound the collection to the grid, and hopefully we can insert the transaction at the beginning of the collection, that it will automatically display.

so first off we need a test.

        [Test]
        public void AddTransaction()
        {
            TextBox txtDescription = new TextBox();
            TextBox txtCategory = new TextBox();
            TextBox txtAmount = new TextBox();
            ComboBox cboType = new ComboBox();
            cboType.Items.Add("Addition");
            txtAmount.Text = "10.00";
            txtCategory.Text = "Unit Testing";
            txtDescription.Text = "Test";

            Assert.IsTrue(_Controller.UpdateTransaction(txtDescription, txtCategory, txtAmount, cboType));
        }

and this calls the controller code

    public bool UpdateTransaction(TextBox txtDescripiton, TextBox txtCategory, TextBox txtAmount, ComboBox cboType)
    {
        double amount = double.Parse(txtAmount.Text);
        if (amount==0)
            return false;

        string category = txtCategory.Text;
        string description = txtDescripiton.Text;

        txtAmount.Text = string.Empty;
        txtCategory.Text = string.Empty;
        txtDescripiton.Text = string.Empty;

        if (cboType.SelectedText == "Addition")
            return _Engine.AddTransaction(amount, category, description, System.DateTime.Now);
        else
            return _Engine.SubtractTransaction(amount, category, description, System.DateTime.Now);
    }

not the best bit of code at the moment, first we check that we have a valid amount, if not the routine exits, next we obtain the values in the text box’s, then clear them so when we return the form looks nice, lastly we simply add or delete the transaction.

it could do with some error returned, but currently we have no way of displaying this in the form. we can add it to the list of things to do in the next version, as I am sure there will be changes to the interface one users get their hands on it.

now we need to update the form so it calls this, I have refractored the controller so its now a module level variable and added a click event to the button as below

       private void btnUpdate_Click(object sender, EventArgs e)
       {
           _Controller.UpdateTransaction(txtDescription, txtCategory, txtAmount, cboType);
       }

and it didn’t work !

currently there is no update to the database when we hit update, so I need to add that functionality, the best place was in the add and subtract transaction, I only want to update when the transaction id is zero, ie its a new transaction so I added the following method to the money engine and added a call to the function in the add and subtract functions. notice that when the transaction ID is zero, a new transaction then we want to insert it at the top of the collection, if we are loading from the database then we want to add them in order, so its a simple add to list.

     private bool StoreTransaction(MoneyTransaction trans)
        {
            if (trans.TransactionID == 0)
            {
                _History.Insert(0, trans);
                return trans.Update();
            }
            else
            {
                _History.Add(trans);
                return true;
            }
        }

        private bool AddTransaction(int transID, double amount, string category, string description, 
DateTime transactionDate)
        {
            MoneyTransaction trans = new MoneyTransaction(transID, amount, category, description, 
transactionDate, TransactionType.AdditionTransaction);
            
            _Balance += trans.TransactionAmount;

            return StoreTransaction(trans);
        }

        private bool SubtractTransaction(int transID, double amount, string category, string description, 
DateTime transactionDate)
        {
            MoneyTransaction trans = new MoneyTransaction(transID, amount, category, description, 
transactionDate, TransactionType.SubtractionTransaction);
           
            _Balance -= trans.TransactionAmount;

            return StoreTransaction(trans);
        }

 ok so now it updates to the database so when we rerun the application, the transaction is there, so now we just need to get the grid to update, I was hoping that it would happen automatically, well actually it does but the grid has to be forced to update, that turns out that its as simple as calling refresh on the grid.

so Lastly for this release I need to check the grid for deletes.

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 Uncategorized. 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