Project: Pricing Policies
Lesson 44 of 48 in Coddy's Kotlin for Java Developers course.
Challenge
EasyKeep command 1. Add command 2 followed by customer price quantity membership, with the same bounds and membership yes/no. Implement Pricing.total, StandardPricing preserving subtotal, and MemberPricing returning floor of subtotal times 90/100. Return selected total.
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 Java Developers
1Starting Kotlin
Run Your First KotlinStrings and TemplatesVal, Var, and TypesNumbers and ConversionsNullable Types and ElvisRecap: Receipt4Iteration and Transformations
While, Break, and ContinueLambdas and TransformationsSorting Without MutationDestructuring and IndexesRecap: Rank Scores2Values and Decisions
Equality and IdentityBoolean ConditionsIf and When ExpressionsRanges and For LoopsRecap: Eligibility3Collections and Ownership
Arrays and IntArrayRead-only and Mutable ListsMaps and Missing ValuesAliases and Shallow CopiesRecap: Unique Counts6Input, Errors, and Files
Input and String FormattingNullable Parsing and ErrorsFiles, Objects, and ImportsGenerics and List TypesRecap: Validate Records9Project: Order Processing
Project: Order ModelProject: Pricing PoliciesPractice on your own: Kotlin playground