Skip to content

Commit

Permalink
Revert accidental code change.
Browse files Browse the repository at this point in the history
  • Loading branch information
christianhujer committed Feb 3, 2022
1 parent 7a1266e commit edb597f
Showing 1 changed file with 22 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
package com.nelkinda.training

import com.nelkinda.training.ExpenseType.*
import java.time.Clock
import java.time.LocalDateTime
import java.time.ZonedDateTime
import java.time.format.DateTimeFormatter
import java.time.format.FormatStyle
import java.util.*
import java.util.Date

enum class ExpenseType {
DINNER, BREAKFAST, CAR_RENTAL
Expand All @@ -15,41 +9,35 @@ enum class ExpenseType {
class Expense {
lateinit var type: ExpenseType
var amount: Int = 0

fun isMeal() =
type == DINNER || type == BREAKFAST

val name
get() = when (type) {
DINNER -> "Dinner"
BREAKFAST -> "Breakfast"
CAR_RENTAL -> "Car Rental"
}

fun isOverLimit() =
type == DINNER && amount > 5000 || type == BREAKFAST && amount > 1000
}

class ExpenseReport(
private val clock: Clock = Clock.systemDefaultZone()
) {
class ExpenseReport {
fun printReport(expenses: List<Expense>) {
printReport(expenses, Date.from(clock.instant()).toString())
}
var total = 0
var mealExpenses = 0

fun printReport(expenses: List<Expense>, date: String) {
println("Expenses $date")
println("Expenses ${Date()}")

for (expense in expenses) {
val expenseName = expense.name
val mealOverExpensesMarker = if (expense.isOverLimit()) "X" else " "
if (expense.type == ExpenseType.DINNER || expense.type == ExpenseType.BREAKFAST) {
mealExpenses += expense.amount
}

var expenseName = ""
when (expense.type) {
ExpenseType.DINNER -> expenseName = "Dinner"
ExpenseType.BREAKFAST -> expenseName = "Breakfast"
ExpenseType.CAR_RENTAL -> expenseName = "Car Rental"
}

val mealOverExpensesMarker = if (expense.type == ExpenseType.DINNER && expense.amount > 5000 || expense.type == ExpenseType.BREAKFAST && expense.amount > 1000) "X" else " "

println(expenseName + "\t" + expense.amount + "\t" + mealOverExpensesMarker)

total += expense.amount
}

println("Meal expenses: ${expenses.sumMeals()}")
println("Total expenses: ${expenses.sumTotal()}")
println("Meal expenses: $mealExpenses")
println("Total expenses: $total")
}

private fun List<Expense>.sumTotal(): Int = sumOf { it.amount }
private fun List<Expense>.sumMeals(): Int = filter { it.isMeal() }.sumOf { it.amount }
}

0 comments on commit edb597f

Please sign in to comment.