diff --git a/expensereport-abs/ExpenseReport.bas b/expensereport-abs/ExpenseReport.bas index 035cee37..e819e9a4 100644 --- a/expensereport-abs/ExpenseReport.bas +++ b/expensereport-abs/ExpenseReport.bas @@ -1,27 +1,38 @@ +DIM SHARED DINNER, BREAKFAST, CARRENTAL DINNER = 0: BREAKFAST = 1: CARRENTAL = 2 -DIM type(5), amount(5) + +DIM type(4), amount(4) type(0) = DINNER: amount(0) = 5000 type(1) = DINNER: amount(1) = 5001 type(2) = BREAKFAST: amount(2) = 1000 type(3) = BREAKFAST: amount(3) = 1001 type(4) = CARRENTAL: amount(4) = 4 -GOSUB PrintExpenses +CALL PrintExpenses(type(), amount()) END -PrintExpenses: - meals = 0 +SUB PrintExpenses(type(), amount()) STATIC total = 0 - PRINT "Expensereport: " DATE$ - FOR i=0 TO 4 - IF type(i) = DINNER OR type(i) = BREAKFAST THEN meals = meals + amount(i) + mealExpenses = 0 + + PRINT "Expenses: " DATE$ + + FOR i=LBOUND(type) TO UBOUND(type) + IF type(i) = DINNER OR type(i) = BREAKFAST THEN + mealExpenses = mealExpenses + amount(i) + END IF + expenseName$ = "" IF type(i) = DINNER THEN expenseName$ = "Dinner" IF type(i) = BREAKFAST THEN expenseName$ = "Breakfast" IF type(i) = CARRENTAL THEN expenseName$ = "Car Rental" + IF type(i) = DINNER AND amount(i) > 5000 OR type(i) = BREAKFAST AND amount(i) > 1000 THEN mealOverExpensesMarker$ = "X" ELSE mealOverExpensesMarker$ = " " + PRINT expenseName$ CHR$(9) STR$(amount(i)) CHR$(9) mealOverExpensesMarker$ + total = total + amount(i) NEXT i - PRINT "Meal Expenses:" STR$(meals) + + PRINT "Meal Expenses:" STR$(mealExpenses) PRINT "Total Expenses:" STR$(total) - RETURN +END SUB diff --git a/expensereport-ada/expensereport.adb b/expensereport-ada/expensereport.adb index 4c6fe2a3..1befdd7a 100644 --- a/expensereport-ada/expensereport.adb +++ b/expensereport-ada/expensereport.adb @@ -17,11 +17,13 @@ procedure expensereport is expenseName : Unbounded_String; mealOverExpensesMarker : Character := ' '; begin - Put_Line("Expense Report"); + Put_Line("Expenses:"); + for i in expenses'Range loop if (expenses(i).eType = Breakfast or expenses(i).eType = Dinner) then mealExpenses := mealExpenses + expenses(i).amount; end if; + expenseName := To_Unbounded_String("Foo" & "Foo"); case expenses(i).eType is when Breakfast => @@ -31,14 +33,18 @@ procedure expensereport is when CarRental => expenseName := To_Unbounded_String("Car Rental"); end case; + if ((expenses(i).eType = Breakfast and expenses(i).amount > 1000) or (expenses(i).eType = Dinner and expenses(i).amount > 5000)) then mealOverExpensesMarker := 'X'; else mealOverExpensesMarker := ' '; end if; + Put_Line(expenseName & Ada.Characters.Latin_1.HT & Ada.Strings.Fixed.Trim(Integer'Image(expenses(i).amount), Ada.Strings.Left) & Ada.Characters.Latin_1.HT & mealOverExpensesMarker); + total := total + expenses(i).amount; end loop; + Put_Line("Meal expenses:" & Integer'Image(mealExpenses)); Put_Line("Total expenses:" & Integer'Image(total)); end printReport; diff --git a/expensereport-bash/ExpenseReport.sh b/expensereport-bash/ExpenseReport.sh index ae3044e3..2c556be7 100755 --- a/expensereport-bash/ExpenseReport.sh +++ b/expensereport-bash/ExpenseReport.sh @@ -1,9 +1,11 @@ #!/bin/bash function printReport() { - echo "Expenses: $(date)" total=0 meals=0 + + echo "Expenses: $(date)" + type=0 amount=1 while read -a expense ; do @@ -20,8 +22,10 @@ function printReport() { mealOverExpensesMarker=$(if [ "${expense[$type]}" = "DINNER" ] && [ "${expense[$amount]}" -gt 5000 ] || [ "${expense[$type]}" = "BREAKFAST" ] && [ "${expense[$amount]}" -gt 1000 ] ; then echo "X" ; else echo " " ; fi) echo -e "$expenseName\t${expense[$amount]}\t$mealOverExpensesMarker" + total=$(( ${total} + ${expense[$amount]} )) done + echo "Meal expenses: $meals" echo "Total expenses: $total" } diff --git a/expensereport-bwb/ExpenseReport.bas b/expensereport-bwb/ExpenseReport.bas index bf2fc697..4ee6158f 100644 --- a/expensereport-bwb/ExpenseReport.bas +++ b/expensereport-bwb/ExpenseReport.bas @@ -8,10 +8,10 @@ 80 GOSUB 100 90 QUIT 100 REM PRINTREPORT -110 MEALS = 0: SUM = 0 +110 MEALEXPENSES = 0: TOTAL = 0 120 PRINT "Expenses:", DATE$(), TIME$() 130 FOR I = 0 TO 4 -140 IF TYPE(I) = 0 OR TYPE(I) = 1 THEN MEALS = MEALS + AMOUNT(I) +140 IF TYPE(I) = 0 OR TYPE(I) = 1 THEN MEALEXPENSES = MEALEXPENSES + AMOUNT(I) 150 NAME$ = "" 160 IF TYPE(I) = 0 THEN NAME$ = "Dinner" 170 IF TYPE(I) = 1 THEN NAME$ = "Breakfast" @@ -22,8 +22,8 @@ 220 GOTO 240 230 MARKER$ = "X" 240 PRINT NAME$, AMOUNT(I), MARKER$ -250 SUM = SUM + AMOUNT(I) +250 TOTAL = TOTAL + AMOUNT(I) 260 NEXT I -270 PRINT "Meal expenses:", MEALS -280 PRINT "Total expenses:", SUM +270 PRINT "Meal expenses:", MEALEXPENSES +280 PRINT "Total expenses:", TOTAL 290 RETURN diff --git a/expensereport-groovy/ExpenseReport.groovy b/expensereport-groovy/ExpenseReport.groovy index 05e9b635..9a13ed9a 100755 --- a/expensereport-groovy/ExpenseReport.groovy +++ b/expensereport-groovy/ExpenseReport.groovy @@ -1,5 +1,7 @@ #!/usr/bin/env groovy +import java.util.Date; + enum ExpenseType { DINNER, BREAKFAST, @@ -14,21 +16,34 @@ class Expense { def printReport(Expense... expenses) { int total = 0; int mealExpenses = 0; - println "Expenses: ${new java.util.Date()}"; + + println "Expenses: ${new Date()}"; + for (Expense expense in expenses) { if (expense.type == ExpenseType.DINNER || expense.type == ExpenseType.BREAKFAST) { mealExpenses += expense.amount; } + String expenseName = ""; switch (expense.type) { - case ExpenseType.DINNER: expenseName = "Dinner"; break; - case ExpenseType.BREAKFAST: expenseName = "Breakfast"; break; - case ExpenseType.CAR_RENTAL: expenseName = "Car Rental"; break; + case ExpenseType.DINNER: + expenseName = "Dinner"; + break; + case ExpenseType.BREAKFAST: + expenseName = "Breakfast"; + break; + case ExpenseType.CAR_RENTAL: + expenseName = "Car Rental"; + break; } + String mealOverExpensesMarker = expense.type == ExpenseType.DINNER && expense.amount > 5000 || expense.type == ExpenseType.BREAKFAST && expense.amount > 1000 ? "X" : " "; + println "$expenseName\t$expense.amount\t$mealOverExpensesMarker"; + total += expense.amount; } + println "Meal Expenses: $mealExpenses"; println "Total Expenses: $total"; } diff --git a/expensereport-rexx/ExpenseReport.rexx b/expensereport-rexx/ExpenseReport.rexx index 06f49bb4..3bc39503 100644 --- a/expensereport-rexx/ExpenseReport.rexx +++ b/expensereport-rexx/ExpenseReport.rexx @@ -7,20 +7,33 @@ expense.2.type = BREAKFAST; expense.2.amount = 1000 expense.3.type = BREAKFAST; expense.3.amount = 1001 expense.4.type = CAR_RENTAL; expense.4.amount = 4 -SAY "Expenses:" DATE() TIME() -meals = 0 -total = 0 -DO i = 0 TO 4 - IF expense.i.type = DINNER | expense.i.type = BREAKFAST THEN meals = meals + expense.i.amount - SELECT - WHEN expense.i.type = DINNER THEN expenseName = "Dinner" - WHEN expense.i.type = BREAKFAST THEN expenseName = "Breakfast" - WHEN expense.i.type = CAR_RENTAL THEN expenseName = "Car Rental" +CALL printReport +EXIT 0 + +printReport: + total = 0 + mealExpenses = 0 + + SAY "Expenses:" DATE() TIME() + + DO i = 0 TO 4 + IF expense.i.type = DINNER | expense.i.type = BREAKFAST THEN mealExpenses = mealExpenses + expense.i.amount + + expenseName = "" + SELECT + WHEN expense.i.type = DINNER THEN expenseName = "Dinner" + WHEN expense.i.type = BREAKFAST THEN expenseName = "Breakfast" + WHEN expense.i.type = CAR_RENTAL THEN expenseName = "Car Rental" + END + + IF expense.i.type = DINNER & expense.i.amount > 5000 | expense.i.type = BREAKFAST & expense.i.amount > 1000 THEN mealOverExpensesMarker = "X" + ELSE mealOverExpensesMarker = " " + + SAY expenseName expense.i.amount mealOverExpensesMarker + + total = total + expense.i.amount END - IF expense.i.type = DINNER & expense.i.amount > 5000 | expense.i.type = BREAKFAST & expense.i.amount > 1000 THEN mealOverExpensesMarker = "X" - ELSE mealOverExpensesMarker = " " - SAY expenseName expense.i.amount mealOverExpensesMarker - total = total + expense.i.amount -END -SAY "Meal expenses:" meals -SAY "Total expenses:" total + + SAY "Meal expenses:" mealExpenses + SAY "Total expenses:" total + RETURN diff --git a/expensereport-scala/src/main/scala/com/nelkinda/training/ExpenseReport.scala b/expensereport-scala/src/main/scala/com/nelkinda/training/ExpenseReport.scala index 76853e26..dd08463e 100644 --- a/expensereport-scala/src/main/scala/com/nelkinda/training/ExpenseReport.scala +++ b/expensereport-scala/src/main/scala/com/nelkinda/training/ExpenseReport.scala @@ -16,23 +16,30 @@ class Expense(val `type`: ExpenseType, val amount: Int) class ExpenseReport { def printReport(expenses: List[Expense]) { - var totalExpenses = 0 + var total = 0 var mealExpenses = 0 + println(s"Expense Report: ${new Date()}") + for (expense <- expenses) { if (expense.`type` == ExpenseType.DINNER || expense.`type` == ExpenseType.BREAKFAST) { mealExpenses += expense.amount } + var expenseName = expense.`type` match { case ExpenseType.DINNER => "Dinner" case ExpenseType.BREAKFAST => "Breakfast" case ExpenseType.CAR_RENTAL => "Car Rental" } + var mealOverExpensesMarker = if (expense.`type` == ExpenseType.DINNER && expense.amount > 5000 || expense.`type` == ExpenseType.BREAKFAST && expense.amount > 1000) "X" else " " + println(s"${expenseName}\t${expense.amount}\t${mealOverExpensesMarker}") - totalExpenses += expense.amount + + total += expense.amount } + println(s"Meal Expenses: ${mealExpenses}") - println(s"Total Expenses: ${totalExpenses}") + println(s"Total Expenses: ${total}") } }