-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexpensereport.exs
executable file
·89 lines (80 loc) · 2.77 KB
/
expensereport.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/env elixir
defmodule Expense do
defstruct [:type, :amount]
end
defmodule ExpenseReport do
def printReport(htmlMode, expenses) when is_list(expenses) do
if htmlMode do
IO.write("""
<!DOCTYPE html>
<html lang="en">
<head>
<title>Expense Report</title>
</head>
<body>
<h1>Expense Report</h1>
""")
else
IO.puts("Expense Report")
end
if htmlMode do
IO.write("""
<table>
<thead>
<tr><th scope="col">Type</th><th scope="col">Amount</th><th scope="col">Over Limit</th></tr>
</thead>
<tbody>
""")
end
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
if htmlMode do
IO.puts("<tr><td>" <> expenseName <> "</td><td>" <> to_string(expense.amount) <> "</td><td>" <> mealOverExpensesMarker <> "</td></tr>")
else
IO.puts(expenseName <> "\t" <> to_string(expense.amount) <> "\t" <> mealOverExpensesMarker)
end
end
if htmlMode do
IO.write("""
</tbody>
</table>
""")
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))
if htmlMode do
IO.puts("<p>Meal expenses: " <> to_string(mealExpenses) <> "</p>")
IO.puts("<p>Total expenses: " <> to_string(total) <> "</p>")
else
IO.puts("Meal expenses: " <> to_string(mealExpenses))
IO.puts("Total expenses: " <> to_string(total))
end
if htmlMode do
IO.write("""
</body>
</html>
""")
end
end
end
defmodule Main do
def main do
ExpenseReport.printReport(true, [
%Expense{type: :breakfast, amount: 1000},
%Expense{type: :breakfast, amount: 1001},
%Expense{type: :dinner, amount: 5000},
%Expense{type: :dinner, amount: 5001},
%Expense{type: :carrental, amount: 4},
])
end
end
Main.main