I explained that, i want to enter each value twice, because I want to ensure that it was entered correctly both times. Like, when your asked to renter a new password, or email, that is why I want a check.
I accepted that already.
We are talking about ledger
should be considered as being local.
And:
- the return value in the case of no error
- calculating
check
isnât necessary, check what I provided:
if credit_amount-debit_amount != 0
return (false,"Error -- credit and debit must match.")
elseif ...
This is the way to do this.
You did: calculating check
at the beginning, and use it somewhere else. Thats not the way to do it. Itâs a minor point.
Is it something that executes ledger inside of the transaction function?
function ledgertransaction!(
ledger,
date,
credit_account,
credit_amount,
debit_account,
debit_amount )
ledger=DataFrame(
date=[],
chequing=[],
expenses=[],
income=[]);
end
function ledgertransaction(
date,
credit_account,
credit_amount,
debit_account,
debit_amount
)
ledgertransaction!(
date,
credit_account,
credit_amount,
debit_account,
debit_amount )
check=credit_amount-debit_amount
if length(date) == 0
return(false,"Error-- no date entered")
elseif !( credit_account in names(ledger) )
return (false, "Error -- credit account not found.")
elseif !( debit_account in names(ledger) )
return (false, "Error--debit account not found")
elseif check != 0
return (false,"Error -- credit and debit must match." )
else
return(true,"Journal Entry")
default_values=Dict("chequing"=>0,"expenses"=>0,"income"=>0)
spec = Dict(
"date" => date,
credit_account => credit_amount,
debit_account => debit_amount)
row = merge(default_values, spec)
push!(ledger,row)
end
end
Stay focused. You are jumping between the issues.
I decide, we focus now on the easiest: change your code according to:
- calculating
check
isnât necessary, check what I provided:if credit_amount-debit_amount != 0 return (false,"Error -- credit and debit must match.") elseif ...
This is the way to do this.
You did: calculatingcheck
at the beginning, and use it somewhere else. Thats not the way to do it. Itâs a minor point.
Any questions about this?
Ok, I did that because
if credit_amount != debit_amount
doesnât work
but
if credit_amount-debit_amount != 0
return (false,âError â credit and debit must match.â)
elseif âŚ
does
It should work:
if credit_amount != debit_amount
return (false,"Error -- credit and debit must match.")
elseif length(date) == 0
return (false,"Error -- no date entered")
elseif ...
Please check, it should work!
It does.
Great!
Next step:
- the return value in the case of no error
What do you return in case of no error? What should we return?
if length(date) == 0
return(false,"Error-- no date entered")
elseif !( credit_account in names(ledger) )
return (false, "Error -- credit account not found.")
elseif !( debit_account in names(ledger) )
return (false, "Error--debit account not found")
elseif credit_amount-debit_amount != 0
return (false,"Error -- credit and debit must match." )
else
return(true,journal_entry)
end
No, this is type unstable.
(And journal_entry
shouldnât be there at all! It is clear, that this is in your way to proceed. You try to solve problems before you have solved those you have before. Thatâs in your way, itâs blocking you. Remove journal_entry
completely from your code!)
In case of an error you return a value of type Tuple{Bool,String} . We need to stick with this, thats what we have to return in any case.
if length(date) == 0
return(false,"Error-- no date entered")
elseif !( credit_account in names(ledger) )
return (false, "Error -- credit account not found.")
elseif !( debit_account in names(ledger) )
return (false, "Error--debit account not found")
elseif credit_amount-debit_amount != 0
return (false,"Error -- credit and debit must match." )
else
return(true, "there are no errors")
end
Nice!
Let me see the whole function (no journal_entry
please )
function ledgertransaction(
date,
credit_account,
credit_amount,
debit_account,
debit_amount
)
if length(date) == 0
return(false,"Error-- no date entered")
elseif !( credit_account in names(ledger) )
return (false, "Error -- credit account not found.")
elseif !( debit_account in names(ledger) )
return (false, "Error--debit account not found")
elseif credit_amount-debit_amount != 0
return (false,"Error -- credit and debit must match." )
else
return(true, "there are no errors")
default_values=Dict("chequing"=>0,"expenses"=>0,"income"=>0)
spec = Dict(
"date" => date,
credit_account => credit_amount,
debit_account => debit_amount)
row = merge(default_values, spec)
push!(ledger,row)
)
end
end
This wouldnât work as you expect it.
Tell me, what do you think, the line with the return
s does?
What does the line
return(true, "there are no errors")
do?
returns a value from the function to the general program?
I think I need to go over how return works with functions.
you are right, and what about the code after it? Is it ever reached?
No the code ends. Do I move return after ?
function ledgertransaction(
date,
credit_account,
credit_amount,
debit_account,
debit_amount
)
if length(date) == 0
return(false,"Error-- no date entered")
elseif !( credit_account in names(ledger) )
return (false, "Error -- credit account not found.")
elseif !( debit_account in names(ledger) )
return (false, "Error--debit account not found")
elseif credit_amount-debit_amount != 0
return (false,"Error -- credit and debit must match." )
else
default_values=Dict("chequing"=>0,"expenses"=>0,"income"=>0)
spec = Dict(
"date" => date,
credit_account => credit_amount,
debit_account => debit_amount)
row = merge(default_values, spec)
push!(ledger,row)
return(true, "there are no errors")
end
end
Of course.
Whatâs this extra )
there in the line after the last return?
A mistake