# Creating an Accounting Program in Julia, for Pluto

**URL:** <https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227>\
**Category:** Finance and Economics\
**Tags:** question, dataframes, function, pluto\
**Created:** [June 1, 2021, 8:38pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227 "2021-06-01T20:38:12Z")\
**Posts on this page:** 20\
**Page:** 3

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 2, 2021, 8:02pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/41 "2021-06-02T20:02:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 2, 2021, 8:07pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/42 "2021-06-02T20:07:25Z")

</div>

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:

```julia
	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.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 2, 2021, 8:08pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/43 "2021-06-02T20:08:09Z")

</div>

Is it something that executes ledger inside of the transaction function?

```julia
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
```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 2, 2021, 8:11pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/44 "2021-06-02T20:11:23Z")

</div>

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:
> 
> ```julia
> 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?

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 2, 2021, 8:26pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/45 "2021-06-02T20:26:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 2, 2021, 8:28pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/46 "2021-06-02T20:28:47Z")

</div>

It should work:

```julia
           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!

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 2, 2021, 8:29pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/47 "2021-06-02T20:29:59Z")

</div>

It does.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 2, 2021, 8:31pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/48 "2021-06-02T20:31:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 2, 2021, 8:33pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/49 "2021-06-02T20:33:55Z")

</div>

```julia
	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
```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 2, 2021, 8:37pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/50 "2021-06-02T20:37:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 2, 2021, 8:40pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/51 "2021-06-02T20:40:48Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 2, 2021, 8:44pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/52 "2021-06-02T20:44:46Z")

</div>

Nice!  
Let me see the whole function (no `journal_entry` please 😉 )

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 2, 2021, 8:47pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/53 "2021-06-02T20:47:30Z")

</div>

```julia
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

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 2, 2021, 8:49pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/54 "2021-06-02T20:49:26Z")

</div>

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

```julia
       return(true, "there are no errors")

```

do?

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 2, 2021, 9:12pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/55 "2021-06-02T21:12:40Z")

</div>

returns a value from the function to the general program?

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 2, 2021, 9:24pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/56 "2021-06-02T21:24:30Z")

</div>

I think I need to go over how return works with functions.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 2, 2021, 9:28pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/57 "2021-06-02T21:28:19Z")

</div>

you are right, and what about the code after it? Is it ever reached?

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 2, 2021, 9:30pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/58 "2021-06-02T21:30:49Z")

</div>

No the code ends. Do I move return after ?

```julia
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
```

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [June 2, 2021, 9:55pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/59 "2021-06-02T21:55:24Z")

</div>

Of course.  
What’s this extra `)` there in the line after the last return?

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [June 2, 2021, 10:00pm UTC](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227/60 "2021-06-02T22:00:37Z")

</div>

A mistake

[Previous page](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227.md?page=2)

[Next page](https://discourse.julialang.org/t/creating-an-accounting-program-in-julia-for-pluto/62227.md?page=4)
