Kotlinlearncs.online LogoJava
Return to List

Test Writing: Game CSV Sum

Created By: Geoffrey Challen
/ Version: 2020.9.0

Chuchu and Xyz are playing a game. They are keeping score by adding records to a String, like this:

Xyz, 10
Chuchu,5
Xyz, 20
Xyz, 4
Chuchu, 30
Xyz, 31
Chuchu, 31
Chuchu, 10

The winner of the game is the player with the most total points. For the game above, the winner is Chuchu, since he scored 76 points to Xyz's 65. Note that zero is a valid number of points to have scored and should not be treated separately.

Write a function called winner that determines the winner of the game. It accepts a single String argument containing CSV records as shown above and should return a String containing either the winner: either Xyz or Chuchu. If both player score the same number of points, you should return "Tie".

A few notes. First, you'll want to use both split and trim, two of our familiar functions for working with String data. Second, while each line will contain a record, there may be extra whitespace surrounding each record. (The data was entered by bored animals.) Third, recall that you can use Integer.parseInt to convert a String to an int. Fourth, the only two players you need to handle are Xyz and Chuchu. Finally, if the String passed is null or empty you should return null.

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 Java's assert method