Warmup: A shopping list |
As a warmup exercise, let's program a simple shopping calculator. The following example run should explain what it does (the answers to the questions are entered by the user):
Shopping Calculator V0.1 What did you buy? apples How many apples did you buy? 3 What is the price of one apples? 1500 You bought 3 apples for 4500 KRW. What did you buy? bananas How many bananas did you buy? 11 What is the price of one bananas? 800 You bought 11 bananas for 8800 KRW. What did you buy? chocolate How many chocolate did you buy? 1 What is the price of one chocolate? 2200 You bought 1 chocolate for 2200 KRW. What did you buy? coffee How many coffee did you buy? 2 What is the price of one coffee? 2500 You bought 2 coffee for 5000 KRW. What did you buy? In total, you spent 20500 KRW
So the calculator asks for the name of each item, the number of items, and the unit price of the item. It then prints out the total price of each item.
The program ends when the user simply presses Enter instead of typing an item name (that is, the input is an empty string). Then the program prints out the total price of all items.
The tutorial explains the function readString to read from the terminal.
In the second release of the shopping calculator, it prints out the list of items, nicely formatted like this:
Shopping Calculator V0.2 What did you buy? apples How many apples did you buy? 3 What is the price of one apples? 1500 You bought 3 apples for 4500 KRW. What did you buy? bananas How many bananas did you buy? 11 What is the price of one bananas? 800 You bought 11 bananas for 8800 KRW. What did you buy? chocolate How many chocolate did you buy? 1 What is the price of one chocolate? 2200 You bought 1 chocolate for 2200 KRW. What did you buy? coffee How many coffee did you buy? 3 What is the price of one coffee? 2500 You bought 3 coffee for 7500 KRW. What did you buy? Your purchases: ------------------------------------------------------------- apples 3 x 1500 KRW 4500 KRW bananas 11 x 800 KRW 8800 KRW chocolate 1 x 2200 KRW 2200 KRW coffee 3 x 2500 KRW 7500 KRW ------------------------------------------------------------- Total price: 23000 KRW -------------------------------------------------------------You will need to store the items in a list. You can add elements to the end of a list using the add method:
>>> val a = mutableListOf(1, 2, 3, 4) >>> a.add(5) true >>> a [1, 2, 3, 4, 5]You can make an empty array (that is, an array with zero elements) using mutableListOf<T>(), like this (note that you have to indicate the type of elements you want to put into the list later):
>>> val b = mutableListOf<Int>() >>> b [] >>> b.add(7) true >>> b [7] >>> val c = mutableListOf<String>() >>> c [] >>> c.add("CS109") true >>> c [CS109]
In our case, we want to store items in the list that consist of a name, a number, and a price per unit. That is a triple of a string and two integers:
>>> val a = mutableListOf<Triple<String, Int, Int>>() >>> a [] >>> a.add(Triple("apples", 3, 1500)) true >>> a [(apples, 3, 1500)]
To format the strings and numbers with a fixed number of characters, use the format method. Here are a few examples:
>>> "%10s".format("abcdef") abcdef >>> "%10s".format("ab") ab >>> "%-10s".format("ab") ab >>> "%-10s".format("abcdef") abcdef >>> "%5d".format(1) 1 >>> "%5d".format(130) 130 >>> "%5d".format(1300) 1300 >>> "%-8s %5d".format("abc", 13) abc 13 >>> "%-8s %5d".format("abcdef", -1234) abcdef -1234
Warmup: A shopping list |