Please forgive me if I’m wrong, but I have the feeling that part of the problem is because what I would consider as the “first steps” has been skipped making code logic more difficult to understand. Not that how I work up code is how it needs to be done, only that it is what works best for me. eg.
On scratch paper or in a “pre-alpha” (almost always subject to revision) file.
I list variables that I think will be needed (likely not what you have, only for an example). eg.
float wallet - existing "cash" on hand float earnings - additional "cash" float item_cost - how much something costs float additional_cost - shipping & handling, taxes, commission etc. float total_assets - equal to wallet + earnings float credit - any discount float total_expense - equal to (item_cost + additional_cost) - credit
Then I list all the different scenarios I can think of so I can write pseudocode
determine total_expense if not enough "cash" in total_assets exit code if enough "cash" available in assets if enough "cash" in wallet, deduct total_expense from wallet if not enough, zero wallet and deduct remainder of total_expense from earnings calculate remaining "cash" and assign to wallet, earnings and/or total_assets ?
In my experience I almost always need to have more variables and conditional logic than I first think of, but the better I am at thinking things through to begin with the easier go I have at getting to code I’m happy with.
Once I have the pseudocode figured out it’s simply a matter of changing the human readable “if, then do” into actual code.