-
Notifications
You must be signed in to change notification settings - Fork 98
/
ExpenseReport.d
43 lines (39 loc) · 1.36 KB
/
ExpenseReport.d
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
import std.stdio;
import std.datetime.systime;
enum ExpenseType { DINNER, BREAKFAST, CAR_RENTAL }
struct Expense {
ExpenseType type;
int amount;
}
void printReport(Expense[] expenses) {
int meals = 0;
int total = 0;
writefln("Expenses: %s", Clock.currTime());
foreach (expense; expenses) {
if (expense.type == ExpenseType.DINNER || expense.type == ExpenseType.BREAKFAST) {
meals += expense.amount;
}
string name = "";
switch (expense.type) {
case ExpenseType.DINNER: name = "Dinner"; break;
case ExpenseType.BREAKFAST: name = "Breakfast"; break;
case ExpenseType.CAR_RENTAL: name = "Car Rental"; break;
default: name = "";
}
string mealOverExpensesMarker = expense.type == ExpenseType.DINNER && expense.amount > 5000 || expense.type == ExpenseType.BREAKFAST && expense.amount > 1000 ? "X" : " ";
writefln("%s\t%s\t%s", name, expense.amount, mealOverExpensesMarker);
total += expense.amount;
}
writefln("Meal expenses: %d", meals);
writefln("Total expenses: %d", total);
}
void main() {
Expense[] expenses = [
{ ExpenseType.DINNER, 5000 },
{ ExpenseType.DINNER, 5001 },
{ ExpenseType.BREAKFAST, 1000 },
{ ExpenseType.BREAKFAST, 1001 },
{ ExpenseType.CAR_RENTAL, 4 },
];
printReport(expenses);
}