Menu
CoddyTech

Project: Batch Processor

Lesson 46 of 48 in Coddy's Kotlin for C# Developers course.

challenge icon

Challenge

Easy

Keep commands 1, 2 and 3. Add command 4, yes/no membership on the next line, then zero or more raw order rows. Processor(pricing:Pricing).summary(rows:List<String>) must use OrderParser and the supplied policy for each valid order. Return valid count, invalid count and total on separate lines. Apply discounts separately per order. Total fits Int. A custom supplied policy must also work.

Implement the editable files and return the result from Exercise.solve(input: List<String>). Do not print inside editable methods. The visible locked Main.kt reads all input lines and prints your returned text with one final newline. Text.kt supplies parsing and formatting helpers. All arithmetic results fit Int unless a narrower bound is stated.

Editable files: Order.kt, Pricing.kt, StandardPricing.kt, MemberPricing.kt, OrderParser.kt, Processor.kt, Exercise.kt, Text.kt. Only Main.kt has main; it is locked and is the last file. All files compile together in the same package.

The locked driver independently calls the required public APIs on fresh objects and alternate collaborators. Implement those behaviors; reproducing only fixture output is insufficient.

Try it yourself

fun main() {
    val input = generateSequence { readlnOrNull() }.toList()
    val stage=input[0].toInt()
check(Order("probe",7,3).subtotal()==21 && Order("Ada",0,2).customer=="Ada")
if(stage>=2)check(StandardPricing().total(13)==13 && MemberPricing().total(13)==11 && MemberPricing().total(0)==0)
if(stage>=3){check(OrderParser.parse("Ada 7 3").subtotal()==21);var bad=false;try{OrderParser.parse("Ada 7 0")}catch(e:IllegalArgumentException){bad=true};check(bad)}
if(stage>=4){val p=Processor(Pricing {it+1});check(p.summary(listOf("Ada 7 3","bad","Bo 0 1"))=="2\n1\n23");check(p.summary(emptyList())=="0\n0\n0")}

    println(Exercise.solve(input))
}

All lessons in Kotlin for C# Developers

Practice on your own: Kotlin playground