I don't have any sample code but I do have pointers.
I have found when ever I embarked on writing process code its best to map out the process in a flowchart/decision tree diagram before writing a stitch of code.
You start with inputs and outputs add decision points and branches of logic.
At...