- By Emmanuel Valverde
- ·
- Posted Jan 21, 2023 9:00:00 AM
ATM machine
Iteration 1 Business rules We want to build an ATM machine and the first thing we need to do, is to create the software that will breakdown which..
This is the third stop on the Test-Driven Development learning track. By now, you should be reasonably familiar with the concepts of "red, green, refactor" and "baby steps". If not, check out the earlier katas on this track: FizzBuzz and Leap Year.
This kata gives further practice at designing software feature-by-feature - an important part of Agile development processes - using TDD. Each time you add a new feature, you'll need to adapt your algorithm by refactoring to allow it to support the new behaviour.
The rules of this kata get steadily more complex. It is recommended that you approach this by adding one rule at a time. Try not to look ahead: imagine that the rules had arrived in separate briefings spread over a six-month project!
Create a simple String calculator with a single method:
class StringCalculator {
int Add(string numbers);
}
The method can take 1 or 2 comma-separated numbers, and will return their sum.
The method returns 0 when passed the empty string.
Example:
Add("") // 0
Add("4") // 4
Add("1,2") // 3
Start with the simplest test case of an empty string and move to 1 and two numbers.
Allow the Add method to handle an unknown amount of numbers.
Example:
Add("1,2,3,4,5,6,7,8,9") // 45
Allow the Add method to recognise newlines as well as commas as separators. The two separator types can be used interchangeably.
NB: Focus on the happy path - since this is not production code, it's fine if the code crashes if it's given invalid input (e.g. "1,\n2"
).
Example:
Add("1\n2,3") // 6
Optionally support custom separators. To change separator, the beginning of the string will contain a separate line that looks like this: “//<separator>\n<numbers>”
Example:
Add("//;\n1;2") // 3
Note that all existing scenarios should still be supported.
Calling Add with a negative number will throw an exception negatives not allowed
, and the negative that was passed.
If there are multiple negatives, show all of them in the exception message.
Example:
Add("1,-2,-3") // error: negatives not allowed: -2 -3
Numbers bigger than 1000 should be ignored.
Example:
Add("1001, 2") // 2
Separators can be of any length if surrounded by square brackets.
Example:
Add("//[***]\n1***2***3") // 6
Allow multiple single-character separators like this: “//[delim1][delim2]\n”
Example:
Add("//[*][%]\n1*2%3") // 6
Handle multiple separators with any character length.
Example:
Add("//[foo][bar]\n1foo2bar3") // 6
Credit: Roy Osherove
If you add a new test and then realise that you're going to need a big code change to make it pass, it's OK to back out the test (either by deleting it or marking it as ignored), and refactor your code until it's ready for the new test and functionality.
In fact, this is quite a powerful way of working: just be careful not to inadvertently add any new features while refactoring.
Iteration 1 Business rules We want to build an ATM machine and the first thing we need to do, is to create the software that will breakdown which..
In computer science, a stack is a famous abstract data type that provides certain operations on a collection of elements. Stacks have a long history,..
There is a shortage of Christmas trees this year, however, you can help! In the absence of real trees, Santa is going to teach the children of the..
Join our newsletter for expert tips and inspirational case studies
Join our newsletter for expert tips and inspirational case studies