forked from christianhujer/expensereport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExpenseReport.pl
executable file
·50 lines (42 loc) · 1.39 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
#!/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 @expenses = @_;
my $mealExpenses = 0;
my $total = 0;
my $datestring = localtime();
print "Expenses: $datestring\n";
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" : " ";
print "$expenseName\t".$expense->{'amount'}."\t$mealOverExpensesMarker\n";
$total += $expense->{'amount'};
}
print "Meal Expenses: $mealExpenses\n";
print "Total Expenses: $total\n";
}
printReport(new Expense(CAR_RENTAL, 5), new Expense(DINNER, 5000), new Expense(BREAKFAST, 1001), new Expense(CAR_RENTAL, 4));