I want to develop a java application where a user should be able to add calculation items depending on each other. Consider this scenario:
- Add an item (A) with a value lets say 50
- Add another item (B) which is item A*5
- Add item (C) which has a value of (A*50%)+B
- Delete item (A) fails as long it is used in other items.
I can store the equations as varchar in the database and parse it back in java when I recall using equation parser or something similar. But when I tried this, the problem was what If item A is deleted? The database wont know if the item is linked to another item if I use a varchar. I want the items to be linked to each other using foreign keys, how can I achieve that?
Sorry if this is a duplicate question, but I could not find a proper answer that solves my problems.
You can use the tree java structure to represent the expression. each node will represent an operator and 2 operands. The concrete values will be at the leaf level.
The calculation would be a simple in order walk on the tree.
In the DB you will need a representation of the expression (tree) pointing to all its components (tree nodes).
(Or use alternative expression tree DB representation)
the DB
expression entries table:
nodes table: