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.
That logic doesn’t look like how I think it should be. For example, changing code logic to pseudocode, as a hypothetical example it’s like
// start values
wallet = 5
earnings = 10
amount = 7
if wallet value < amount value
// 5 is less than 7
empty wallet to zero
// wallet now = 0, earnings = 10, amount = 7
deduct amount value from earnings value
// walllet = 0, earnings now = 3
UPDATE db table to earnings = 3
I can’t speak for others, but I would be less than pleased at having my wallet emptied because it had insufficient funds to cover the amount.
I don’t want to necessarily empty wallet to zero, I want to take what’s left in wallet (which isn’t enough for the current purchase) and use it for the purchase, along with what ever is needed from earnings, to complete the purchase, or (if that combination is not enough for the purchase, then: ‘not enough money’ message. (If the wallet gets emptied to zero in that process, that’s okay)
I know that coming up with naming that is self-evident is not always so easy. In this case I’m afraid that I don’t understand what your variables represent as well as you do. Nor do I understand exactly what you want to happen under what conditions. It may be that similar to some utilities you want to “charge a late fee” or similar to some banks “charge a bounced check fee”. Again, you would best know what you want to happen when.
The variable names I used in post # 21 may not exactly correspond to your variables, but hopefully the pseudocode logic is close to what you have in mind.
Thanks again for your reply.
This is how I would write it:
I want to take what’s left in wallet (which isn’t enough for the current purchase) and use it for the purchase, along with what ever is needed from earnings, to complete the purchase, or (if that combination is not enough for the purchase, then: ‘not enough money’ message
Thanks. I find it easier to be broken down into list form
I want to take what’s left in wallet
(which isn’t enough for the current purchase) and
use it for the purchase,
along with what ever is needed from earnings,
to complete the purchase,
or (if that combination is not enough for the purchase,
then: ‘not enough money’ message
Then change each to rough pseudocode that’s between “human” and “code”
// I want to take what’s left in wallet
$current_wallet_amount
// (which isn’t enough for the current purchase) and
$current_wallet_amount < $current_purchase_amount
// use it for the purchase,
$unpaid_purchase_amount = $current_purchase_amount - $current_wallet_amount
$current_wallet_amount = 0
// along with what ever is needed from earnings,
$current_earnings - $unpaid_purchase_amount
// to complete the purchase,
$unpaid_purchase_amount = 0
// or (if that combination is not enough for the purchase,
if ($current_wallet_amount + $current_earnings) < $current_purchase_amount
// then: ‘not enough money’ message
display message
Then, if that all looks good (I don’t think it does, even though it is close), change to code. i.e. the “which isn’t” becomes an if (condition) { etc.
Except i’d rearrange it slightly for streamlining the ifs, by using some mathematical logic.
If I have enough combined cash to make the transaction:
Subtract the whole amount from the primary source of funds.
If the primary source is now negative:
Add the primary source (which is a negative number...) to the secondary source.
Set the primary source to 0.
Endif
Do any other 'you paid for it' logic.
Else
display not enough cash.
Endif
You dont need to calculate the unpaid amount separately - it’s simply the negative value of the primary payment type.
You’re guaranteed not to have a negative value in the secondary source because you’ve already checked that the combined funds are sufficient for the purchase.
The if after the else doesn’t seem necessary. Unless you want it there for readability it can be assumed that when $wallet is not greater than or equal to $amount it would be less than $amount.
The $wallet - $amount line performs an operation but I don’t see where it’s doing anything like changing or assigning the value to a variable.
The $earnings section looks to be OK as long as $earnings is enough to add the negative $wallet to it. I think it would be a good idea to wrap that section of code in if conditionals to make sure that’s true.