Contents

Kata 10

Overview

In this Kata you’ll design, develop, and test a C# computer program from scratch. This will pull together all the skills you’ve learned in the book so far!

Problem description

A small business owner is having trouble calculating how much to pay his employees every month. He has contacted you in order to build a software system that can solve this problem for him.

The business employs people to work shifts in a factory. Employees are paid on an hourly basis.

Shifts can vary in length, and frequency. This means each month the pay for any given employee can vary. Calculating this by hand is tedious, so the business owner would like a computer system that can do this for him.

The business owner explains that at the end of the month pay packets are calculated, then distributed to employees. Currently the workflow looks like this:

  • The registration system creates a text file containing how many hours each employee has worked
  • This file is sent to the accountant’s computer
  • The accountant manually calculates how much each employee is owed
  • The accountant manually creates a text file with the employee pay information
  • This file is then e-mailed to the senior accountant to be checked
  • If it’s OK, a bank transfer is set up to each employees bank account

You are given a sample of the text file that is currently used to calculate how much the employees should be paid each month. This is known as a Work-Rate file. A sample file is provided:

7.60
4
John 180
Sally 122
Mike 200
Sara 167

The format of the Work-Rate is as follows:

  • The first line is the hourly rate of pay
    • It is always a decimal point number
  • The second line is the number of work rate entries
  • On the following line there will be zero or more work rate entires
  • Each entry contains on a single line:
    • The employees first name
    • A space
    • An integer representing how many hours the employee has worked

An employee is paid for the amount of hours worked multiplied by the rate of pay.

For every hour worked over 180 hours a week, an employee earns overtime pay. Overtime pay is equal to the 1.5x the normal rate of pay.

Currently, the accountant produces a computer file containing how much each employee is owed. This is known as the Payroll file. A sample of the corresponding payroll created from the above sample is provided:

Pay for employees this month:
John 1368
Sally 927.2
Mike 1596
Sara 1269.2

Requirements

The business owner asks if you can create a program that can:

  • Read the Work-Rate file from a file called workRate.txt
  • Calculate the amount each employee is owed using the file
    • Pay = Hours Worked * Rate of pay + (Overtime hours worked * Overtime rate of pay)
  • Output the Payroll File to the Standard Output

Validating your program

Inside this Kata’s folder is a TestData folder. This contains Work-Rate files, and their corresponding Payroll files. Use these to validate that your program produces the right Payroll file for each Work-Rate file. Try and get your program to work with them all!

How to approach the problem

Starting out

First, you’ll want to create a new project using the .NET Core SDK command line tools. Look at Kata 2 to refresh your memory on how that’s done.

Next, you need to create a class to hold the majority of your code. Create a new file and write the skeleton for your class in it.

For instance, you could create a class called PayrollCalculator, in PayrollCalculator.cs. The skeleton for PayrollCalculator might look like this:

using System;

class PayrollCalculator {
    public void CalculatePayroll() {
        // Code will go here...
    }
}

Then, in Program.cs, call CalculatePayroll from Main:

static void Main(string[] args)
{
    PayrollCalculator payrollCalculator = new PayrollCalculator();
    payrollCalculator.CalculatePayroll();
}

Parsing the Work-Rate file

You need to read the Work-Rate file into the program using an array. Recall Kata 9. Remember to import System.IO at the top of your class:

using System.IO;

You can now use File.ReadAllLines to read a Work-Rate file into your program. Because the files are in the TestData folder, you’ll need to include that in the filepath you provide to File.ReadAllLines, like so:

string[] file = File.ReadAllLines("TestData/workRate-1.txt");

Next, think about how to parse the Work-Rate file once it’s been loaded into your program. There are 3 key pieces of information:

  • The hourly rate
  • The number of Work-Rate entries in the file
  • The Work-Rate entires themselves

What data type makes sense for the hourly rate? What about the number of Work-Rate entires in the file? How can you convert strings into other data types? Recall Kata 4. How might you find out how to convert strings into types other than ints?

How can you repeatedly read the Work-Rate file entires? Which array index might you need to start at? Which array index will you read up until? How can you parse the text from the file into separate variables? Recall Kata 7 and Kata 9.

Employees

You know that each employee has a name, and a number of hours worked. Perhaps it makes sense to create a class that can represent an employee. What fields might a potential Employee class need? What would the constructor look like? Recall Kata 9.

Once you’ve parsed the Work-Rate file, how might you use the variables you’ve parsed to construct an Employee object? How might you then place each Employee object in an array to use later? Which index should each Employee object be placed into the array?

Calculating a single employee’s pay

It would be nice to have method that is able to take an hourly rate, and number of hours worked, and calculate how much to pay based on those. This method could also calculate overtime.

What would be the types of this method’s arguments? What would the return type of such a method be? Recall Kata 6. How can you use mathematical operators to calculate how much each employee is owed? Recall Kata 4. How can you use logic to determine if overtime should be paid? How can you calculate the number of hours of overtime an employee has worked?

If you’re finding the overtime part difficult, just try and calculate using the normal rate at first. Once that’s working, then you can try adding the logic for overtime pay too.

Calculating the Payroll file

It would be even nicer to have a method that can take an array of Employee objects, and an hourly rate as arguments, and output the Payroll file to the Standard Output.

How would it interact with the method that can calculate a single employee’s pay? How could you print to the Standard Output? Recall Kata 3.

General tips

  • If you get stuck, try and get small pieces of the program working instead of tackling it all at once.
    • Try and read in the Work-Rate file, and print it back out again
    • Try and calculate the correct pay for a single employee
  • Use Console.WriteLine liberally throughout your code to see the value of variables. It can help you understand how your code is working.
  • Use descriptive variable names that explain what data they hold. This can make things clearer
    • For instance, instead of n being shorthand for an employee’s name, use employeeName
  • If all else fails, you can refer to the model solution! This should help clear things up
    • There is also an in-depth discussion of how to write the solution too!
    • You can find the model solution for Kata 9 on the Kata Solutions page
Copyright Mikiel Agutu 2019