Kotlinlearncs.online LogoJava
Return to List

Test Writing: Calculate Grocery Bill

Created By: Pranav Narayanan
/ Version: 2024.9.1

Write a method groceryBill that takes a String input of a grocery bill and returns the subtotal. Each new item is on a new line, and each line will be in CSV format:

item(String), quantity(Int), price(Double)
"Apples", 3, 2.00

This one line would have a total of 3 * 2.00 = 6.00. There will be a variable amount of whitespace surrounding the values. If the passed string is empty, return 0.0. Otherwise you can assume that the input has at least one entry and that it follows this format.

You will need the String.toInt() and String.toDouble() methods to turn a String into a Int/Double value.

Test Design Challenge

You're challenge is to write tests for this problem described above.

  • Provide a method named test that accepts no arguments and does not return a value.
  • If the implementation of the class described above is incorrect, your test method should throw an exception.
  • If it is correct, do not throw an exception.
  • You may want to use Kotlin's assert or check methods
Report a Problem

Attribution must link to this page: https://www.learncs.online/testtesting/kotlin/calculate-grocery-bill/[email protected]