The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Creating custom formula to generate pay

I have a table which stores Allowance/Deductions codes and their standard amounts. The structure looks somewhat like this:

ADCode      ADAmount
---------------------
GIS          100
DPF          150
DPF.ADV      200
HRA          100

I want to give the user a capability to create any formula using column values and there by create a new ADCode depending upon other ADCodes, example:

New_ADCode_1 = (GIS + DPF + DPF.ADV)/20 * 100

New_ADCode_2 = 3 * (HRA)

Any idea on how to substitute values in the formula of the ADCodes that are present and create a new ADCode that uses this formula every time an employees pay is calculated.

I am using SQL Server 2005 with C#.
RK Send private email
Saturday, August 25, 2007
 
 
Well, one way would be to create a set of tables whose entries could be combined to represent a mathematical formula. I only suggest this as an example of what NOT to do: entries in this table will be impossible to read or edit and will only discourage people from using the system.

The correct solution is to write a simple expression evaluator in whatever language you have to hand (you mentioned C#, which should do just fine). Better yet, find an expression evaluator that has already been written for you. You will need to be able poke the guts of the evaluator to provide variable values in the expressions (in your case, given a variable name, you need to be able to look up the name in your database and return the evaluated value).

Once you have the expression evaluator component, you just put a text field in your table that will hold an expression to be evaluated for the value of the table entry. The text field is easy to read and edit, so nobody gets frustrated, and the evaluator component is much faster than a monstrous database table and interpretation system.

Writing a simple experssion evaluator is pretty easy, just use a recursive descent parser (look it up on google or wikipedia).  I would be very surprised, however, if there weren't a few expression evaluators for C# already floating around out there.
Jeffrey Dutky Send private email
Saturday, August 25, 2007
 
 
Antlr should work just fine for you for parsing the formulas and getting their values back, and it's got a few arithmetic parser grammars written already.
Miki Watts Send private email
Saturday, August 25, 2007
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz