Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Rule to validate a transfer between bank accounts?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i'm a Java beginner and currently using a rule in the transfer method that executes a query in the repository, however I am not comfortable mixing query in the code and I would like to know if there is another way to validate what I am doing without using a query. Follow:



My class of service where the transfer method is implemented:

Note: I would like the transfer rule to be validated here.



I don't know how to create this rule so far.

Thanks in advance.
 
Marshal
Posts: 70727
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please start by explaining the rule in plain English, maybe, “The amount transferred must not be more than the balance of the originating account.”
 
Marshal
Posts: 25971
70
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouldn't a transfer between two accounts require two queries, one for each account?
 
Campbell Ritchie
Marshal
Posts: 70727
288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What would you query for the recipient account? Would you simply verify its existence, or would you require permission from the account holder?
 
renan freth
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?




Just check its existence.
 
renan freth
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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



Exact.
Sorry for the ignorance because I'm using the google translator.
 
renan freth
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Wouldn't a transfer between two accounts require two queries, one for each account?




Yes. How would I do this query on my service transfer method?
As I mentioned, I don't feel safe using @Query
 
renan freth
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another point I would like to mention is:

 
Campbell Ritchie
Marshal
Posts: 70727
288
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 22679
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
renan freth
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.



Interesting. Thank you.
 
renan freth
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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



I am using MongoDB as the database. I don't think I have the ability to use Spring Data JPA annotations.
 
Tim Holloway
Saloon Keeper
Posts: 22679
153
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I'd use MongoDB for a bank account database, but regardless, Spring Data is not just for SQL DBMS systems.

Spring Data also has modules for graph databases like Neo4J and NoSQL DBMS's like MongoDB.
 
Grow your own food... or this tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic