Creating an Accounting Program in Julia, for Pluto

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: calculating check at the beginning, and use it somewhere else. Thats not the way to do it. It’s a minor point.

Any questions about this?

1 Like

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 :wink: )

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 returns 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