Last active
February 22, 2019 13:52
-
-
Save DavidBrower/b8f51cd62c83b23e0a6e99418fe9177e to your computer and use it in GitHub Desktop.
Technical Business Analyst Exercise
This file contains 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
using System; | |
using System.Collections.Generic; | |
namespace Robotics | |
{ | |
public static class Program | |
{ | |
private static readonly Dictionary<string, Order> Orders = new Dictionary<string, Order>(StringComparer.OrdinalIgnoreCase) | |
{ | |
["Start"] = Order.Start, | |
["Pause"] = Order.Pause, | |
["Exit"] = Order.Exit, | |
["End"] = Order.End, | |
["Resume"] = Order.Resume | |
}; | |
private static void Main(string[] args) | |
{ | |
Robot process = new Robot(); | |
PrintIntroduction(); | |
PrintState(process.CurrentState); | |
string line; | |
while ((line = Console.ReadLine()) != null) | |
{ | |
Order command; | |
if (Orders.TryGetValue(line, out command)) | |
{ | |
process.MoveNext(command); | |
PrintState(process.CurrentState); | |
} | |
else | |
{ | |
continue; | |
} | |
} | |
} | |
private static void PrintState(RobotState robotState) | |
{ | |
Console.ForegroundColor = ConsoleColor.Blue; | |
Console.WriteLine($"Status: I am {robotState}"); | |
Console.ForegroundColor = ConsoleColor.White; | |
} | |
private static void PrintIntroduction() | |
{ | |
PrintRobot(); | |
Console.WriteLine("Beep! I am a Robot!"); | |
Console.WriteLine(); | |
Console.WriteLine("You can give me one of the following orders:"); | |
Console.ForegroundColor = ConsoleColor.Yellow; | |
foreach (var order in Orders) | |
{ | |
Console.WriteLine($"* {order.Key}"); | |
} | |
Console.ForegroundColor = ConsoleColor.White; | |
} | |
private static void PrintRobot() | |
{ | |
string robot = @" | |
\.===./ | |
| o o | | |
\_O_/ | |
/|(\)|\ | |
d |___| b | |
|_| | |
(ooo) | |
"; | |
Console.ForegroundColor = ConsoleColor.Yellow; | |
Console.WriteLine(robot); | |
Console.ForegroundColor = ConsoleColor.White; | |
} | |
} | |
public enum RobotState | |
{ | |
Inactive, | |
Active, | |
Paused, | |
Terminated | |
} | |
public enum Order | |
{ | |
Start, | |
End, | |
Pause, | |
Resume, | |
Exit | |
} | |
public class Robot | |
{ | |
private readonly Dictionary<Change, RobotState> _validChanges; | |
public RobotState CurrentState { get; private set; } | |
public Robot() | |
{ | |
CurrentState = RobotState.Inactive; | |
_validChanges = new Dictionary<Change, RobotState> | |
{ | |
[new Change(RobotState.Inactive, Order.Exit)] = RobotState.Terminated, | |
[new Change(RobotState.Inactive, Order.Start)] = RobotState.Active, | |
[new Change(RobotState.Active, Order.End)] = RobotState.Inactive, | |
[new Change(RobotState.Active, Order.Pause)] = RobotState.Paused, | |
[new Change(RobotState.Paused, Order.End)] = RobotState.Inactive, | |
[new Change(RobotState.Paused, Order.Resume)] = RobotState.Active | |
}; | |
} | |
private RobotState GetNext(Order order) | |
{ | |
Change transition = new Change(CurrentState, order); | |
RobotState nextState; | |
if (_validChanges.TryGetValue(transition, out nextState)) | |
return nextState; | |
PrintError(order); | |
return CurrentState; | |
} | |
private void PrintError(Order order) | |
{ | |
Console.ForegroundColor = ConsoleColor.Red; | |
Console.WriteLine($"I can't {order} when I'm {CurrentState}!"); | |
Console.ForegroundColor = ConsoleColor.White; | |
} | |
public RobotState MoveNext(Order command) | |
{ | |
CurrentState = GetNext(command); | |
return CurrentState; | |
} | |
} | |
class Change | |
{ | |
private readonly RobotState _currentState; | |
private readonly Order _command; | |
public Change(RobotState currentState, Order command) | |
{ | |
_currentState = currentState; | |
_command = command; | |
} | |
public override int GetHashCode() | |
{ | |
return 17 + 31 * _currentState.GetHashCode() + 31 * _command.GetHashCode(); | |
} | |
public override bool Equals(object obj) | |
{ | |
Change other = obj as Change; | |
return other != null && this._currentState == other._currentState && this._command == other._command; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment