Skip to content

Instantly share code, notes, and snippets.

@cameronfletcher
Created April 12, 2016 06:10
Show Gist options
  • Save cameronfletcher/df9d2d03e26f861c780567982acb1eea to your computer and use it in GitHub Desktop.
Save cameronfletcher/df9d2d03e26f861c780567982acb1eea to your computer and use it in GitHub Desktop.
Car Tracker Application
namespace CarTracker
{
using System;
using System.Linq;
public class Application
{
public static void Main()
{
new Application().Run();
}
public void Run()
{
Console.WriteLine("Car Tracker");
var command = string.Empty;
while (!string.Equals(command, "exit", StringComparison.OrdinalIgnoreCase))
{
try
{
this.Handle(command);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.Write("> ");
command = Console.ReadLine().Trim();
}
Console.WriteLine("Goodbye!");
}
private void Handle(string command)
{
var commandParts = command.Split(new[] { ' ' });
switch (commandParts.First().ToLowerInvariant())
{
case "?":
case "help":
Console.WriteLine(@"Commands:
list - lists all the registered cars
register [reg] - registers a car with the [reg]
drive [reg] [dist] - drives the car with the [reg] the specified [dist]
scrap [reg] - scraps the car with the [reg]");
break;
case "list":
// list cars
break;
case "register":
if (commandParts.Length < 2)
{
Console.WriteLine("Please specify the registration.");
break;
}
// register car: commandParts[1]
Console.WriteLine("Registered {0}.", commandParts[1]);
break;
case "drive":
if (commandParts.Length < 3)
{
Console.WriteLine("Please specify the registration and the distance.");
break;
}
var distance = 0;
if (!int.TryParse(commandParts[2], out distance))
{
Console.WriteLine("Distance must be a number.");
break;
}
// drive car: commandParts[1] distance
Console.WriteLine("Drove {0} a distance of {1:G}km.", commandParts[1], distance);
break;
case "scrap":
if (commandParts.Length < 2)
{
Console.WriteLine("Please specify the registration.");
break;
}
// scrap car: commandParts[1]
Console.WriteLine("Scrapped {0}.", commandParts[1]);
break;
case "":
break;
default:
Console.WriteLine("Eh?");
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment