-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathExpenseReport.raku
executable file
·82 lines (73 loc) · 2.41 KB
/
ExpenseReport.raku
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
#!/usr/bin/rakudo
use v6;
enum ExpenseType <DINNER BREAKFAST CAR_RENTAL>;
class Expense {
has ExpenseType $.type;
has Int $.amount;
}
sub printReport($html_mode, *@expenses) {
my $mealExpenses = 0;
my $total = 0;
my $datestring = DateTime.now();
if ($html_mode) {
print qq:to/END/;
<!DOCTYPE html>
<html lang="en">
<head>
<title>Expense Report: {$datestring}</title>
</head>
<body>
<h1>Expense Report: {$datestring}</h1>
END
} else {
print "Expense Report: $datestring\n";
}
if ($html_mode) {
print qq:to/END/;
<table>
<thead>
<tr><th scope="col">Type</th><th scope="col">Amount</th><th scope="col">Over Limit</th></tr>
</thead>
<tbody>
END
}
for @expenses -> $expense {
if ($expense.type == DINNER || $expense.type == BREAKFAST) {
$mealExpenses += $expense.amount;
}
my $expenseName = "";
given $expense.type {
when DINNER { $expenseName = "Dinner"; }
when BREAKFAST { $expenseName = "Breakfast"; }
when CAR_RENTAL { $expenseName = "Car Rental"; }
}
my $amount = $expense.amount;
my $mealOverExpensesMarker = $expense.type == DINNER && $expense.amount > 5000 || $expense.type == BREAKFAST && $expense.amount > 1000 ?? "X" !! " ";
if ($html_mode) {
print "<tr><td>{$expenseName}</td><td>{$amount}</td><td>{$mealOverExpensesMarker}</td></tr>\n";
} else {
print "$expenseName\t$amount\t$mealOverExpensesMarker\n";
}
$total += $expense.amount;
}
if ($html_mode) {
print qq:to/END/;
</tbody>
</table>
END
}
if ($html_mode) {
print "<p>Meal Expenses: {$mealExpenses}</p>\n";
print "<p>Total Expenses: {$total}</p>\n";
} else {
print "Meal Expenses: $mealExpenses\n";
print "Total Expenses: $total\n";
}
if ($html_mode) {
print qq:to/END/;
</body>
</html>
END
}
}
printReport(1, Expense.new(type => BREAKFAST, amount => 1000), Expense.new(type => BREAKFAST, amount => 1001), Expense.new(type => DINNER, amount => 5000), Expense.new(type => DINNER, amount => 5001), Expense.new(type => CAR_RENTAL, amount => 4));