Change Kata (OOP)

_config.yml

Another Kata. Here are the rules:

“The coin change kata should give us the minimum number of coins for a given value of cents. For example, if I had to receive 26 cents from a cashier, I would expect to be given 1 quarter and 1 cent - and not 5 nickels. Link to USA coin denominations

See if you can make any sense of it:

Here is the code:

But one question is moot: it is clear that the coin itself (i.e. the code in the denomination_range_equivalent method) could be extracted out as a separate class which is chosen using a factory method - but is there are real need for that? Probably best to keep it simple right now and leave the “denomination_range_equivalent” method in the Currency class. If you want to see other examples of where I go all the way and apply a polymorphic approach and a factory method, please check out the following katas:

They quite elegantly show how you would use polymorphism to supply behaviour rather than directly using a conditional.

Other than that, it is looking exactly like all the other katas I’ve attempted so far. They’re all the same. A loop plus polymorphism, plus a factory method. But I decided not to extract the final class in this particular case. There’s probably no need to, unless required.

And here are my tests:

Any questions, please feel free to ask.

Written on April 19, 2017