Skip to content

Instantly share code, notes, and snippets.

@drulabs
Last active August 7, 2018 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drulabs/e57ed00b25f13b2d768e446d76fdc882 to your computer and use it in GitHub Desktop.
Save drulabs/e57ed00b25f13b2d768e446d76fdc882 to your computer and use it in GitHub Desktop.
// THE KOTLIN WAY
// 1. Who did maximum number of transactions
val maxTransactionsBy = customers.maxBy { it.transactions.count() }
// List all transactions irrespective of customers
val transactions = customers.flatMap { it.transactions }
// 2a. Get total amount deposited in the bank (use transactions constant from above)
val totalAmountDeposited = transactions.filter { it.type == "deposit" }.sumByDouble { it.amount }
// 2b. Get total amount withdrawn from the bank (use transactions constant from above)
val totalAmountWithdrawn = transactions.filter { it.type == "withdraw" }.sumByDouble { it.amount }
// 3. What is the net amount deposited?
val netAmountDeposited = transactions.fold(0.0) { amount, transaction ->
when (transaction.type) {
"deposit" -> amount + transaction.amount
"withdraw" -> amount - transaction.amount
else -> amount
}
}
// 4. The Bank require customers to keep a minimum balance of 1500.00
// Create lists of customers with balance BELOW and ABOVE minimum allowed balance
val minimumAllowedLimit = 1500.0
val (lowBalanceCustomers, highBalanceCustomers) = customers.partition { it.balance < minimumAllowedLimit }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment