Skip to content

Commit

Permalink
Add ExpenseReport in Elixir.
Browse files Browse the repository at this point in the history
  • Loading branch information
christianhujer committed Dec 19, 2021
1 parent 8cc271f commit bcdc9ba
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ The ExpenseReport example currently exists in the following languages:
- [COBOL](expensereport-cobol/)
- [D](expensereport-d/)
- [Dart](expensereport-dart/)
- [Elixir](expensereport-elixir/)
- [F#](expensereport-fsharp/)
- [Fortran](expensereport-fortran/)
- [Go](expensereport-go/)
Expand Down Expand Up @@ -64,7 +65,6 @@ The ExpenseReport example currently exists in the following languages:
(in no particular order and with no guarantee)

- Eiffel
- Elixir
- Elm
- Erlang
- Logo
Expand Down
41 changes: 41 additions & 0 deletions expensereport-elixir/expensereport.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env elixir

defmodule Expense do
defstruct [:type, :amount]
end

defmodule ExpenseReport do
def printReport(expenses) when is_list(expenses) do
IO.puts("Expense Report")

for expense <- expenses do
expenseName = case expense.type do
:breakfast -> "Breakfast"
:dinner -> "Dinner"
:carrental -> "Car Rental"
end
mealOverExpensesMarker = case expense.type do
:breakfast -> if expense.amount > 1000 do "X" else " " end
:dinner -> if expense.amount > 5000 do "X" else " " end
_ -> " "
end
IO.puts(expenseName <> "\t" <> to_string(expense.amount) <> "\t" <> mealOverExpensesMarker)
end

mealExpenses = Enum.sum(Enum.map(Enum.filter(expenses, fn expense -> expense.type == :breakfast || expense.type == :dinner end), fn expense -> expense.amount end))
total = Enum.sum(Enum.map(expenses, fn expense -> expense.amount end))

IO.puts("Meal expenses: " <> to_string(mealExpenses))
IO.puts("Total expenses: " <> to_string(total))
end
end

defmodule Main do
def main do
ExpenseReport.printReport([
%Expense{type: :breakfast, amount: 50},
])
end
end

Main.main

0 comments on commit bcdc9ba

Please sign in to comment.