Bank Account (Design by Contract) Application

This application contains an Account class that implements:

  • Deposit(double amount) - increases the account’s balance with the amount;
    • Rules:
      • the amount should always be positive
  • Withdraw(double amount) - decreases the account’s balance;
    • Rules:
      • the amount should always be positive
      • if the amount exceeds the balance:
        • the balance should be left untouched
        • an exception should be thrown.
  • Instantiating a new Account class
    • Rules:
      • the start amount should always be positive
      • the name of the account should not be empty

Additionally, Program class makes use of Account.cs and it also demonstrates the use of Code Contracts that enforce Design by Contract principles.



Setting up

To be able to observe the defined Design by Contract principles you would need to install Visual studio 2015 .

As well as installing Code Contracts for .NET.

Once you are done with the above, in VS you would need to:

  • Go to Project > Properties > Code Contracts
    • tick Perform Static Contract Checking
    • untick Cache results
    • set Warning Level to high
    • Save

As shown in the picture below:

  • By Building - incorrect param values should get squiggly lines

  • Into Task List window you would be able to see the warnings that were generated



Example

Not obeying the positive amount rule when setting up a new Account

Having this line of code will give us the Warning ⚠️ that the amount we provide when setting up an account should have a positive value.
As well as:


Not obeying the positive amount rule when Depositing


The DbC rules we used can be seen 👀 in the Account.cs class.



Assignment made by:

David Alves 👨🏻‍💻 :octocat: Github
Elitsa Marinovska 👩🏻‍💻 :octocat: Github

Attending "Discrete Mathematics" course of Software Development bachelor's degree