Problem overview
This first version of the shopping cart prints a tiny receipt for one item. The program starts with three values: the item name, the price of one unit and the quantity bought. It multiplies the price by the quantity to get the total and prints two lines. The first is the item and the quantity, written as Coffee Mug x3. The second is the total, written as Total: $36.
With price = 12 and quantity = 3, the total is 12 * 3 = 36:
Coffee Mug x3
Total: $36
The lesson asks for the two lines to be joined into one string called result, which is then printed once.
Approach: build each piece, then join them
The receipt is made of small parts, and the clearest way to write it is one named value per part:
totalis the number, the price times the quantity;line1is the first row, built from the item name, the text" x"and the quantity;line2is the second row, built from the text"Total: $"and the total;resultis the two rows with a newline between them.
Naming each part makes the last line of the program trivial and makes mistakes easy to find: if the receipt looks wrong, you can print one part and see which one is off.
Step by step
- Compute
total. Writeval total = price * quantity. TwoIntvalues multiplied give anInt. - Build
line1. Joinitem, the text" x"andquantitywith+. The space belongs inside the quotes, before thex. - Build
line2. Join the text"Total: $"andtotal. The dollar sign is part of the text. - Build
result. Putline1, then"\n", thenline2. The sequence\ninside a string is a single newline character, and it makes whatever follows start on a new line. - Print
resultwithprintln.
The solution in Scala
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)
Scala details worth knowing:
valmeans a value that never changes. Every name in this program is assigned once, so each is aval. Scala works out the types itself:totalis anIntandline1is aString.+joins text and converts numbers. When one side of+is aStringand the other is anInt, Scala turns the number into text. That is whyitem + " x" + quantityworks with no explicit conversion.- The
$is an ordinary character here. In"Total: $"it prints as it is. It only has a special meaning inside an interpolated string, one that starts withsbefore the opening quote, such ass"$item x$quantity". That form gives the sameCoffee Mug x3as the+version, but this lesson uses+. printlnadds a final newline. The printed result is two lines, and the output ends with a line break afterTotal: $36.- Order matters. The statements run from top to bottom, so a name has to be defined on an earlier line than the line that uses it.
Testing
For the starter values, result is the string Coffee Mug x3, a newline and Total: $36. It is 24 characters long, and splitting it at the newline gives two pieces, one per receipt row. Changing the data changes the receipt in the same way: with price = 10 and quantity = 1, the total is 10 and the receipt says Total: $10.
Edge cases and common mistakes
- Forgetting the space before the
x.item + "x" + quantityprintsCoffee Mugx3. - Leaving out
"\n". The two rows are joined on one line:Coffee Mug x3Total: $36. - Putting a space after the newline. The second row would start with a space, and the text would no longer match.
- Using
vareverywhere. It works, butvaltells readers, and the compiler, that the value never changes.
Key takeaways
- Break the output into named parts, then join the parts.
+between aStringand anIntmakes text."\n"starts a new line, and$is just a character in a plain string.- The next version builds on this one.
