Contents

Kata 8 solution

First, create a file in the App folder, called UserAgeValidator.cs.

using System;

class UserAgeValidator {
}

The most obvious thing to do first is to create a public method that validates the user’s age, and returns true or false:

using System;

class UserAgeValidator {   
    public bool ValidateUserAge() {
        return true;
    }
}

Next we want to get the user’s age from the Standard Input. Copy over GetUsersAgeInYears from QuizGame.cs:

using System;

class UserAgeValidator {   
    public bool ValidateUserAge() {
        int ageInYears = GetUsersAgeInYears();
        return true;
    }

    private int GetUsersAgeInYears() {
        Console.WriteLine("Please tell me how old you are in years!");
        int ageInYears = Convert.ToInt32(Console.ReadLine());
        return ageInYears;
    }
}

Next we want to validate the return value of GetUsersAgeInYears. Copy over GetIsAgeValid from QuizGame.cs:

using System;

class UserAgeValidator {

    public bool ValidateUserAge() {
        int ageInYears = GetUsersAgeInYears();;
        bool isAgeValid = GetIsAgeValid(ageInYears);

        return isAgeValid;
    }

    private int GetUsersAgeInYears() {
        Console.WriteLine("Please tell me how old you are in years!");
        int ageInYears = Convert.ToInt32(Console.ReadLine());
        return ageInYears;
    }

    private bool GetIsAgeValid(int ageInYears) {
        bool isAgeValid = ageInYears >= 0;

        return isAgeValid;
    }
}

Look in ValidateUserAge - we call GetIsAgeValid and return that value.

Finally, amend StartGame in QuizGame.cs to use the new class:

public void StartGame(string name) {
    GreetUser(name);
    UserAgeValidator userAgeValidator = new UserAgeValidator();

    bool isAgeValid = userAgeValidator.ValidateUserAge();

    if(isAgeValid == true) {
        DoQuizQuestions();
    } else {
        Console.WriteLine("Age is invalid...Sorry!");
    }
}
Copyright Mikiel Agutu 2019