Tim Holloway wrote:In practical terms, when you're working with a banking system it would be via doubl-entry accounting. A transfer would require 2 operations: 1) subtract amount to transfer from source. 2) add amount to transfer to destination.
To avoid losing (or doubling) money, these two operations would normally be part of a single Database Transaction.
So here's a pseudo-code you might use with Spring Data:
With Spring Data annotations, this could all be done automatically in a method marked @Transactional. and you wouldn't have to commit/rollback manually and the method would simply throw an InsufficientFundsException (which is a class that you subclass from Exception).
Campbell Ritchie wrote:Surely you would have a validation of the same rule for a withdrawal already.I hope you have been taught that it is error‑prone to use doubles for money because of their imprecision. It would be better to have the transfer as an instance method of an account.
Paul Clapham wrote:Wouldn't a transfer between two accounts require two queries, one for each account?
Campbell Ritchie wrote:Please start by explaining the rule in plain English, maybe, “The amount transferred must not be more than the balance of the originating account.”
Campbell Ritchie wrote:What would you query for the recipient account? Would you simply verify its existence, or would you require permission from the account holder?
Junilu Lacar wrote:Ok, I mustered up enough courage to wade into that huge lambda expression and here are my first impressions:
1. It does nothing observable. The lists created on lines 5 & 6 are local to the lambda
2. This code could probably benefit from some Rename, Extract, and Compose refactoring.
3. Since you're attempting to test something that doesn't appear to have any externally observable behavior, I doubt that you understand what this piece of code does either. A good understanding of what any piece of code does is an important pre-requisite that's critical to successfully testing it.
Campbell Ritchie wrote:Welcome to the Ranch
Please always tell us where such questions come from. Apart from the compiler error caused by missing quote marks, what does nda mean? Please avoid unexplained abbreviations. And what do you think the output would be when you correct the quotes? Would the code compile then?