-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathExpenseReport.lua
83 lines (75 loc) · 2.52 KB
/
ExpenseReport.lua
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
ExpenseType = {
DINNER = 1,
BREAKFAST = 2,
CAR_RENTAL = 3,
}
Expense = { type, amount }
function Expense:new(type, amount)
o = {}
setmetatable(o, self)
self.__index = self
o.type = type
o.amount = amount
return o
end
function printReport(expenses, htmlMode)
mealExpenses = 0
total = 0
if htmlMode then
print("<!DOCTYPE html>")
print("<html>")
print("<head>")
print("<title>Expense Report: " .. os.date() .. "</title>")
print("</head>")
print("<body>")
print("<h1>Expense Report: " .. os.date() .. "</h1>")
else
print("Expenses: " .. os.date())
end
if htmlMode then
print("<table>")
print("<thead>")
print("<tr><th scope=\"col\">Type</th><th scope=\"col\">Amount</th><th scope=\"col\">Over Limit</th></tr>")
print("</thead>")
print("<tbody>")
end
for i, expense in ipairs(expenses) do
if expense.type == ExpenseType.DINNER or expense.type == ExpenseType.BREAKFAST then
mealExpenses = mealExpenses + expense.amount
end
expenseName = ""
if expense.type == ExpenseType.DINNER then expenseName = "Dinner" end
if expense.type == ExpenseType.BREAKFAST then expenseName = "Breakfast" end
if expense.type == ExpenseType.CAR_RENTAL then expenseName = "Car Rental" end
mealOverExpensesMarker = (expense.type == ExpenseType.DINNER and expense.amount > 5000 or expense.type == ExpenseType.BREAKFAST and expense.amount > 1000) and "X" or " "
if htmlMode then
print("<tr><td>" .. expenseName .. "</td><td>" .. expense.amount .. "</td><td>" .. mealOverExpensesMarker .. "</td></tr>")
else
print(expenseName .. "\t" .. expense.amount .. "\t" .. mealOverExpensesMarker)
end
total = total + expense.amount
end
if htmlMode then
print("</tbody>")
print("</table>")
end
if htmlMode then
print("<p>Meal Expenses: " .. mealExpenses .. "</p>")
print("<p>Total Expenses: " .. total .. "</p>")
else
print("Meal Expenses: " .. mealExpenses)
print("Total Expenses: " .. total)
end
if htmlMode then
print("</body>")
print("</html>")
end
end
expenses = {
Expense:new(ExpenseType.DINNER, 5000),
Expense:new(ExpenseType.DINNER, 5001),
Expense:new(ExpenseType.BREAKFAST, 1000),
Expense:new(ExpenseType.BREAKFAST, 1001),
Expense:new(ExpenseType.CAR_RENTAL, 4),
}
printReport(expenses, true)