Last active
August 29, 2015 14:13
-
-
Save ascjones/7e1fa0f654f7aa1c0438 to your computer and use it in GitHub Desktop.
Yes Or No
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class YesOrNo<TState> | |
{ | |
private readonly TState state; | |
private readonly bool keepGoing; | |
public bool Success { get { return keepGoing; } } | |
public YesOrNo(TState state = default (TState), bool keepGoing = true) | |
{ | |
this.state = state; | |
this.keepGoing = keepGoing; | |
} | |
public YesOrNo<TState> Question(string question, Func<TState, TState> onYes, Action<TState> onNo) | |
{ | |
if (keepGoing) | |
{ | |
Console.Write(question + " "); | |
var key = Console.ReadKey(); | |
if (key.KeyChar.ToString().ToLower() == "y") | |
{ | |
Console.WriteLine(); | |
var newState = onYes(state); | |
return new YesOrNo<TState>(newState); | |
} | |
Console.WriteLine(); | |
onNo(state); | |
return new YesOrNo<TState>(state, false); | |
} | |
return new YesOrNo<TState>(state, false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment