Problem overview
This is the first version of the Shopping Cart project. It is a small program that prints a one-item receipt, and it practises variables, arithmetic and string concatenation together. The starter code gives item = "Coffee Mug", price = 12 and quantity = 3.
The program must work out the total cost, which is price * quantity, build two lines of text, join them with a newline into one string called result, and print it. The expected output is:
Coffee Mug x3
Total: $36
The first line is the item name, a space, an x and the quantity. The second line is Total: $ followed by the total.
Approach
Work in the order the data flows. First calculate the number you need, total. Then build each output line in its own variable, line1 and line2. Finally join the lines into result and print it once.
Giving each line a name makes the program easy to read and to change. When a later version adds more rows, the new rows are just more variables, and the part that joins them is the only place that needs to grow.
Step by step
- Create
totalasprice * quantity. Three mugs at 12 each cost 36. It is aval, because it is calculated once. - Create
line1. Joinitem, the text" x"(a space and the letter x) andquantity. The text starts with a space, which is why the output readsCoffee Mug x3and notCoffee Mugx3. - Create
line2. Join the label"Total: $"andtotal. - Create
result. Joinline1,"\n"andline2. The\nescape is a single newline character, so the second line begins right after it. - Print
resultwithprintln.
The solution in Kotlin
val item = "Coffee Mug"
val price = 12
val quantity = 3
val total = price * quantity
val line1 = item + " x" + quantity
val line2 = "Total: $" + total
val result = line1 + "\n" + line2
println(result)
Kotlin details worth knowing:
- Adding a number to a string. When the left side of
+is aString, Kotlin turns the number on the right into text and joins them. Soitem + " x" + quantityworks from left to right: theStringitemplus" x"is aString, and aStringplus theIntquantityis aStringagain. The other order does not compile:quantity + " x"is an error, becauseInt.plusaccepts only numbers. - Arithmetic comes first.
price * quantityis anInt, with no text involved. Doing the arithmetic in its own variable keeps numbers and text apart. - Dollar signs in strings. Kotlin uses
$for string templates, as in"$total". A$directly before a closing quote has nothing to insert, so"Total: $"prints a plain dollar sign. To put a dollar sign right before a template, escape it:"Total: \$$total"also printsTotal: $36. - The newline.
"\n"is an escape sequence for one character.printlnthen adds its own line ending after the last line. - Types. Kotlin infers
totalasIntand the lines asString. No type annotations are needed.
Edge cases and common mistakes
- A missing space. Writing
"x"instead of" x"givesCoffee Mugx3. The lesson's tests compare the text exactly. - A space after the dollar sign. The second line is
Total: $36, with no space between$and the number. - Starting a join with a number.
total + " dollars"doesn't compile for the reason above. Start with a string or use a template. - Printing the lines separately. Two
printlncalls produce the same two lines, but the lesson asks for one string,result, built with a newline, so follow that. - Adding instead of multiplying. The total is
price * quantity, not a sum of the two.
Testing
Change item, price and quantity in the editor above and check the output:
| item, price, quantity | output |
|---|---|
| "Coffee Mug", 12, 3 | Coffee Mug x3, then Total: $36 |
| "Notebook", 4, 10 | Notebook x10, then Total: $40 |
