-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathExpenseReport.rb
executable file
·85 lines (78 loc) · 1.8 KB
/
ExpenseReport.rb
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
#!/usr/bin/ruby
class Expense
attr_reader :type, :amount
def initialize(type, amount)
@type = type
@amount = amount
end
end
def printReport(html_mode, *expenses)
total = 0
mealExpenses = 0
if html_mode
puts <<END
<!DOCTYPE html>
<html lang="en">
<head>
<title>Expense Report #{Time.now}</title>
</head>
<body>
<h1>Expense Report #{Time.now}</h1>
END
else
puts "Expenses: #{Time.now}"
end
if html_mode
puts <<END
<table>
<thead>
<tr><th scope="col">Tyep</th><th scope="col">Amount</th><th scope="col">Over Limit</th></tr>
</thead>
<tbody>
END
end
for expense in expenses
if expense.type == :dinner || expense.type == :breakfast
mealExpenses += expense.amount
end
expenseName = ""
case expense.type
when :breakfast
expenseName = "Breakfast"
when :dinner
expenseName = "Dinner"
when :car_rental
expenseName = "Car Rental"
end
mealOverExpensesMarker = expense.type == :dinner && expense.amount > 5000 || expense.type == :breakfast && expense.amount > 1000 ? "X" : " "
if html_mode
puts "<tr><td>#{expenseName}</td><td>#{expense.amount}</td><td>#{mealOverExpensesMarker}</td></tr>"
else
puts "#{expenseName}\t#{expense.amount}\t#{mealOverExpensesMarker}"
end
total += expense.amount
end
if html_mode
puts "</tbody>"
puts "</table>"
end
if html_mode
puts "<p>Meal Expenses: #{mealExpenses}</p>"
puts "<p>Total Expenses: #{total}</p>"
else
puts "Meal Expenses: #{mealExpenses}"
puts "Total Expenses: #{total}"
end
if html_mode
puts "</body>"
puts "</html>"
end
end
printReport(
true,
Expense.new(:breakfast, 1000),
Expense.new(:breakfast, 1001),
Expense.new(:dinner, 5000),
Expense.new(:dinner, 5001),
Expense.new(:car_rental, 4),
)