-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathExpenseReport.pl
executable file
·92 lines (81 loc) · 2.32 KB
/
ExpenseReport.pl
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
90
91
92
#!/usr/bin/perl
package Expense;
use constant {
DINNER => 1,
BREAKFAST => 2,
CAR_RENTAL => 3,
};
sub new {
my $class = shift;
my $self = {
'type' => shift,
'amount' => shift
};
bless $self, $class;
return $self;
}
sub printReport($@) {
my $html_mode = shift;
my @expenses = @_;
my $mealExpenses = 0;
my $total = 0;
my $datestring = localtime();
if ($html_mode) {
print <<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 <<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 my $expense (@expenses) {
if ($expense->{'type'} == DINNER or $expense->{'type'} == BREAKFAST) {
$mealExpenses += $expense->{'amount'};
}
my $expenseName = "";
if ($expense->{'type'} == DINNER) {
$expenseName = "Dinner";
} elsif ($expense->{'type'} == BREAKFAST) {
$expenseName = "Breakfast";
} elsif ($expense->{'type'} == CAR_RENTAL) {
$expenseName = "Car Rental";
}
my $mealOverExpensesMarker = $expense->{'type'} == DINNER && $expense->{'amount'} > 5000 || $expense->{'type'} == BREAKFAST && $expense->{'amount'} > 1000 ? "X" : " ";
if ($html_mode) {
print "<tr><td>$expenseName</td><td>".$expense->{'amount'}."</td><td>$mealOverExpensesMarker</td></tr>\n";
} else {
print "$expenseName\t".$expense->{'amount'}."\t$mealOverExpensesMarker\n";
}
$total += $expense->{'amount'};
}
if ($html_mode) {
print "</tbody>\n";
print "</table>\n";
}
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 "</body>\n";
print "</html>\n";
}
}
printReport(1, new Expense(BREAKFAST, 1000), new Expense(BREAKFAST, 1001), new Expense(DINNER, 5000),new Expense(DINNER, 5001), new Expense(CAR_RENTAL, 4));