Menu
CoddyTech

Project: Order Model

Lesson 43 of 48 in Coddy's Kotlin for Dart Developers course.

challenge icon

Challenge

Easy

Command 1 is the first input line; the next is customer price quantity. Customer is ASCII letters, price 0..1000, quantity 1..100. Implement Order constructor, customer property and subtotal(). Return customer then subtotal. Later API skeletons must retain their signatures for the fixed driver.

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 Dart Developers

Practice on your own: Kotlin playground